SyntaxBomb - Indie Coders
Languages & Coding => BlitzMax / BlitzMax NG => Topic started by: braxtonrivers on October 26, 2020, 03:25:49
I have noticed that when dealing with Floats in the latest BlitzMax-NG on Win x64 if it is not implicitly cast then it produces different results as follows, is this normal behaviour or am I doing something wrong?
Thanks in advance
Local test:Float=Float(1/2)
Print test
Outputs:
0.000000000
Local aspectRatio:Float=Float(DesktopWidth()/DesktopHeight())
Print aspectRatio
Outputs:
1.00000000
While the following output as expected.
Local test:Float=1/2.0
Print test
or
Local test:Float=1/Float(2)
Print test
Outputs:
0.500000000
Local aspectRatio:Float=Float(DesktopWidth())/Float(DesktopHeight())
Print aspectRatio
Outputs:
1.77777779
Looks like normal behaviour as I would expect that unless you specifically declare it's a float then it would be treated as an integer. That's the way AGK works too.
Thank you Qube, I still have much to learn, I wasn't quite sure how casting works, I am currently migrating all code to NG when time permits as it is pretty awesome.
Even in vanilla "1/2" will result in "0".
just think of "do it on the right first, then try to do whatever is needed to store it to the left"
f:float = integer divided by integer
step 1: integer divided by integer = integerResult
step 2: store integerresult in float "f"
on output of f the representation type of "float" is used (so 0 becomes 0.0000000011 or similar)
so each time I want to have floats in the calculation too, I cast one of them to float .
floatResult = intX / float(intY)
also stuff like "double" needs to be considered. Stuff like "1.2345" are no double, they are floats. But you can do stuff like this:
local myDouble:double = 1.0:double
bye
Ron