On this page
- Why the sum rounds up
- Why 0.1 repeats in binary
- IEEE 754 explained
- Floating-point spacing
- Compare with a tolerance
- Get an exact decimal sum
- Predict the result
- Questions people ask
The short version
- 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.
- 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.
- 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.
One gap, four positions
1 of 4 · Exact decimal 0.3Three tenths sits 0.2 gaps above the lower float.
| Position | Exact 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.
Build a binary fraction
The four digits 0011 repeat after the first zero.
Use Next bit to step through with animations paused.
| Place | Double the remainder | Bit | New remainder |
|---|---|---|---|
| 1 | 1 × 2 = 2 | 0 | 2 |
| 2 | 2 × 2 = 4 | 0 | 4 |
| 3 | 4 × 2 = 8 | 0 | 8 |
| 4 | 8 × 2 = 16 | 1 | 6 |
| 5 | 6 × 2 = 12 | 1 | 2 |
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.
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
| Field | Stored bits | Positions, from 0 |
|---|---|---|
| Sign | 1 | 63 |
| Exponent | 11 | 62–52 |
| Fraction | 52 | 51–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.
The gap to the next larger float
At 2⁰ = 1Binary64 2⁻⁵² · Binary32 2⁻²³
| Starting value | Binary64 gap | Binary32 gap |
|---|---|---|
| 2-4 = 0.0625 | 2-56 | 2-27 |
| 2-3 = 0.125 | 2-55 | 2-26 |
| 2-2 = 0.25 | 2-54 | 2-25 |
| 2-1 = 0.5 | 2-53 | 2-24 |
| 20 = 1 | 2-52 | 2-23 |
| 21 = 2 | 2-51 | 2-22 |
| 22 = 4 | 2-50 | 2-21 |
| 23 = 8 | 2-49 | 2-20 |
| 24 = 16 | 2-48 | 2-19 |
| 25 = 32 | 2-47 | 2-18 |
| 26 = 64 | 2-46 | 2-17 |
| 27 = 128 | 2-45 | 2-16 |
| 28 = 256 | 2-44 | 2-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.
Compare two floats within a tolerance
- Strict equality
- False
- Absolute gap
- 5.551115123125783e−17
- Allowed gap
- 3.000000000000001e−16
Within tolerance.
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.
Add the same inputs two ways
- JavaScript Number
- 0.30000000000000004
- Exact scaled integers
- 0.3
Add 1 + 2 in units of 1/10 to get 3/10.
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.
Which sums pass strict equality?
0.125 + 0.25 === 0.375
Choose an answer to see why.
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).