Is this normal Float behaviour or am I doing something wrong?

Started by braxtonrivers, October 26, 2020, 03:25:49

Previous topic - Next topic

braxtonrivers

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

Qube

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.
Mac Studio M1 Max ( 10 core CPU - 24 core GPU ), 32GB LPDDR5, 512GB SSD,
Beelink SER7 Mini Gaming PC, Ryzen 7 7840HS 8-Core 16-Thread 5.1GHz Processor, 32G DDR5 RAM 1T PCIe 4.0 SSD
MSI MEG 342C 34" QD-OLED Monitor

Until the next time.

braxtonrivers

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.

Derron

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