December 01, 2020, 06:44:43 PM

Author Topic: Is this normal Float behaviour or am I doing something wrong?  (Read 160 times)

Offline braxtonrivers

  • Jr. Member
  • **
  • Posts: 35
Is this normal Float behaviour or am I doing something wrong?
« 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

Offline Qube

  • Administrator
  • Hero Member
  • *****
  • Posts: 2566
  • I mostly code at night... Mostly.
    • SyntaxBomb - Indie Coders
Re: Is this normal Float behaviour or am I doing something wrong?
« Reply #1 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.
Mac mini ( 2018 / 2020 ), 3 GHz 6-Core Intel Core i5, 16 GB 2667 MHz DDR4, 1TB NVMe, eGPU Radeon Pro 580 8 GB, LG Ultragear 27GL83A-B 27 Inch
Commodore VIC-20, 1.1Mhz MOS 6502 CPU, 5KB RAM, VIC ( 6560 ) GPU

Until the next time...

Offline braxtonrivers

  • Jr. Member
  • **
  • Posts: 35
Re: Is this normal Float behaviour or am I doing something wrong?
« Reply #2 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.

Offline Derron

  • Hero Member
  • *****
  • Posts: 3271
Re: Is this normal Float behaviour or am I doing something wrong?
« Reply #3 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