PureBasic - really pure? For example: Str()-Function

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Lebostein
Addict
Addict
Posts: 826
Joined: Fri Jun 11, 2004 7:07 am

PureBasic - really pure? For example: Str()-Function

Post by Lebostein »

Hi,

the new chaos of Str()-functions is very annoying and not pure. Look at this code:

Code: Select all

test1$ = StrD(2.67896543322678)
test2$ = StrF(345.23)
test3$ = Str(201)
test4$ = StrQ(12147483647)

Debug test1$ ; 2.678965
Debug test2$ ; 345.230011
Debug test3$ ; 201
Debug test4$ ; 12147483647
For every variable I must check the type... and the results without the second function parameter are bad.

-----------------------

Look at FreeBasic. Thats pure! And the results are ok:

Code: Select all

test1$ = Str(2.67896543322678)
test2$ = Str(345.23)
test3$ = Str(201)
test4$ = Str(12147483647)

print test1$ ' 2.67896543322678
print test2$ ' 345.23
print test3$ ' 201
print test4$ ' 12147483647

sleep
Clear, simple and really 'pure'....

-----------------------

My wish: a single Str()-Function for all types with optional parameter like this:

Code: Select all

;With second parameter, add or cut decimals:

test1$ = Str(2.67896543322678, 3)
test2$ = Str(345.23, 4)
test3$ = Str(201, 8)
test4$ = Str(12147483647, 2)

Debug test1$ ; 2.678
Debug test2$ ; 345.2300
Debug test3$ ; 201.00000000
Debug test4$ ; 12147483647.00

;Without second parameter, output the number is as is:

test1$ = Str(2.67896543322678)
test2$ = Str(345.23)
test3$ = Str(201)
test4$ = Str(12147483647)

Debug test1$ ; 2.67896543322678
Debug test2$ ; 345.23
Debug test3$ ; 201
Debug test4$ ; 12147483647
Killswitch
Enthusiast
Enthusiast
Posts: 731
Joined: Wed Apr 21, 2004 7:12 pm

Post by Killswitch »

I agree! Although I don't like the idea of a secon parameter - just one, standard Str() function will do.
~I see one problem with your reasoning: the fact is thats not a chicken~
Lebostein
Addict
Addict
Posts: 826
Joined: Fri Jun 11, 2004 7:07 am

Post by Lebostein »

Ok, or two procedures:

Str(value) for integers (byte, char, word, long, quad)
StrR(value [, decimals]) for reals (float, doubles)
User avatar
IceSoft
Addict
Addict
Posts: 1682
Joined: Thu Jun 24, 2004 8:51 am
Location: Germany

Post by IceSoft »

@Lebostein,
You are not right! This is pure!

PURE-SPEED !!!! :twisted:

My wish to @Fred:
Let it be as it is.

But If (fred) want he can write an additional (overloaded) function for our lazy programmers :P
Belive! C++ version of Puzzle of Mystralia
<Wrapper>4PB, PB<game>, =QONK=, PetriDish, Movie2Image, PictureManager,...
Lebostein
Addict
Addict
Posts: 826
Joined: Fri Jun 11, 2004 7:07 am

Post by Lebostein »

Hey, Mr. Shrek. It was a suggestion!!! I want to see here a factually discussion and no obscenities! :?

Pro:
- limited set of functions and easy way to use

Contra:
- losing of speed (the only reason to slow down is the check of the type)
Last edited by Lebostein on Tue Feb 14, 2006 12:33 pm, edited 1 time in total.
User avatar
IceSoft
Addict
Addict
Posts: 1682
Joined: Thu Jun 24, 2004 8:51 am
Location: Germany

Post by IceSoft »

Lebostein wrote:Hey, Mr. Shrek. It was a suggestion!!! I want to see here a factually discussion and no obscenities! :?
Where is the obscenities ? :wink:
Belive! C++ version of Puzzle of Mystralia
<Wrapper>4PB, PB<game>, =QONK=, PetriDish, Movie2Image, PictureManager,...
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

Well, you got half your wish (no obscenities) and perhaps the start to a complete wish-fulfillment. :D
@}--`--,-- A rose by any other name ..
Fred
Administrator
Administrator
Posts: 18161
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

We could do an 'all in one' function, but it will make a slow down and bloat the call to every 'normal' Str() functions (ie: when no quad/double/floats are involved). Also the Str() function itself will be much bigger.
Killswitch
Enthusiast
Enthusiast
Posts: 731
Joined: Wed Apr 21, 2004 7:12 pm

Post by Killswitch »

That's very true. Ah well, it wouldn't be too hard to write our own wrapper function, right?
~I see one problem with your reasoning: the fact is thats not a chicken~
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

Humour a tyro.

With an numeric expression, it is evaluated according to internal rules and conversions by the compiler, which knows the type needed step by step, I assume? These conversions are not handled by the final exe, are they? (Because that would add runtime overhead, surely?)

If so (compiler, not exe, converts) then with something like Str(exp), the current type of expression (the last conversion, so to speak) would be known by the compiler. Could it not then, behind the scenes, just use the StrX that applied?

There are already functions for the various types, so this should add no overhead to the final program, only the compiler is involved in deciding which flavour of Str to use?


Also, wouldn't this be less overhead from the compiler point of view than having to build in checks to handle things like StrF(myDouble.d) or StrQ(myFloat.f) for every permutation of StrX call verses actual data type?


Given my vast experience in writing compilers :P I am probably way off course, but the above seems to make sense. Anyhow, just curious.


Edit:
Removed the worst of the bad grammer. Live with the rest. :)
@}--`--,-- A rose by any other name ..
MLK
User
User
Posts: 57
Joined: Sat Jan 24, 2004 8:46 pm
Location: Germany

Post by MLK »

Fred wrote:Also the Str() function itself will be much bigger.
but there could be added a StrL() function and/or Str could become Str(val[,type]).
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

oooooooo i feel a wish coming up....

* BURP *

yep, there it is

fred, could we get a...

Code: Select all

CompilerTypeIf <varname> = <typeconstant>
  ...
CompilerEndIf
oh i would *love* that!

(hmm, a preprocessor could also handle it, but that would not improve portability)
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
joske
User
User
Posts: 22
Joined: Thu Feb 09, 2006 8:12 pm
Location: Netherlands, Rotterdam

Post by joske »

MLK wrote:but there could be added a StrL() function and/or Str could become Str(val[,type]).
Yes! that sounds good.
Then normally you can use Str(val) :arrow: easy to use,
and if you have any speed critical application you can specify the type with Str(val, type) :arrow: keeping high speed.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

joske wrote:
MLK wrote:but there could be added a StrL() function and/or Str could become Str(val[,type]).
Yes! that sounds good.
Then normally you can use Str(val) :arrow: easy to use,
and if you have any speed critical application you can specify the type with Str(val, type) :arrow: keeping high speed.
There is no technical reason for not making an overloaded version of Str() which replaces itself with the correct type version at compile time, thus still giving the full speed at run-time.
remi_meier
Enthusiast
Enthusiast
Posts: 468
Joined: Sat Dec 20, 2003 6:19 pm
Location: Switzerland

Post by remi_meier »

But Fred would have to introduce explicit casting to explicitly choose a
procedure, what he never wanted (if I remember correctly).
Athlon64 3700+, 1024MB Ram, Radeon X1600
Locked