== Why is this an issue?

https://learn.microsoft.com/en-us/dotnet/api/system.double.nan[double.NaN] and https://learn.microsoft.com/en-us/dotnet/api/system.single.nan[float.NaN] are not equal to anything, not even themselves. 

When anything is compared with `NaN` using one of the https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/comparison-operators[comparison operators] `>`, `>=`, `<`, `<=` or the https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/equality-operators#equality-operator-[equality operator] `==`, the result will always be `false`. In contrast, when anything is compared with `NaN` using the https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/equality-operators#inequality-operator-[inequality operator] `!=`, the result will always be `true`.

Instead, the best way to see whether a variable is equal to `NaN` is to use the https://learn.microsoft.com/en-us/dotnet/api/system.single.isnan[float.IsNaN] and https://learn.microsoft.com/en-us/dotnet/api/system.double.isnan[double.IsNaN] methods, which work as expected.

== How to fix it

=== Code examples

==== Noncompliant code example

[source,csharp,diff-id=1,diff-type=noncompliant]
----
var a = double.NaN;

if (a == double.NaN) // Noncompliant: always false
{
  Console.WriteLine("a is not a number");
}
if (a != double.NaN)  // Noncompliant: always true
{
  Console.WriteLine("a is not NaN");
}
----

==== Compliant solution

[source,csharp,diff-id=1,diff-type=compliant]
----
var a = double.NaN;

if (double.IsNaN(a)) 
{
  Console.WriteLine("a is not a number");
}
if (!double.IsNaN(a)) 
{
  Console.WriteLine("a is not NaN");
}
----


ifdef::env-github,rspecator-view[]

'''
== Implementation Specification
(visible only on this page)

=== Message

Use [double|float].IsNaN() instead.


'''
== Comments And Links
(visible only on this page)

include::../comments-and-links.adoc[]

endif::env-github,rspecator-view[]
