Quick revision
The short answer
With ==, NaN is never equal to anything, even itself, and 0.0 equals -0.0. Double.equals and Double.compare use the bit patterns instead, so NaN equals NaN and 0.0 differs from -0.0, which means a HashSet<Double> keeps one NaN but two zeros. Arrays.sort and Double.compare order values totally, with -0.0 before 0.0 and NaN after positive infinity, while a comparator built from < and > breaks on NaN, so measurement pipelines should normalise or reject NaN at ingestion and document how they treat -0.0.
What to remember
In this answer, we saw that == follows IEEE 754, so NaN is never equal to itself and 0.0 equals -0.0, while Double.equals and Double.compare work from the bits, so NaN equals NaN and the zeros differ. That is why a HashSet<Double> holds one NaN and two zeros, and why Arrays.sort orders -0.0 before 0.0 and NaN last. Comparators built from < and > violate the Comparator contract when NaN is present, so we use Double::compare and clean NaN and -0.0 at ingestion with a documented rule.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
With ==, NaN is never equal to anything, even itself, and 0.0 equals -0.0. Double.equals and Double.compare use the bit patterns instead, so NaN equals NaN and 0.0 differs from -0.0, which means a HashSet<Double> keeps one NaN but two zeros. Arrays.sort and Double.compare order values totally, with -0.0 before 0.0 and NaN after positive infinity, while a comparator built from < and > breaks on NaN, so measurement pipelines should normalise or reject NaN at ingestion and document how they treat -0.0.
Why this question is asked
Measurement data, such as sensor readings, prices from feeds or metrics, almost always contains a few odd values: a division by zero that produced NaN, or a negative reading that rounded to -0.0. Those values behave differently depending on which comparison we use, and the differences surface as duplicate rows, missing rows or a sort that throws an exception. Interviewers ask this question to see whether we know that Java has two sets of rules for doubles and where each one applies. In this answer, we will recap what NaN and -0.0 are, compare the two rule sets, see what happens in a HashSet and in sorting, look at why naive comparators break, and decide how to handle these values in a data pipeline.
1A Quick Recap of NaN and -0.0
IEEE 754, the floating point standard Java follows, reserves some bit patterns for special values. NaN, short for not a number, is the result of undefined operations such as 0.0 / 0.0, Math.sqrt(-1) or infinity minus infinity. It represents a missing or meaningless result, and the standard says it compares unequal to everything.
The standard also has two zeros, because the sign is stored in its own bit. -0.0 appears when a negative result is too small to represent, for example -1e-200 * 1e-200, or from operations such as -1.0 / Double.POSITIVE_INFINITY. Numerically it equals 0.0, but it is a different bit pattern, and it behaves differently in a few places, such as 1.0 / -0.0, which is negative infinity.
2Two Sets of Rules
The operators ==, <, > and friends follow IEEE 754. Double.equals, Double.compare and Double.hashCode do not: they work from the value's bits, using Double.doubleToLongBits, which also maps every NaN bit pattern to one canonical NaN. Java needs the second set because equals must be reflexive (x.equals(x) is always true) for collections to work, and sorting needs a total order in which every pair of values compares consistently.
| Comparison | NaN vs NaN | 0.0 vs -0.0 | Used by |
|---|---|---|---|
| ==, <, > | never equal; < and > false | equal | our own if statements |
Double.equals, hashCode | equal | different | HashSet, HashMap, distinct() |
Double.compare, compareTo | equal; NaN after +Infinity | -0.0 before 0.0 | Arrays.sort, TreeSet, sorted() |
3What Happens in a HashSet<Double>
A HashSet decides whether two elements are duplicates with hashCode and equals, so it follows the bit-based rules. All NaN values collapse into one element, and the two zeros stay separate.
For deduplicating measurements, this means that two readings that are both zero can survive as two distinct values if one came out as -0.0, and that all failed readings, which are NaN, collapse into one. Neither is usually what the person reading the report expects. A TreeSet<Double> gives the same result here, because Double.compare also separates the two zeros and treats NaN as equal to itself.
4Sorting, and Why Naive Comparators Break
Arrays.sort(double[]) and Double.compare give a total order: negative infinity, then negative numbers, then -0.0, then 0.0, then positive numbers, then positive infinity, then NaN. A comparator written with the operators does not, because every comparison involving NaN returns false.
The naive comparator says NaN equals 1.0 and NaN equals 3.0, yet 1.0 is less than 3.0. That breaks the transitivity required by the Comparator contract. On large lists, TimSort can detect the inconsistency and throw IllegalArgumentException with the message Comparison method violates its general contract!, and on small lists it silently produces an order that depends on where the NaN values started. The fix is to use Double::compare or Comparator.naturalOrder(), which follow the total order.
5Handling These Values in a Data Pipeline
The safest approach is to decide at the boundary. At ingestion, we check each value with Double.isNaN and Double.isInfinite and either reject the record, store it as an explicit missing value, or log it, so that NaN never reaches sorting, aggregation or deduplication. Averages are a good reason: a single NaN turns any sum or mean into NaN.
For -0.0, we write down the rule. In most measurement data, -0.0 is just zero, so we normalise it at ingestion with value == 0.0 ? 0.0 : value, which turns both zeros into positive zero. In the few domains where the sign of zero matters, such as some scientific calculations, we keep it and document that equality and grouping treat the two zeros differently.
6Follow-up questions
Why does Java's Double.equals disagree with ==?
Because collections need equals to be reflexive, symmetric and transitive, and hashCode must agree with it. Under IEEE rules NaN is not equal to itself, which would make it impossible to find a NaN in a HashSet. Comparing bits gives a well-behaved equals, at the price of disagreeing with == for NaN and the two zeros.
Does Math.min treat -0.0 differently?
Yes. Math.min(0.0, -0.0) returns -0.0 and Math.max returns 0.0, because the methods treat negative zero as smaller. If any argument is NaN, both return NaN. That differs from writing a < b ? a : b, which returns b for 0.0 and -0.0.
How does the same issue show up in databases?
PostgreSQL's float types treat NaN as equal to itself and greater than every other value, so it can be indexed and sorted, which differs from Java's == but matches Double.compare. Behaviour for -0.0 and NaN varies between databases, so the safe choice is still to clean values before they are stored.
7Conclusion
In this answer, we saw that == follows IEEE 754, so NaN is never equal to itself and 0.0 equals -0.0, while Double.equals and Double.compare work from the bits, so NaN equals NaN and the zeros differ. That is why a HashSet<Double> holds one NaN and two zeros, and why Arrays.sort orders -0.0 before 0.0 and NaN last. Comparators built from < and > violate the Comparator contract when NaN is present, so we use Double::compare and clean NaN and -0.0 at ingestion with a documented rule.
Open with
Java has two sets of rules for doubles. The operators follow IEEE 754: NaN compares unequal to everything, including itself, so NaN == NaN is false, and both NaN < x and NaN > x are false; 0.0 == -0.0 is true.
Then, if they want more
Double.equals, hashCode and compare work from the bit patterns instead, with all NaNs canonicalised, so Double.valueOf(NaN).equals(NaN) is true and 0.0 is not equal to minus 0.0. That is what collections and sorting use.
So a HashSet<Double> keeps a single NaN, no matter how many we add, but keeps 0.0 and minus 0.0 as two elements, and stream distinct behaves the same. Arrays.sort and Double.compare define a total order: minus 0.0 before 0.0 and NaN after positive infinity.
The danger is a hand-written comparator using less than and greater than: it says NaN equals every number, which breaks transitivity, so TimSort can throw Comparison method violates its general contract, or just produce a wrong order. I would always use Double::compare.
For measurement data, I would decide at ingestion: check isNaN and isInfinite and reject or mark the reading as missing, because one NaN also poisons any average, and normalise minus 0.0 to 0.0 unless the domain needs the sign, and document that rule so deduplication and grouping behave predictably.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Explain how NaN and -0.0 behave in ==, Double.equals, Double.compare and in a...": https://scrivio-interview-library.netlify.app/java/types-and-values/a8e21c1ad0/