Detecting a NAN

Blitz3D Forums/Blitz3D Programming/Detecting a NAN

Damien Sturdy(Posted 2004) [#1]
Hey peeps, how can i detect if a Float has buggered up?
when an object resets, it gets moved through another object knocking it flying- which causes it it to move so fast that everything turns NAN.

What i want to do, is detect the NAN, and then reset everything, or at least, give a decent runtime error.


Dreamora(Posted 2004) [#2]
prevent divisions through "0" or nearly 0. I guess you have a FPS bound movement and do not have an FPS precalculation before players can do anything to prevent the 0 division problem.

a possibility is:

function divide#( a#, b# )
epsilon# = 0.0001

if b > epsilon
return a / b
else
return a / epsilon
endif
end function



haven't had NAN cases so far beside the 0 division


Damien Sturdy(Posted 2004) [#3]
heh, im not doing the division though, ODE is. and its just rotation, so the fix is simply to set it to 0 when it happens (well, i hope...)


Perturbatio(Posted 2004) [#4]
=null?


fredborg(Posted 2004) [#5]
If a>a then runtimeerror "NANANANANA"


Koriolis(Posted 2004) [#6]
And to catch infinite values at the same time:
If (f + 1) = f Then RuntimeError("Got a NaN or an Inf")



Damien Sturdy(Posted 2004) [#7]
Cheers, Perfect :D


Tom(Posted 2004) [#8]
There is another way to detect a NaN, wait til xmas and see who gets you the socks! That's your Nan right there! :)


Damien Sturdy(Posted 2004) [#9]
:p

actually, i found

If var=Nan actually worked- now i just have a wheel go missing instead of a grey screen :/


Hotcakes(Posted 2004) [#10]
Nan is a keyword/constant?


DJWoodgate(Posted 2004) [#11]
No.

Cygnus. I believe that just creates a new variable called nan, which would therefore also detect var=0.0. If you want to go down that route though you could define Global zero#=0.0, Infinity#=1.0/zero, Nan#=zero/zero.


Perturbatio(Posted 2004) [#12]
By Jove DJW, that works! cool :)


Michael Reitzenstein(Posted 2004) [#13]
If you'd like them to be constants, you can do this:

Const Infinity = (999.0)^(99999.0)
Const NaN = (-1.0)^(0.5)


Damien Sturdy(Posted 2004) [#14]
Odd, because it actually is only triggering the code when it equals "NaN"... my cars are getting cleared and reloaded when a NAN is detected, but now, every 5 or so clearances causes another error i gota sort.

if var=NaN detects 0 then the cars would be reset whenever they arent moving.. which doesnt happen (im checking a variable as well as displaying its contents on screen)

Interesting stuff this.....


Michael Reitzenstein(Posted 2004) [#15]
NaN isn't a reserved keyword! It's just a variable, which is never setup and thus has the value 0. If var = NaN totally doesn't work, unless you use something like my Const Nan = (-1.0)^(0.5) above.

What also works is;

If var = "NaN"

Since var is automatically cast to a string, and String( <not a number> ) = "NaN".


Koriolis(Posted 2004) [#16]
As well as 'if var="Infinity"' and 'if var="-Infinity"' BTW


Damien Sturdy(Posted 2004) [#17]
Odd, as always, you guys are right- it was working but it did cause glitches. fixed :/