NaN?
Blitz3D Forums/Blitz3D Programming/NaN?
| ||
Hi guys. I've got a system that is using some float variables. Something in the math is cousing it to produce a value that prints to the screen as "NaN". This produces undesirable results in my program. Any idea what this "NaN" is? |
| ||
Not a Number if I recall correctly. Sounds like you're doing something that either won't take floats or it's doing something so drastic to them that it just won't take it. |
| ||
Yep, or a divide by zero. |
| ||
I'm pretty sure a divide by zero would cause Blitz to error out (always did for me). |
| ||
I've had it where it would Nan by a divide by 0 and wouldn't actually crash until I gave the computer input. Whereby it would then give me a divide by 0 error. When I got that the screen would blank out and I'd still see 2D drawing operations but all 3D would be gone. Also I meant to say it is a NaN but it can also happen if you divide by 0 which ... seems to happen to me sometimes and Blitz doesn't immediately crash as I stated above. |
| ||
# divided by 0 = Infinity A NaN can be caused by 0/0 though. Here's a nifty Function to detect these 'values', thanks to Koriolis: Function isNaNOrInf%(f#) Return (f + 1) = f End Function Returns False for a normal number, True for something eville. |
| ||
An integer divide by zero causes an error, but with floats it just does it! |
| ||
12 years old, I'm a topic-necrophile \o/ If you want to make a difference between "NaN" and "Infinity" : Const NAN# = 2^32; this is the value that stores the NaN "number" Function IsInfinity%(f#) If (f=-f) Then Return 0; -> NaN is not infinity ! Return f>NAN; Yep, infinity is bigger than the chaos \o/ End Function Function IsNan%(f#) Return f=NAN; in 32bit value (Float), NaN is just stored as the biggest possible float (2^32) End Function Local i# = 0.0 Local l_Inf# = 1/i Local l_Nan# = i/i Print "l_Inf Is Nan : "+IsNan(l_Inf) Print "l_Inf Is Inf : "+IsInfinity(l_Inf) Print "l_Nan Is Nan : "+IsNan(l_Nan) Print "l_Nan Is Inf : "+IsInfinity(l_Nan) WaitKey() End ps : A division by zero will not result to a MAV or a compile error, it has to be explicit. For example, this should generate a compile error Local i# = 0 Print i/0 While this should report a Nan without any crash. Local i# = 0 Local c% = 0 Print i/c You can so multiply, divide add, substract and so by both infinity and NaN values Local a# = 0 Local b# = a/a Local c# = 1/a Local d# = b/c Local e# = c/b Print b Print c Print d Print e (you'll also notice NaN is the only value you can get if one or both value is NaN) |
| ||
Thanks to Hotcakes & Bobysait for your great Nan code. I shall try those out. I discovered there are possibly 16777214 different variations of Nan (for the 32bit, (IEEE 754) floating point standard). This can be seen as a bonus, allowing you to store hidden data inside Nans (that is 23 bits at a time), and 16777214 unique values. *( Still in that case, it shall become necessary to program a special DLL to intercept floats and handle them like INTs. ) 16777214 = 2^24 -2 : That data was taken from StackOverflow page --> http://tinyurl.com/ju48nrc Here's the basic definition of Nan for floating point values: Therefore, a Nan occurs when all exponent bits are set to 1, (for 16,32,64 ... bit versions) Some bits from x (usually and preferably the first one) can be used to determine the type of NaN: qNan or sNan . There are two types of Nans: qNans (quite Nans) sNans (singaling Nans) Wikipedia link: http://tinyurl.com/pr3ae5e In the case of a signaling Nan, special data may be stored in the Nan, allowing it to act as a place holder for more complex data. =================================================================================================== "C" code to detect a 32bit Nan :: http://tinyurl.com/jrpcle5 "C" code to detect a 64bit Nan :: http://tinyurl.com/j7hupk5 "C" code to detect a 128bit Nan, (requires SSE) --> http://tinyurl.com/hgmtwjk Assembly language to detect 32, 64, or 128 bit versions of Nan ---> http://tinyurl.com/hgmtwjk SIMD instructions for floating point equality comparison (with NaN == NaN) ---> http://tinyurl.com/hnl3h28 =================================================================================================== |