Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing Scala and Java Double.NaN

Tags:

java

nan

scala

Why does this comparison evaluate to true?

scala> Double.NaN equals java.lang.Double.NaN
res5: Boolean = true

But this one evaluates to false?

scala> Double.NaN == java.lang.Double.NaN
res6: Boolean = false

aside: this interesting Twitter thread prompted me to ask this question

like image 760
Kevin Meredith Avatar asked Apr 18 '14 03:04

Kevin Meredith


People also ask

What is double NaN in Java?

NaN. public static final double NaN. A constant holding a Not-a-Number (NaN) value of type double . It is equivalent to the value returned by Double.

What is NaN in Scala?

Scala Float isNaN() method with example Return Type: It returns true if this Float value or the specified float value is Not-a-Number (NaN), or false otherwise.

What is the value of double NaN?

In C#, the Double. NaN field represents a value that is not a number. It is constant.


1 Answers

This is not about Scala's NaN vs. Java's -- there is only one NaN:

scala> val a = Double.NaN
a: Double = NaN

scala> val b = java.lang.Double.NaN
b: Double = NaN

Nor is it about there being two objects with the same value. It's about the definition of NaN. Two NaNs are not == because that's the way NaN is defined -- it is not a number, but rather a special value that means "undefined." If you have two of those, how would you know whether they are equal? For example:

scala> val x = 0.0 / 0.0
x: Double = NaN

scala> val y = Math.sqrt(-1)
y: Double = NaN

scala> x == y
res9: Boolean = false

Fortunately they are not ==; they are not numbers whose values you can compare.

As for x.equals(y), well, why would you do that in Scala? But given that you did, you are running into the bit of Java weirdness that I.K. pointed us to the docs for. Let's demonstrate it:

public class Foo {
  public static void main( String[] args ) {
    double nan1 = 0.0 / 0.0;        Double box1 = nan1;
    double nan2 = Math.sqrt(-1);    Double box2 = nan2;
    System.out.println( nan1 == nan2 );         // false, as expected
    System.out.println( box1.equals(box2) );    // true -- WTF???
  }
}
like image 100
AmigoNico Avatar answered Sep 24 '22 03:09

AmigoNico