Number systems · Floating point

Why 0.1 + 0.2 Doesn't Equal 0.3 (Floating Point, Visualized)

Why does 0.1 + 0.2 not equal 0.3 in JavaScript and Python? The stored inputs are slightly too large, and their sum lands right on a rounding tie. Flip the bits, watch the repeating binary digits, and see exactly where the extra digit comes from.

8 min read Interactive
Bit explorer

Inside a 64-bit float

Sign · 1 bit
0
Exponent · 11 bits
01111111011
Fraction · 52 bits
1001100110011001100110011001100110011001100110011010
Shortest decimal representation
0.1
Exact stored value 0.1000000000000000055511151231257827021181583404541015625 3602879701896397 / 2^55
0x3FB999999999999A · Normal · exponent −4
Flip any bit. Use arrow keys between bits. Compare the short decimal with the exact stored value.
On this page
  1. Why the sum rounds up
  2. Why 0.1 repeats in binary
  3. IEEE 754 explained
  4. Floating-point spacing
  5. Compare with a tolerance
  6. Get an exact decimal sum
  7. Predict the result
  8. Questions people ask
BinaryFree calculator

The short version

  1. Convert the inputs to binary. The decimal fractions 0.1 and 0.2 repeat in base 2, so binary64 stores the nearest available value for each. Both approximations are slightly high.
  2. Add and round again. The exact sum of the stored inputs lands halfway between two binary64 values. The tie goes to the one with a 0 in its last significand bit.
  3. Compare with the stored 0.3. The decimal literal 0.3 rounds to the lower neighbor. The addition rounds to the upper one, which JavaScript prints as 0.30000000000000004.

Why Does 0.1 + 0.2 Not Equal 0.3?

In exact decimal arithmetic, one tenth plus two tenths is three tenths. In JavaScript, a numeric literal such as 0.1 is read as a binary64 Number. Each input is rounded to that format before the addition. The addition rounds its own result, and typing 0.3 separately rounds the exact decimal three tenths.

The two stored inputs add to 10808639105689191 / 255 before rounding. That value sits exactly halfway between the two blue dots below. The decimal target sits one fifth of the way across the same gap, so its nearest float is the lower dot. Use the steps to follow each value along the same scale.

Step by step

One gap, four positions

Stored floatExact value
Magnified number line from stored 0.3 to the stored sum01 gapStored 0.3Exact 0.3Exact input sumStored sum

1 of 4 · Exact decimal 0.3Three tenths sits 0.2 gaps above the lower float.

Positions are measured from the lower float in units of one binary64 gap. The axis magnifies that single gap.
Exact values around three tenths
PositionExact decimal
Stored 0.3
0 gaps from the lower float
0.299999999999999988897769753748434595763683319091796875
Exact 0.3
0.2 gaps from the lower float
0.3
Exact stored-input sum
0.5 gaps from the lower float
0.3000000000000000166533453693773481063544750213623046875
Rounded sum
1 gaps from the lower float
0.3000000000000000444089209850062616169452667236328125

At a tie, JavaScript uses the IEEE 754 round-to-nearest, ties-to-even rule. “Even” refers to the last bit of the significand. The lower float ends in hexadecimal 3 (binary 0011) and the upper one ends in 4 (binary 0100). The upper one wins. The two rounded values are exactly 2−54 apart, about 5.55 × 10−17.

JavaScript’s usual number-to-string conversion uses a short decimal that round-trips to the same stored number. Typing 0.3 gets you to the lower dot. Typing 0.30000000000000004 gets you back to the upper one.

Why 0.1 Repeats in Binary

After the binary point, the places are halves, quarters, eighths and sixteenths. A finite string of those places always adds up to a fraction with a power of two in the denominator. In lowest terms, 1/10 still has a factor of five in its denominator, so no finite binary string reaches it exactly. The same test predicts that 1/8 ends after three places as 0.001₂.

To get the next bit, double the remainder. If the result is at least one whole denominator, write a 1 and subtract the denominator. Otherwise write a 0. When a remainder comes back, the steps from there repeat. Try one tenth and watch the four-bit cycle return.

Playground

Build a binary fraction

Binary expansion
0.000110011001…₂

The four digits 0011 repeat after the first zero.

Follow the remainders below. Once 2 appears again, the same four bits repeat for 1/10.
First five binary long-division steps for one tenth
PlaceDouble the remainderBitNew remainder
11 × 2 = 202
22 × 2 = 404
34 × 2 = 808
48 × 2 = 1616
56 × 2 = 1212

IEEE 754 Explained Through 64 Bits

A binary64 value has a sign bit, an 11-bit exponent field and a 52-bit fraction field. For a normal number, subtract 1023 from the stored exponent and put an implicit 1 in front of the fraction. That gives 53 bits of precision. The value is the signed significand multiplied by two to the resulting exponent. If scientific notation with powers of ten is familiar, this is the same idea with powers of two.

For 0.1, the stored exponent is 1019, so the power of two is 2−4. The exact value is 3602879701896397 / 255. In the bit explorer, flip the sign bit or flip the last fraction bit and compare how much each edit changes the value.

Decoder

Read a binary64 pattern from hex

Optional 0x prefix. Include any leading zeros.

Class
Normal
Sign bit
0
Exponent field
1019 → −4
Fraction field, hex
999999999999A

Shortest decimal representation 0.1

Stored bits by binary64 field064 bitsSign1Exponent11Fraction52

Each hex digit encodes four bits. Field widths stay the same for every pattern. For other base conversions, use the guide to converting between number bases.
Widths of binary64 fields
FieldStored bitsPositions, from 0
Sign163
Exponent1162–52
Fraction5251–0

