You must be logged in to reply.

Page 1 of 1 out of 5 messages.

Debugger disagrees with the FEZ Panda II!

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:

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:
Debug.Print("" + nanValue != nanValue); //True
Debug.Print("" + IsNaN(nanValue)); //False

What's going on here?

Thanks
Reply #1 — Posted 4 years ago
by Gus
Reply #1 — Posted 4 years ago
by Gus
There was something about this IIRC. Have you tried this on emulator?
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
This is code based on the IEEE standard 754 .

// @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));
		}
	}
Reply #4 — Posted 4 years ago
by Mike
Reply #4 — Posted 4 years ago
by Mike
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.