SyntaxBomb - Indie Coders

Languages & Coding => BlitzMax / BlitzMax NG => Topic started by: braxtonrivers on October 26, 2020, 03:25:49 AM

Title: Is this normal Float behaviour or am I doing something wrong?
Post by: braxtonrivers on October 26, 2020, 03:25:49 AM
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

Code: [Select]
Local test:Float=Float(1/2)
Print test
Outputs:
0.000000000

Code: [Select]
Local aspectRatio:Float=Float(DesktopWidth()/DesktopHeight())
Print aspectRatio
Outputs:
1.00000000

While the following output as expected.

Code: [Select]
Local test:Float=1/2.0
Print test
or
Code: [Select]
Local test:Float=1/Float(2)
Print test
Outputs:
0.500000000

Code: [Select]
Local aspectRatio:Float=Float(DesktopWidth())/Float(DesktopHeight())
Print aspectRatio

Outputs:
1.77777779
Title: Re: Is this normal Float behaviour or am I doing something wrong?
Post by: Qube on October 26, 2020, 03:43:20 AM
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.
Title: Re: Is this normal Float behaviour or am I doing something wrong?
Post by: braxtonrivers on October 26, 2020, 04:06:26 AM
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.
Title: Re: Is this normal Float behaviour or am I doing something wrong?
Post by: Derron on October 26, 2020, 07:14:53 AM
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
SimplePortal 2.3.6 © 2008-2014, SimplePortal