blitz3d and floats - strange problem

Blitz3D Forums/Blitz3D Beginners Area/blitz3d and floats - strange problem

Dax Trajero(Posted 2004) [#1]
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


Sunteam Software(Posted 2004) [#2]
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.


WolRon(Posted 2004) [#3]
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)


Dax Trajero(Posted 2004) [#4]
are there any commands that can round the number from 997.901 to 997.9 ?


WolRon(Posted 2004) [#5]
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



Rob Farley(Posted 2004) [#6]
Use ints and divide by 10.


Dax Trajero(Posted 2004) [#7]
does divide by 10 use much processor time ?


WolRon(Posted 2004) [#8]
nope