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?
I realize that this is a very old thread, but it came up in my search. What ever came of this? Is there something wrong with double comparisons in involving NaN?
I just encountered something similar and very strange. On a G80 a have a double variable called “new_value” and a double property of the class called “Value”. “new_value” is 0.5 and “Value” is NaN.
My program on the G80 evaluates “new_value != Value” as false
Visual Studio evaluates “new_value != Value” correctly as true in the Watch or Immediate windows.
I took out all of my specific application. Here is a simple test code:
using System;
using Microsoft.SPOT;
namespace TestNaN
{
public class Program
{
public static void Main()
{
double new_value = 0.5;
double Value = double.NaN;
bool equal = new_value == Value;
bool not_equal = new_value != Value;
}
}
}
Here is a copy/paste from the Watch window in Visual Studio 2013:
new_value 0.5 double
Value NaN double
equal true bool
not_equal false bool
new_value == Value false bool
new_value != Value true bool
The results are strange. The variable “equal” should be false, as entering “new_value == Value” into Visual Studio is. Likewise, the variable “not_equal” should be true, as the expression “new_value != Value” is in Visual Studio.
Are the “equal” and “not_equal” variables evaluated and assigned to on the microcontroller, while the expressions entered into the Watch window are evaluated differently, i.e. by Visual Studio?
Do you see this? If not, I guess it’s possible that my whole system is corrupted somehow. I’ve been having several problems lately that apparently others don’t see, e.g. Serial deploy to G80 with FEZ Config or MFDeploy
I’m going to try rebooting again… Rebooting did NOT fix it.
I may try reinstalling everything… (please let me know if it is reproducible. If so, I won’t go through all the trouble)