Detecting a NAN
Blitz3D Forums/Blitz3D Programming/Detecting a NAN
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.
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
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...)
=null?
If a>a then runtimeerror "NANANANANA"
And to catch infinite values at the same time:
If (f + 1) = f Then RuntimeError("Got a NaN or an Inf")
Cheers, Perfect :D
There is another way to detect a NaN, wait til xmas and see who gets you the socks! That's your Nan right there! :)
:p
actually, i found
If var=Nan actually worked- now i just have a wheel go missing instead of a grey screen :/
Nan is a keyword/constant?
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.
By Jove DJW, that works! cool :)
If you'd like them to be constants, you can do this:
Const Infinity = (999.0)^(99999.0)
Const NaN = (-1.0)^(0.5)
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.....
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".
As well as 'if var="Infinity"' and 'if var="-Infinity"' BTW
Odd, as always, you guys are right- it was working but it did cause glitches. fixed :/