An exponent field of all zeros gives signed zero when the fraction is zero, and a subnormal number otherwise. Subnormals use a leading 0 and an exponent of −1022. An exponent of all ones gives infinity if the fraction is zero and NaN (“not a number”) otherwise. Try the presets above to see the same 64 positions interpreted under those rules.

How Floating-Point Error Changes With Scale

Between 2e and the next power of two, each tick in the binary64 fraction field adds 2e−52. That is the spacing within that range. Binary32 has 23 stored fraction bits and a spacing of 2e−23 over the normal ranges shown here. Both gaps double at each power of two.

Chart

The gap to the next larger float

Binary64Binary32
Log-scale spacing above powers of two in binary64 and binary32 2−562−482−402−322−242−16-4048Exponent e in 2ᵉ Binary64 Binary32

At 2⁰ = 1Binary64 2⁻⁵² · Binary32 2⁻²³

Log scale shows the doubling as two parallel lines. On a linear scale, binary64 lies too close to zero to see apart from the baseline. Use the scientific notation converter for more practice reading small numbers.
Exact spacing above each power of two
Starting valueBinary64 gapBinary32 gap
2-4 = 0.06252-562-27
2-3 = 0.1252-552-26
2-2 = 0.252-542-25
2-1 = 0.52-532-24
20 = 12-522-23
21 = 22-512-22
22 = 42-502-21
23 = 82-492-20
24 = 162-482-19
25 = 322-472-18
26 = 642-462-17
27 = 1282-452-16
28 = 2562-442-15

In JavaScript, Number.EPSILON is the gap from 1 to the next larger binary64 number, exactly 2−52. Near 0.3 the gap above a float is a quarter as big. For large numbers the same absolute tolerance can be smaller than a single representable step. Near zero it can be larger than the values you need to distinguish.

How to Compare Floating-Point Numbers

Choose a tolerance that matches what your inputs measure and how the result will be used. An absolute tolerance is a gap in the original units, such as a fraction of a millimeter. A relative tolerance is a fraction of the size of the larger magnitude. Combining them gives the comparison an absolute floor near zero.

Calculator

Compare two floats within a tolerance

Relative tolerance runs from 0 up to 1, excluding 1. Absolute tolerance is 0 or greater.

Strict equality
False
Absolute gap
5.551115123125783e−17
Allowed gap
3.000000000000001e−16

Within tolerance.

Compare the gap with the larger of the absolute tolerance and the relative tolerance times the larger input magnitude. Presets and custom inputs use JavaScript’s stored Number values.

The lab uses the same finite-value rule as Python’s math.isclose. With a zero absolute tolerance and a relative tolerance below one, a nonzero value cannot pass when compared with zero. When your program needs an exact equality check, such as comparing integer identifiers, use exact equality.

How to Get an Exact Decimal 0.3

For an exact decimal total, start with the original decimal text. For values that share a fixed unit, you can turn 0.10 and 0.20 into 10 and 20 hundredths, add those integers, and place the decimal point at the end. In JavaScript, BigInt keeps the integer step exact even past the safe integer range for Number.

Calculator

Add the same inputs two ways

Use plain decimals with a dot, up to 30 digits before and 18 after it. Negative values work too.

JavaScript Number
0.30000000000000004
Exact scaled integers
0.3

Add 1 + 2 in units of 1/10 to get 3/10.

Both exact inputs use the same scale before addition. The result keeps all decimal places implied by that scale.

Python’s Decimal('0.1') + Decimal('0.2') gives Decimal('0.3'). The quoted strings matter. Decimal.from_float(0.1) preserves the binary approximation it receives. Decimal arithmetic can still require rounding, for example when dividing one by three, so set precision and rounding to suit the calculation.

Predict the Result Before You Run It

Check the denominator first, then think about the precision of the operation. Two fractions that end in binary can still produce a result that needs rounding if it takes more bits than the format can keep. These short examples fit whenever all the exact values are finite binary fractions.

Quiz

Which sums pass strict equality?

1 of 4

0.125 + 0.25 === 0.375

Choose an answer to see why.

Each expression uses JavaScript Number arithmetic and strict equality.

Questions people ask

JavaScript rounds 0.1 and 0.2 to binary64 values before adding them. Their exact sum lands on a midpoint and rounds to the upper neighbor, 0.30000000000000004. The literal 0.3 rounds to the lower neighbor, so strict equality returns false.

It is the short decimal representation JavaScript displays for the upper of the two binary64 neighbors around exact decimal 0.3. That float is exactly 0.3000000000000000444089209850062616169452667236328125.

On a typical Python installation, float uses binary64 and rounds the same way for this sum. You can inspect a float with as_integer_ratio() or add Decimal('0.1') and Decimal('0.2') to get exact decimal 0.3.

A value between two representable numbers has to be rounded to fit the chosen format. This can happen when converting inputs or when arithmetic produces a result that needs more precision than the format provides.

IEEE 754 specifies formats and rules for floating-point arithmetic. Binary64 is its 64-bit binary format, with one sign bit, 11 exponent bits and 52 stored fraction bits. Normal values have an implied leading 1, giving 53 bits of significand precision.

A fraction has a terminating binary expansion when its denominator in lowest terms is a power of two. For example, 0.125 is 1/8 and fits in binary64. One tenth repeats. A terminating value must also fit the format’s available precision and exponent range.

Number.EPSILON measures the gap from 1 to the next larger JavaScript Number. Pick tolerances that reflect your input accuracy and the scale of the values. Use an absolute tolerance where you need to handle comparisons near zero.

For fixed-scale totals, keep exact integer units such as cents or use decimal arithmetic initialized from strings. Apply the rounding policy your calculation requires. To display a rounded JavaScript value to one decimal place, use toFixed(1).