Comparing Numbers

Blitz3D Forums/Blitz3D Beginners Area/Comparing Numbers

pc_tek(Posted 2008) [#1]
I am having a little trouble comparing two 'real' numbers to see if they are equal...

sv#=0
ms#=0.44
While Not KeyDown(1)
Print sv+" - "+ms
If sv=ms Then
Stop
Else
sv=sv+.01
EndIf
Delay 100
Wend


When you run the above code, both numbers are displayed and at one point become equal. But the program is ignoring it completely!!

Any ideas anyone?


GfK(Posted 2008) [#2]
Two things.

First, comparing two floats like this is a very bad idea - you can't rely on them ever becoming equal due to inaccuracies of floating point maths.

Second, that "Stop" will be ignored if you don't have Debug on.


pc_tek(Posted 2008) [#3]
Debug is on.

And thats the first I've heard of floating point maths being stupidly unworkable.


Stevie G(Posted 2008) [#4]
Yes, use a tolerance ... You see that there is a tiny difference between the two.

sv#=0
ms#=0.44
tolerance# = 0.000001
While Not KeyDown(1)
	Print sv+" - "+ms

	If Abs( sv-ms) < tolerance
		Print( sv-ms)
		Stop
	Else
		sv=sv+.01
	EndIf
	Delay 100
Wend



pc_tek(Posted 2008) [#5]
WOW! What a solution.

Cheers Stevie!


Ross C(Posted 2008) [#6]
Just remember, if you do:


a# = 0.001

; a will equal 0.001

a = a + 30000

; a will equal 30000.0



Blitz can only hold a certain amount of accuracy in floating point numbers. The above would render a tolorance value useless. I think it's 8 places of accuracy?


pc_tek(Posted 2008) [#7]
Thanks for all your help guys...

Now I know Blitz is crap at tax programs.

LMAO!