An expression tests a value against System.Single.NaN or System.Double.NaN.
Rule descriptionSystem.Double.NaN, which represents a value that's not a number, results when an arithmetic operation is undefined. Any expression that tests for equality between a value and System.Double.NaN always returns false
. Any expression that tests for inequality (!=
in C#) between a value and System.Double.NaN always returns true
.
To fix a violation of this rule and accurately determine whether a value represents System.Double.NaN, use System.Single.IsNaN or System.Double.IsNaN to test the value.
When to suppress warningsDo not suppress a warning from this rule.
ExampleThe following example shows two expressions that incorrectly test a value against System.Double.NaN and an expression that correctly uses System.Double.IsNaN to test the value.
Imports System
Namespace ca2242
Class NaNTests
Shared zero As Double
Shared Sub Main2242()
Console.WriteLine(0 / zero = Double.NaN)
Console.WriteLine(0 / zero <> Double.NaN)
Console.WriteLine(Double.IsNaN(0 / zero))
End Sub
End Class
End Namespace
class NaNTests
{
static double zero = 0;
static void RunIt()
{
Console.WriteLine(0 / zero == double.NaN);
Console.WriteLine(0 / zero != double.NaN);
Console.WriteLine(double.IsNaN(0 / zero));
}
}
Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide. Additional resources In this articleWas this page helpful?
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4