Scrivio Interview library
CodingFoundations6 min read · updated 12 November 2026

Why does System.out.println(0.1 + 0.2 == 0.3); print false, and how should you compare doubles?

0.1, 0.2 and 0.3 cannot be stored exactly in binary floating point, so each double holds the nearest representable value, and the rounding errors add up: 0.1 + 0.2 is 0.30000000000000004, which is not the same double as 0.3. We compare doubles with a tolerance that suits the size of the numbers, for example Math.abs(a - b) < 1e-9, or use BigDecimal when we need exact decimal results. We should also know that == treats NaN as unequal to itself and 0.0 as equal to -0.0, while Double.compare and Double.equals do the opposite.

Why this question is asked

Every developer meets this surprise at some point, and it is not a Java quirk: the same happens in JavaScript, Python, C and any language that uses IEEE 754 doubles. Interviewers ask it to check that we understand why the result is not exact, and, more importantly, that we know how to compare floating point values correctly in tests, in business rules and in sorting. In this answer, we will recap how doubles store numbers, see where 0.30000000000000004 comes from, compare with a tolerance, look at BigDecimal for exact decimal work, and then cover the special values NaN and -0.0.

1A Quick Recap of Binary Floating Point

A double stores a number as a binary fraction times a power of two, with 53 bits of precision. Numbers that are sums of powers of two, such as 0.5, 0.25 or 0.75, are stored exactly. Most decimal fractions are not. Just as 1/3 is 0.333... forever in decimal, 1/10 is a repeating fraction in binary: 0.000110011001100... So the double for 0.1 is the closest 53-bit binary value, which is very slightly more than 0.1.

2Where 0.30000000000000004 Comes From

Each literal is rounded once when it is stored, and the addition rounds again. The errors do not cancel out here, so the sum ends up one step above the double nearest to 0.3.

Java
System.out.println(0.1 + 0.2); // 0.30000000000000004
System.out.println(0.3); // 0.3
System.out.println(0.1 + 0.2 == 0.3); // false
System.out.println(new BigDecimal(0.1)); // 0.1000000000000000055511151231257827021181583404541015625
System.out.println(0.5 + 0.25 == 0.75); // true: these are exact in binary
What the doubles really hold
ExpressionPrinted by JavaExact in binary?
0.10.1no, slightly above 0.1
0.20.2no, slightly above 0.2
0.1 + 0.20.30000000000000004no, one step above the double for 0.3
0.30.3no, slightly below 0.3
0.5 + 0.250.75yes

Notice that Java prints 0.3 for the literal even though it is not exactly 0.3. Double.toString prints the shortest decimal that identifies that double uniquely, which hides the error until arithmetic makes it visible. The BigDecimal constructor shows the exact value the double holds.

3Comparing With a Tolerance

Since tiny errors are expected, we compare doubles by asking whether they are close enough rather than identical. The question is what close enough means. A fixed absolute tolerance, such as 1e-9, works for values of known, moderate size. For values that can be very large or very small, a relative tolerance scales with the numbers being compared.

Java
static boolean nearlyEqual(double a, double b, double absTol, double relTol) {
double diff = Math.abs(a - b);
return diff <= absTol || diff <= relTol * Math.max(Math.abs(a), Math.abs(b));
}
nearlyEqual(0.1 + 0.2, 0.3, 1e-12, 1e-9); // true
nearlyEqual(1e15 + 0.2, 1e15, 1e-12, 1e-9); // true: relative tolerance
nearlyEqual(1.0, 1.1, 1e-12, 1e-9); // false
assertEquals(0.3, 0.1 + 0.2, 1e-9); // JUnit: the third argument is the delta

The right tolerance depends on the domain. A sensor reading accurate to 0.01 can use 1e-3; a physics simulation might need a tolerance tied to Math.ulp, the gap between a double and its next neighbour. In tests, JUnit's assertEquals for doubles takes a delta argument for exactly this reason.

4When We Need Exact Decimals: BigDecimal

Tolerance is fine for measurements, but not for money or anything that must match a decimal record exactly. For those, BigDecimal stores the decimal digits exactly. We must create it from a String or with BigDecimal.valueOf, not from the double constructor, which captures the binary error.

