You must be logged in to reply.
Page 1 of 1 out of 5 messages.
TOPIC
Debugger disagrees with the FEZ Panda II!
Minor with 210 exp
2 years ago (modified)
Exp:   hardware / software   |   Own:   None
I have a function to check for NaN values in my code. As per the spec, this is done by comparing for equality against itself:

Code Language: C#
public static bool IsNaN(double x)
{
   return x != x;
}


I then pass it a value which is known to be NaN. This should return true. If I stick a breakpoint at the return line, and select "x != x", Visual Studio shows me that the value is true. Great! But when I do a "Debug.Print" on the result, I get false.

What the hell is going on here? Are floating point operations implemented incorrectly on the processor?

Is there any other way to detect a NaN value?

EDIT: Actually, it's better than that:
Code Language: C#
Debug.Print("" + nanValue != nanValue); //True
Debug.Print("" + IsNaN(nanValue)); //False

What's going on here?

Thanks
Employee
2 years ago
Exp:   Excellent hardware / Excellent software   |   Own:   Spider
There was something about this IIRC. Have you tried this on emulator?
Minor with 210 exp
2 years ago
Exp:   hardware / software   |   Own:   None
No. There wouldn't be any point. It works in the debugger, so it'll work in the emulator.

More info here:
http://stackoverflow.com/questions/6557344/what-is-causing-this-to-behave-strangely
Minor with 570 exp
2 years ago
Exp:   Good hardware / Excellent software   |   Own:   Panda II
This is code based on the IEEE standard 754 .

Code Language: C#
// @struct IEEE_DOUBLEREP | allows bit access to 8 byte floats
   //[StructLayout(LayoutKind.Sequential)]
   //public struct ieee_doublerep
   //{
   //    ulong low_mantissa;     // @field low 16 bits of mantissa
   //    ushort mid_mantissa; // @field mid 16 bits of mantissa
   //    uint high_mantissa:4;      // @field high 4 bits of mantissa
   //    uint exponent:11;       // @field exponent of floating point number
   //    uint sign:1;            // @field sign of floating point number
   //};

   public struct DoubleEx
   {
      public const long NANMASK = 0x7FF0000000000000;
      public const long INFINITYMASK = 0x000FFFFFFFFFFFFF;

      public const double NaN = 0.0f / 0.0f;
      public const double NegativeInfinity = -1.0f / 0.0f;
      public const double PositiveInfinity = 1.0f / 0.0f;
      public static bool IsNaNBad(double x)
      {
         return x != x;
      }

      public unsafe static bool IsNaN(double value)        
      {
         long rep = *((long*)&value);
         return ((rep & NANMASK) == NANMASK &&
               ((rep & INFINITYMASK) != 0));
      }

      public unsafe static bool IsPositiveInfinity(double value)
      {
         double negInf = DoubleEx.PositiveInfinity;
         return *((long*)&value) == *((long*)&negInf);
      }

      public unsafe static bool IsNegativeInfinity(double value)
      {
         double posInf = DoubleEx.PositiveInfinity;
         return *((long*)&value) == *((long*)&posInf);
      }

      public unsafe static bool IsInfinite(double x)
      {
         long rep = *((long*)&x);
         return ((rep & NANMASK) == NANMASK &&
               ((rep & INFINITYMASK) == 0));
      }
   }

Legend with 59,767 exp
2 years ago
Exp:   Good hardware / Excellent software   |   Own:   CANxtra, Cerberus, Cerbot, Cobra, Cobra II, Domino, EMX, G120, G400, Game-O, Hydra, Medusa, Panda, Panda II, Raptor, Spider
When using Debub.Print, all values should be strings.

For example,

Debug.Print("xxx=" + (1 == 1).ToStrilng());

See if that helps.

Page 1 of 1 out of 5 messages.
You must be logged in to reply.