다음을 통해 공유


Code Analysis and NaN (Not a Number)

Code Analysis in Visual Studio Team System can be extremely helpful when it comes to pointing out subtle bugs that are hard to detect even via code inspection. One example is dealing with NaN. NaN which stands for Not a Number, represents a constant where the result of an arithmetic operation is undefined. Examples of such operations are:

  • Dividing the number zero by zero
  • Multiplying any number by +ve or –ve infinity
  • The return value of a number of Math functions such as Sin(), when the input is +ve or –ve infinity

To illustrate the point, in the code example below, the value of result is Double.NaN. That is because Sin(∞) = NaN. But what's interesting is that the if condition below returns false, even though result equals double.NaN! Similarly, if you use the inequality operation (!=), the condition returns true! What gives? The problem is that you cannot use the quality operator (==) or the inequality operator (!=) to test whether a value is NaN.

 class Example
{
    static void Main()
    {
        double result = Math.Sin(double.PositiveInfinity);

        if (result == double.NaN)
        {
            result = 0.0;
        }

    }
}

Instead, you have to use the double.IsNaN() or single.IsNaN() to test whether a value is NaN. The code below shows the correct way to test against NaN.

 class Example
{
    static void Main()
    {
        double result = Math.Sin(double.PositiveInfinity);

        if (double.IsNaN(result))
        {
            result = 0.0;
        }
    }
}

With Code Analysis , this bug would have been caught as soon as the developer had written the code since Code Analysis detects this issue and warns the developer about it. In the screenshot below, I have enabled the Code Analysis rule CA2242 which will warn me if I don't test against NaN correctly.

Code Analysis warning CA2242

Not only does Code Analysis warn me about the problem but it also tells me how to fix the issue.

Code Analysis warning.

Habib Heydarian.