Code: Select all
CurrentFrame.f = 3.87199997901917
Elapsed.f = 0.01600000075996
CurrentFrame = Mod(CurrentFrame + Elapsed * 8, 4)
If CurrentFrame >= 4.0
Debug "instead is 4 or more"
Else
Debug "CurrentFrame should be zero"
EndIf

Code: Select all
CurrentFrame.f = 3.87199997901917
Elapsed.f = 0.01600000075996
CurrentFrame = Mod(CurrentFrame + Elapsed * 8, 4)
If CurrentFrame >= 4.0
Debug "instead is 4 or more"
Else
Debug "CurrentFrame should be zero"
EndIf
Code: Select all
CurrentFrame.f = 3.87199997901917
Elapsed.f = 0.01600000075996
CurrentFrame = CurrentFrame + Elapsed * 8
CurrentFrame = Mod(CurrentFrame, 4.0)
If CurrentFrame >= 4.0
Debug "instead is 4 or more"
Else
Debug "CurrentFrame is zero"
EndIf
Code: Select all
CurrentFrame.d = 3.87199997901917
Elapsed.d = 0.01600000075996
CurrentFrame = Mod(CurrentFrame + Elapsed * 8, 4)
If CurrentFrame >= 4.0
Debug "instead is 4 or more"
Else
Debug "CurrentFrame should be zero"
EndIf
It should work with @ricardo_sdl's numbers.The exact range of values, which can be used with floats and doubles to get correct results from arithmetic operations, looks as follows:
Float: +- 1.175494e-38 till +- 3.402823e+38
Double: +- 2.2250738585072013e-308 till +- 1.7976931348623157e+308
More information about the 'IEEE 754' standard you can get on Wikipedia.
Code: Select all
CurrentFrame.f = 3.87199997901917
Elapsed.f = 0.01600000075996
y.f=CurrentFrame + (Elapsed * 8.0)
z.f =Elapsed * 8.0
Debug y
Debug z
Debug z+CurrentFrame
Code: Select all
CurrentFrame.f = 3.87199997901917
Elapsed.f = 0.01600000075996
; CurrentFrame = Mod(CurrentFrame + Elapsed * 8, 4)
If Mod(CurrentFrame + Elapsed * 8, 4) >= 4.0
Debug "instead is 4 or more"
Else
Debug "CurrentFrame should be zero"
EndIf
Or you use mpdecimal library https://www.bytereef.org/mpdecimal/Tawbie wrote:Hi Norm,
The issue with floats here is not the range that they support but the precision with which a float can represent a decimal number, before rounding occurs.
Code: Select all
Procedure.f FMod(a.f, b.f)
ProcedureReturn a - b * Int(a / b)
EndProcedure
You are right. I am sorry for having posted misleading code.Tawbie wrote:Little John's code appears to give the expected result but it is actually calculating a different thing. The original code was attempting to test the remainder of 3.99999998509885 / 4 = 3.99999998509885 which is less than 4 but is not zero!. Little John's code is testing the remainder of 4.0/4 which equals 0.