Sub topic 11
Double Equality: NaN and Negative Zero
Assume you know Java primitive types, wrapper objects, ==, equals, and basic List and Map use. This chapter focuses on two double values that make equality behave differently from many developers expect: NaN and negative zero.
Two kinds of equality
A primitive double uses the == operator defined by IEEE 754 floating-point rules. A Double object uses its equals method. These rules are similar for ordinary values, but they differ for special values.
| Values | Primitive `==` | `Double.equals` |
|---|---|---|
| `1.5` and `1.5` | `true` | `true` |
| `NaN` and `NaN` | `false` | `true` |
| `0.0` and `-0.0` | `true` | `false` |
NaN means “not a number.” Java produces it for some invalid floating-point operations, such as 0.0 / 0.0. It is still a valid double value.
Negative zero is a distinct floating-point representation. It compares equal to positive zero with primitive ==, but it can preserve the direction of a calculation and behave differently in some operations.
NaN is never equal with primitive `==`
The primitive comparison Double.NaN == Double.NaN is false. Therefore, this is not a reliable way to test for NaN:
public class DoubleSpecialValues {
public static void main(String[] args) {
double invalid = 0.0 / 0.0;
System.out.println(invalid); // NaN
System.out.println(invalid == Double.NaN); // false
System.out.println(Double.isNaN(invalid)); // true
}
}Use Double.isNaN(value) when the value is a primitive. If you have a Double reference, value.isNaN() also works when the reference is not null.
Double.equals distinguishes zero signs
Double.equals compares the floating-point representation in a way that treats the two zero signs as different. This matters when a Double is used in a collection or as a map key, because equals and hashCode must agree.
public class DoubleEquality {
public static void main(String[] args) {
double positiveZero = 0.0;
double negativeZero = -0.0;
System.out.println(positiveZero == negativeZero); // true
Double boxedPositive = positiveZero;
Double boxedNegative = negativeZero;
System.out.println(boxedPositive.equals(boxedNegative)); // false
Double firstNaN = Double.NaN;
Double secondNaN = 0.0 / 0.0;
System.out.println(firstNaN.equals(secondNaN)); // true
}
}The autoboxing in this example converts each primitive double to a Double. Calling equals then uses wrapper-object rules, not primitive == rules.
Choosing a comparison method
Use the method that matches the meaning of your comparison:
- Use primitive
==for ordinary numeric equality when you accept IEEE 754 behavior. - Use
Double.isNaNto detectNaN. - Use
Double.equalswhen comparingDoubleobjects and when representation-sensitive equality matters. - Use
Double.comparewhen implementing ordering, sorting, or a comparator.
Double.compare gives a consistent ordering. It treats negative zero and positive zero as different, and it places NaN after ordinary numeric values.
public class DoubleOrdering {
public static void main(String[] args) {
System.out.println(Double.compare(-0.0, 0.0)); // negative number
System.out.println(Double.compare(0.0, -0.0)); // positive number
System.out.println(Double.compare(Double.NaN, 100.0)); // positive number
System.out.println(Double.compare(Double.NaN, Double.NaN)); // 0
}
}Do not confuse exact equality with approximate equality. For calculations involving measurement or rounding, compare the difference with an allowed tolerance instead of requiring exact equality. That issue is separate from the special rules for NaN and signed zero.
Exercise
What does Double.isNaN(0.0 / 0.0) return, and why is 0.0 / 0.0 == Double.NaN not a valid test?
Show answer
It returns true. Primitive floating-point equality says that NaN is unequal to every value, including another NaN.