binary fractions (floats)
BlitzMax Forums/BlitzMax Programming/binary fractions (floats)
| ||
0.600000024 is the nearest to 0.6 for a float type but is this the same on all platforms? ie is this going to work okay on all platforms? is binary rounding a reliable "feature" ? if a>0.600000024 then if a<0.600000024 then if a=0.600000024 then where I need 3 distinct states just for fun! Local a:Float=0.0 While a<>0.600000024 a:+0.1 Print a Wend |
| ||
Maybe you can use an Integer where the upper 16 bits are the main part of the number, and the lower 16 bits are the decimal part. It wouldn't give you great accuracy either but it would be predictable. |
| ||
I really wouldn't recommend making assumptions like that. With floats you should always check that the number lies within certain bounds, ie. in the above case, test that the float equals 0.6 to a given level of accuracy (say 3dp) |
| ||
The best way to handle stuff like that is to use an epsilon value, ie:Const EPSILON:Float = 0.001 Local a:Float = 6.0 Print "a is really: "+a If Abs(a-6.0)<EPSILON Print "a=6.0" ElseIf a-6.0<0.0 Print "a<6.0" Else Print "a>6.0" EndIfIncidentally "a is really 6.0" on my machine :) |
| ||
Also, depending on what you are doing, I have seen source code where people use two ints to store a numerator and denominator. |