E0095

E0095#

Warning name: nan_pattern

A pattern uses a floating-point constant whose value is NaN (not a number). NaN is not equal to any value, including itself, so this pattern never matches. This warning is enabled by default.

Erroneous example#

///|
const Infinity : Double = 1.0e308 * 1.0e308

///|
const NaN : Double = Infinity - Infinity

///|
pub fn describe(value : Double) -> String {
  match value {
    NaN => "not a number"
    _ => "number"
  }
}

The NaN branch is not selected even when value is NaN.

Suggestion#

Use is_nan() in a match guard instead of matching against a NaN constant:

///|
pub fn describe(value : Double) -> String {
  match value {
    x if x.is_nan() => "not a number"
    _ => "number"
  }
}

///|
test {
  inspect(describe(0.0 / 0.0), content="not a number")
  inspect(describe(1.0), content="number")
}