blitz3d and floats - strange problem
Blitz3D Forums/Blitz3D Beginners Area/blitz3d and floats - strange problem
| ||
Just messing around with some code in my game and getting some strange results, so to investigate I simplified the code and STILL got weird results. value# = 1000 For loop = 1 to 25 print value# if value# >=0.1 value# = value# - 0.1 end if Next Now I'd expect for each loop, I'd get values like 1000, 999.9, 999.8 and so on... Can someone explain why its goes... 998.1 998.0 997.901 997.801 |
| ||
It's a little scary I admit, but it is not uncommon. I tested your code on this pc and it does the same. All I can say is that maybe the Blitz products can not be relied upon for 100% accurate floating point maths. However as I mainly use floats for 3D stuff, it doesn't tend to need to be 100% accurate so I'm happy to let little things like this go. |
| ||
inaccuracies in floating point math and Blitz likes to only retain the six most significant digits when converting a float to a string or integer (which it's doing when you print it) |
| ||
are there any commands that can round the number from 997.901 to 997.9 ? |
| ||
Notice what happens if you do this instead:value# = 1000 For loop = 1 To 25 Print value# If value# >=0.1 ;value# = value# - 0.1 value# = value# * 10 value# = value# - 1 value# = value# / 10.0 End If Next WaitKey |
| ||
Use ints and divide by 10. |
| ||
does divide by 10 use much processor time ? |
| ||
nope |