Comparing Numbers
Blitz3D Forums/Blitz3D Beginners Area/Comparing Numbers
| ||
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? |
| ||
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. |
| ||
Debug is on. And thats the first I've heard of floating point maths being stupidly unworkable. |
| ||
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 |
| ||
WOW! What a solution. Cheers Stevie! |
| ||
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? |
| ||
Thanks for all your help guys... Now I know Blitz is crap at tax programs. LMAO! |