Page 1 of 1

Type value for constants or something to fix this

Posted: Wed Jan 11, 2006 11:12 pm
by Psychophanta

Code: Select all

#a=256
#b=(1/3)*#a
Debug #b
Returns wrong.

Code: Select all

#a=256.0
#b=(1/3)*#a
Debug #b
Returns good, but it is ugly to say the compiler a constant is float by adding .0 to the value.

Posted: Thu Jan 12, 2006 5:56 pm
by remi_meier
It isn't a problem for me, but 256f would be shorter (and 256d for doubles :D )

Posted: Thu Jan 12, 2006 5:58 pm
by Polo
#a.f would just be perfect ;)

Posted: Thu Jan 12, 2006 6:10 pm
by remi_meier
But a constant cannot have a type because the 'preprocessor' just inserts
it directly to the code. It would break some 'standard' :wink: (but it would
be ok for me :P )

Posted: Thu Jan 12, 2006 6:22 pm
by Kale
Polo wrote:#a.f would just be perfect ;)
This is how it used to be but was changed, it was just wrong. 8)

Code: Select all

#a=256.0
I think this is ok, just add a '.0' if you want the compiler to use floats.

Posted: Thu Jan 12, 2006 6:50 pm
by Psychophanta
Kale wrote:I think this is ok, just add a '.0' if you want the compiler to use floats.
It is ok, and functional, but ugly, not very elegant
I think there must be better solution.:?

Posted: Thu Jan 12, 2006 11:46 pm
by Rescator
Not really, as a constant is just a constant that is glued in as is into a line of code.

So if you have to use .0 in the code line then you also need to use .0 in the constant.

Posted: Tue Jan 17, 2006 6:02 pm
by Psychophanta

Code: Select all

#M2=0.5
Debug #M2
#M=1/2
Debug #M
Do you see what i want to say? Is it consistent?

EDIT: Well, i've said nothing but repeat ... forget

Posted: Tue Jan 17, 2006 8:47 pm
by blueznl
psycho, yes, it is consistent, and you know :-)

Posted: Tue Jan 17, 2006 11:46 pm
by Rescator
It would really confuse beginners, as you would have so many variations.
#a.f=256 ;integer forced to float
#a=256.0 ;float
#a.f=256.0 ;float float?
#a=256 ;integer

I don't know about you folks but I find having just
#a=256.0 ;float
#a=256 ;integer
so much simpler :)

I've made it a habit to always use .0 on "round" floats.

Posted: Mon Mar 03, 2008 2:35 pm
by #NULL
<edit>
stupid code removed :roll: :lol:

Posted: Mon Mar 03, 2008 3:47 pm
by Kaeru Gaman

Code: Select all

#a=256.0
#b=(1/3)*#a
Debug #b
Debug wrote:0.0
*puzzled*

...so, who said it did right?

Code: Select all

#a=256
#b=#a/3
Debug #b

Code: Select all

#a=256.0
#b=#a/3
Debug #b
...mind the typecast order in PB.
it's always an issue, so you should be used to it meanwhile...

Posted: Mon Mar 03, 2008 8:07 pm
by Psychophanta
Kaeru Gaman wrote:

Code: Select all

#a=256.0
#b=(1/3)*#a
Debug #b
Debug wrote:0.0
*puzzled*

...so, who said it did right?
I did. It worked for previous versions.
Now you need to do:

Code: Select all

#a=256.0
#b=1.0/3.0*#a
Debug #b
:?

Posted: Mon Mar 03, 2008 8:38 pm
by Kaeru Gaman
yup, but it's still a typecast-problem.

like this one:
http://www.purebasic.fr/english/viewtopic.php?p=202206

but I think, especially for constants it should be no problem to write it in a way that will work, e.g.

Code: Select all

#b=#a/3 
PS:
I agree that it would be nice to type constants, but this will be no solution for this problem.

Code: Select all

#a.f=256
#b.f=(1/3)*#a
Debug #b
will produce the same problem, because 1 and 3 are integer and result in 0