31

My method is as follows

  def myMethod(myDouble: Double): Double = myDouble match {
    case Double.NaN => ...
    case _ => ...
  }

The IntelliJ debugger is showing NaN but this is not being picked up in my pattern matching. Are there possible cases I am omitting

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
deltanovember
  • 42,611
  • 64
  • 162
  • 244

3 Answers3

48

It is a general rule how 64-bit floating point numbers are compared according to IEEE 754 (not Scala or even Java related, see NaN):

double n1 = Double.NaN;
double n2 = Double.NaN;
System.out.println(n1 == n2);     //false

The idea is that NaN is a marker value for unknown or indeterminate. Comparing two unknown values should always yields false as they are well... unknown.


If you want to use pattern matching with NaN, try this:

myDouble match {
    case x if x.isNaN => ...
    case _ => ...
}

But I think pattern matching will use strict double comparison so be careful with this construct.

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
  • +1 for pointing out that `NaN != NaN` in many (if not all) programming languages that expose this value to the programmer. – Ray Toal Aug 02 '11 at 06:57
  • 1
    Note that if most numbers are not NaN, `.isNaN` is only about half as fast as `java.lang.Double.isNaN`, so the latter should be preferred in tight loops. (The match is as fast as an if statement.) For clarity (i.e. everywhere except tight performance-critical loops), the `.isNaN` form is probably best. – Rex Kerr Aug 02 '11 at 16:12
12

You can write an extractor (updated according to bse's comment):

object NaN {
  def unapply(d:Double) = d.isNaN
}


0.0/0.0 match {
  case NaN() => println("NaN")
  case x => println("boring " + x)
}
//--> NaN
Landei
  • 54,104
  • 13
  • 100
  • 195
  • 2
    The extractor can be even simpler: `object NaN { def unapply(d:Double) = d.isNaN }`. It can be used like this: `0.0/0.0 match { case NaN() => print("NaN") }` – bseibold Aug 02 '11 at 13:51
4

Tomasz is correct. You should use isNaN instead.

scala> Double.NaN.isNaN
res0: Boolean = true
hammar
  • 138,522
  • 17
  • 304
  • 385