Floating points - how is this possible?

Blitz3D Forums/Blitz3D Programming/Floating points - how is this possible?

fall_x(Posted 2005) [#1]
I'm trying to calculate the height of an entity relative to the terrain. Pretty easy you'd think, but there is a problem.
How can this code :

py#=entityy(pos)
ty#=terrainy(e_terrain\terrain,entityx(pos),0,entityz(pos))
text 10,160,py+" - "+ ty + " = " + (py-ty)


Generate this result? (actual screenshot of this exact code)



Shouldn't it be 3.5 ? There is probably a very logical explenation for this, but I don't understand.


GfK(Posted 2005) [#2]
There is probably a very logical explenation for this
Single precision floats aren't accurate and results will vary from PC to PC. I get 3.5 for the above.

Try this if you really need it restricted to 1 decimal place:
a#=15.6569
b#=12.1553
Print a#-b#
Print Round(a#-b#)
WaitKey
End

Function Round#(v#)
	return Float(Left$(Str$(v#),Instr(Str$(v#),".")+1))
End Function



fall_x(Posted 2005) [#3]
Is there a way to solve it?

Because this is the problem... I have an entity at that position, and another entity at the exact same spot (which loads its coords from a file with some readlines, but it has the exact same xyz coordinates in that file). If I calculate the height between my first entity and the terrain, it gives me 3.5 - but if I calculate the height between the second entity and the terrain, it gives me the result as in the screenshot above.
Yet if I display the coordinates of both entities, they are exactly the same.
It's really weird.


fall_x(Posted 2005) [#4]
Ok, storing the values as actual floats with WriteFloat seems to solve it, but I still don't get it. It used to read it with ReadLine, and the value in the file was 15.6569, and this was stored in a floating point value and the entity was positioned there (Y). If I show it's Y coordinate, it gives me 15.6569 - yet in calculations it's wrong.
But when storing it with WriteFloat and reading it with ReadFloat, it can be used in calculations without a problem.
Weird.


Ross C(Posted 2005) [#5]
Floats are rounded to a certain precision., so i think that calculation introduces an extra decimal place, and that gets rounded.


Ross C(Posted 2005) [#6]
Sorry for the late responce :S


flying willy(Posted 2005) [#7]
NEVER test for equals when you use floats. NO-ONE does this... because floats will always be slightly different on different pc's.

If you want to test for 3.5 you should do... if abs(num-3.5)<0.01 instead.