Java
BigDecimal a = new BigDecimal("0.1");
BigDecimal b = new BigDecimal("0.2");
System.out.println(a.add(b)); // 0.3
System.out.println(a.add(b).compareTo(new BigDecimal("0.3"))); // 0: numerically equal

We compare BigDecimal values with compareTo, because equals also compares the scale, so 0.30 and 0.3 are not equal under equals.

5The Special Values: NaN and -0.0

IEEE 754 also defines NaN, which means not a number and results from operations such as 0.0 / 0.0, and a negative zero. The == operator follows the IEEE rules: NaN is not equal to anything, not even itself, and 0.0 == -0.0 is true. Double.compare and Double.equals instead define a total order for sorting and hashing: they treat NaN as equal to itself and distinguish 0.0 from -0.0.

Java
double nan = 0.0 / 0.0;
System.out.println(nan == nan); // false
System.out.println(Double.isNaN(nan)); // true: the correct test
System.out.println(Double.compare(nan, nan)); // 0
System.out.println(0.0 == -0.0); // true
System.out.println(Double.compare(0.0, -0.0)); // 1: 0.0 sorts after -0.0
System.out.println(Double.valueOf(0.0).equals(-0.0)); // false

So to check for NaN we use Double.isNaN, never x == Double.NaN, which is always false. And we should be aware that collections and sorting use the Double.compare and equals rules, while our own comparisons with == use the IEEE rules.

6Follow-up questions

What does 1.0 / 0 give versus 1 / 0?

1.0 / 0 is floating point division, because one operand is a double, and IEEE 754 defines it as positive infinity, so it prints Infinity without any exception. 1 / 0 is integer division, and integers have no infinity, so Java throws ArithmeticException with the message / by zero. Similarly, 0.0 / 0 gives NaN, while 0 / 0 throws.

Why not always use BigDecimal?

Because it is much slower, allocates an object for every result, and is clumsy to write. For scientific, statistical and graphics work, double is the right tool and tolerance comparisons are the right technique. BigDecimal is for decimal values that must be exact, mainly money and quantities with fixed decimal places.

Does using float instead of double change the picture?

It makes it worse. float has only 24 bits of precision, about seven decimal digits, so errors appear sooner and are larger. 0.1f + 0.2f happens to print 0.3 because of the shorter rounding, but float arithmetic still drifts quickly in loops.

7Conclusion

In this answer, we saw that 0.1, 0.2 and 0.3 have no exact binary representation, so 0.1 + 0.2 is the double 0.30000000000000004 and == returns false. We compared doubles with an absolute or relative tolerance suited to the magnitude, used BigDecimal built from strings for exact decimal work, and covered the special values: NaN is unequal to itself under == and must be tested with Double.isNaN, while Double.compare and Double.equals treat NaN as equal to itself and separate 0.0 from -0.0.

What to say in the interviewAbout 90 seconds aloud

Open with

It prints false because doubles are binary IEEE 754 numbers, and 0.1, 0.2 and 0.3 are repeating fractions in binary, like one third in decimal. Each literal is stored as the nearest 53-bit binary value, and the addition rounds again, so 0.1 plus 0.2 ends up as 0.30000000000000004, one step above the double that 0.3 maps to.

Then, if they want more

Java prints the literal 0.3 as 0.3 because Double.toString shows the shortest decimal that identifies the value, which hides the error. So I never compare computed doubles with ==.

I compare with a tolerance suited to the magnitude: Math.abs(a - b) less than something like 1e-9 for moderate values, or a relative tolerance scaled by the larger of the two when values can be very large or small; in JUnit, assertEquals with a delta.

When I need exact decimal results, such as money, I use BigDecimal created from strings or BigDecimal.valueOf, and compare with compareTo. I would also mention the special values. With ==, NaN is not equal to itself, so I test it with Double.isNaN, and 0.0 equals minus 0.0.

Double.compare and Double.equals are different: they treat NaN as equal to itself and order minus 0.0 before 0.0, which is what sorting and hash-based collections use. And 1.0 divided by 0 gives Infinity, while integer 1 divided by 0 throws ArithmeticException.

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.
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 "Why does System.out.println(0.1 + 0.2 == 0.3); print false, and how should you compare...": https://scrivio-interview-library.netlify.app/java/types-and-values/704ab0082b/