Every floating-point operation rounds its exact result to a number of the format, and the small errors that follow explain most surprises of computer arithmetic. This lesson defines round to nearest with ties to even, measures rounding error in units in the last place and with machine epsilon, works out 0.1 + 0.2 bit by bit, and shows why addition is not associative, how a small number can vanish into a large one, and how floating-point numbers should be compared.
Rounding to a floating-point number
A format holds only finitely many numbers (lesson 2.5), so the exact result of an operation often cannot be stored, and the processor must choose one of the two numbers of the format that most closely bracket it.[1] Intel’s manual lists the four rounding modes of IEEE 754 for that choice: round to nearest, round up, round down and round toward zero, with round to nearest as the default.[2] The 2019 version of the standard has a fifth, roundTiesToAway, which settles ties away from zero; the C standard makes its support optional for binary arithmetic.[3]
To round to nearest is to choose the number of the format closest to the exact result. If the exact result lies exactly halfway between two numbers of the format, a tie, the one whose least significant bit is 0, the even one, is chosen.[4]
The other three modes always go one way: round down (toward −∞) takes the closest number not greater than the exact result, round up (toward +∞) the closest not less, and round toward zero the closest not greater in absolute value.[7] Round up and round down are called directed rounding; they serve interval arithmetic, which computes guaranteed lower and upper bounds.[8] Whatever the mode, the error is less than one unit in the last place of the result.[9]
| x | To nearest, ties to even | Down (toward −∞) | Up (toward +∞) | Toward zero |
|---|---|---|---|---|
| 2.6 | 2.5 | 2.5 | 2.75 | 2.5 |
| 2.625 | 2.5 | 2.5 | 2.75 | 2.5 |
| −2.6 | −2.5 | −2.75 | −2.5 | −2.5 |
| 15.7 | +∞ | 15 | +∞ | 15 |
IEEE 754 requires that the result of addition, subtraction, multiplication and division be exactly rounded: computed exactly, and then rounded to the nearest floating-point number with ties to even.[10] Exactly rounded results are also called correctly rounded.[11] The standard gives the same requirement for square roots,[12] so a program that follows it produces the same bits on every machine.
Why ties to even, and not halfway up as in the Decimals unit (rounding decimals)? Rounding every tie up pushes results upward. Requiring the last digit of the result to be even sends ties down half the time and up the other half.[13] Goldberg gives a reason for preferring it that Reiser and Knuth published in 1975:[14] with the round-up rule, a computation that repeatedly subtracts and adds back the same number can drift upward step by step, while with round to even it cannot.[15]
Round 2.6, 2.625 and 2.875 into the toy format of lesson 2.5 (precision 4: between 2 and 4 the step is ¼).
- 2.6 lies between 2.5 and 2.75, 0.1 above the first and 0.15 below the second: it rounds to 2.5 = 1.010₂ × 2¹.
- 2.625 lies exactly halfway between 2.5 = 1.010₂ × 2¹ and 2.75 = 1.011₂ × 2¹. The last fraction bit of 2.5 is 0: it rounds to 2.5.
- 2.875 lies halfway between 2.75 = 1.011₂ × 2¹ and 3 = 1.100₂ × 2¹. Now 3 is the even one: it rounds to 3.
Answer: 2.5, 2.5 and 3. Of the two ties, one went down and one went up.
Round 15.7 and 0.01 into the toy format, whose largest number is 15 and whose smallest positive number is 132.
- 15.7 lies above 15. With unlimited exponents the next number would be 16, and 15.7 is nearer to 16. Since 16 is beyond the format, the result overflows: under round to nearest an operation that overflows produces an infinity of the right sign,[16] here +∞. Only numbers below 15.5, halfway to 16, round down to 15.
- 0.01 lies between 0 and 132 = 0.03125, nearer to 0: the result is 0. An operation whose result underflows gives a subnormal number or a signed zero.[16]
Answer: +∞ and 0. These two cases are the floating-point forms of overflow (lesson 2.3) and underflow.
Round to the toy format
- Round
- 1
- Score
- 0
- Streak
- 0
Round x = 0.390625 to the toy format (precision 4, exponents −2 to 3): tap the number it becomes under round to nearest, ties to even.
Tap a number on the line.
Units in the last place and machine epsilon
Two measures of rounding error are in use. Goldberg’s survey writes ulps for units in the last place: the error of a floating-point number is counted in units of its last significant digit, and even the floating-point number nearest to the correct result may be in error by as much as 0.5 ulp.[17] The relative error is the difference between the two numbers divided by the real number.[18]
For a floating-point number d₀.d₁…d₍ₚ₋₁₎₂ × 2ᴱ of precision p, one ulp is the value of the last digit’s place, 2ᴱ⁻ᵖ⁺¹: the step between it and the next larger number of the same exponent (lesson 2.5, Theorem 3). Python’s function math.ulp returns exactly this, the value of the least significant bit of a float.[19]
For a binary format of precision p, Goldberg’s machine epsilon is ε = 2⁻ᵖ, half an ulp of 1. It bounds the relative error of rounding to nearest.[20] For binary32, ε = 2⁻²⁴ ≈ 5.96 × 10⁻⁸; for binary64, ε = 2⁻⁵³ ≈ 1.11 × 10⁻¹⁶.
Let x be a real number with 2ᴱ ≤ |x| < 2ᴱ⁺¹, where E lies in the normal range of a binary format of precision p, and let fl(x) be x rounded to nearest. If fl(x) is finite (x does not overflow), then |fl(x) − x| ≤ ½ ulp = 2ᴱ⁻ᵖ, and the relative error |fl(x) − x| ÷ |x| is at most ε = 2⁻ᵖ.
Between 2ᴱ and 2ᴱ⁺¹ the numbers of the format are 2ᴱ⁻ᵖ⁺¹ apart (lesson 2.5, Theorem 3), and 2ᴱ⁺¹ is again a number of the format, or, when E is the largest exponent, the point beyond which rounding overflows. So |x| lies in an interval of length 2ᴱ⁻ᵖ⁺¹ between two candidates, and the nearer is at most half that length away: 2ᴱ⁻ᵖ. Since |x| ≥ 2ᴱ, the relative error is at most 2ᴱ⁻ᵖ ÷ 2ᴱ = 2⁻ᵖ.
With exact rounding (section 1), Theorem 1 applies to every operation: the computed sum of two floating-point numbers a and b is (a + b)(1 + δ) with |δ| ≤ ε, as long as the result is normal; the same holds for −, × and ÷. Python’s documentation puts it in words: on most machines the error of each float operation is no more than 1 part in 2⁵³.[21]
“Machine epsilon” has a second meaning. The C standard’s DBL_EPSILON is the difference between 1 and the least value greater than 1 that is representable, b¹⁻ᵖ; for binary64 it is 2⁻⁵² ≈ 2.22 × 10⁻¹⁶,[22] and Python’s sys.float_info.epsilon is defined the same way.[23] That is one ulp of 1, twice Goldberg’s ε. Both are in use: before relying on a stated epsilon, check which definition it follows.
What is one ulp of the binary64 numbers 1, 1,000 and 0.1?
- 1 = 1.0₂ × 2⁰: one ulp is 2⁰⁻⁵² = 2⁻⁵² ≈ 2.22 × 10⁻¹⁶.
- 2⁹ = 512 ≤ 1,000 < 1,024, so E = 9 and one ulp is 2⁹⁻⁵² = 2⁻⁴³ ≈ 1.14 × 10⁻¹³.
- 0.1 is stored as 1.1001…₂ × 2⁻⁴ (lesson 2.5), so one ulp is 2⁻⁴⁻⁵² = 2⁻⁵⁶ ≈ 1.39 × 10⁻¹⁷.
Answer: 2⁻⁵², 2⁻⁴³ and 2⁻⁵⁶. The ulp is proportional to the number’s size, within a factor of 2.
How large is the error of the binary32 value of 0.1, 13,421,773 × 2⁻²⁷ (lesson 2.5), in ulps and relative to ε?
- The error is 13,421,773 × 2⁻²⁷ − 110 = 134,217,730 − 134,217,72810 × 2²⁷ = 210 × 2²⁷ = 0.2 × 2⁻²⁷.
- The value has E = −4, so one ulp is 2⁻⁴⁻²³ = 2⁻²⁷: the error is 0.2 ulp.
- Relative to 0.1 the error is 0.2 × 2⁻²⁷ ÷ 0.1 = 2 × 2⁻²⁷ = 2⁻²⁶ ≈ 1.49 × 10⁻⁸, which is ¼ ε, since ε = 2⁻²⁴.
Answer: 0.2 ulp, a relative error of ¼ ε. Both are within the bounds of Theorem 1, 0.5 ulp and ε.
Round 0.04 into the toy format and compute the relative error. Its ε is 2⁻⁴ = 0.0625.
- 0.04 lies below 0.25, among the subnormal numbers, which are 132 apart: between 132 = 0.03125 and 232 = 0.0625.
- The nearer is 0.03125, with an error of 0.00875 ≤ ½ step.
- Relative to 0.04 the error is 0.00875 ÷ 0.04 ≈ 0.22, more than three times ε.
Answer: 0.03125, with a relative error of about 22%. Theorem 1 holds only in the normal range: subnormal numbers keep a fixed step near 0, like fixed point (lesson 2.4), and lose relative precision.
0.1 + 0.2, bit by bit
In binary64 the sum 0.1 + 0.2 is not the number stored for 0.3. The computation below follows the rule of exact rounding step by step, with the significands in hexadecimal (lesson 1.4): each hexadecimal digit is four bits, so 52 fraction bits are 13 digits.
- 0.1
- 1.999999999999A₁₆ × 2⁻⁴stored as 0x3FB999999999999A
- = 0.CCCCCCCCCCCCD₁₆ × 2⁻³the same number: significand halved, exponent raised by 1 to match 0.2
- 0.2
- 1.999999999999A₁₆ × 2⁻³stored as 0x3FC999999999999A
- sum
- 2.6666666666667₁₆ × 2⁻³the exact sum of the two stored values
- = 1.33333333333338₁₆ × 2⁻²normalized: 13 hexadecimal digits (52 bits) fit; the extra digit 8 = 1000₂ is exactly half an ulp, a tie
- result
- 1.3333333333334₁₆ × 2⁻²rounded to the even last digit: 0x3FD3333333333334
- 0.3
- 1.3333333333333₁₆ × 2⁻²stored as 0x3FD3333333333333: one ulp less
Add the binary64 numbers stored for 0.1 and 0.2 and compare the result with the binary64 number stored for 0.3.
- The stored numbers (lesson 2.5) are 0.1 → 0x3FB999999999999A = 1.999999999999A₁₆ × 2⁻⁴ and 0.2 → 0x3FC999999999999A = 1.999999999999A₁₆ × 2⁻³. Their significands are equal; the exponents differ by 1.
- Align the exponents: halving 1.999999999999A₁₆ moves every bit one place right, which gives 0.CCCCCCCCCCCCD₁₆, so 0.1 = 0.CCCCCCCCCCCCD₁₆ × 2⁻³ exactly. (A₁₆ = 1010 ends in 0, so no bit is lost.)
- Add in hexadecimal columns from the right (Number Bases: arithmetic in other bases): A + D = 23 = 16 + 7, write 7 and carry 1; each 9 + C + 1 = 22 = 16 + 6, write 6 and carry 1; 1 + 0 + 1 = 2. The exact sum is 2.6666666666667₁₆ × 2⁻³.
- Normalize: 2.666…67₁₆ = 10.0110 0110 … 0110 0111₂, and halving gives 1.0011 0011 … 0011 0011 1₂ × 2⁻². The first 52 bits after the point are 3333333333333₁₆, and one bit, a 1, remains: exactly half an ulp.
- It is a tie. The candidates end in …3 (last bit 1) and …4 (last bit 0); ties to even takes …4. The result is 1.3333333333334₁₆ × 2⁻² = 0x3FD3333333333334.
- The number stored for 0.3 is 0x3FD3333333333333: one ulp, 2⁻⁵⁴, smaller.
Answer: 0.1 + 0.2 gives 0.3000000000000000444089209850062616169452667236328125, while 0.3 is stored as 0.299999999999999988897769753748434595763683319091796875. They differ, so a program that tests whether 0.1 + 0.2 equals 0.3 gets “false”. Since version 3.1 Python displays the shortest decimal that converts back to the stored binary value;[24] for 0.3 that is “0.3”, but for this sum no decimal of 16 or fewer significant digits converts back to it (0.3 and 0.3000000000000001 lie too far away), so it is shown with 17, as 0.30000000000000004, the sum rounded to 17 significant digits.
Three roundings met here: 0.1 and 0.2 were rounded when they were stored, and their exact sum was rounded again. The exact sum of the stored values, 0.3000000000000000166533453693773481063544750213623046875, lies exactly halfway between the two binary64 neighbors; the stored values were both a little too large, and the tie went up.
Is 0.1 + 0.2 equal to 0.3 when all three are binary32 numbers?
- The stored values are 0.100000001490116119384765625 and 0.20000000298023223876953125; their exact sum is 0.300000004470348358154296875.
- 0.3 is stored as 0.300000011920928955078125 = 0x3E99999A, the binary32 number nearest to 0.3. Its ulp is 2⁻²⁵.
- The exact sum lies 2⁻²⁷ below it, a quarter of an ulp, and the next number below is a whole ulp away. So the sum rounds to 0x3E99999A.
Answer: yes, in binary32 the two are equal, by the luck of the rounding. The same program can say “equal” in one format and “not equal” in another: equality of floating-point results is not a property of the mathematics.
Laws that fail
Because of roundoff, the associative laws of algebra do not necessarily hold for floating-point numbers.[25] Each operation is exactly rounded, but a sequence of operations rounds at every step, and the order of the steps decides what is rounded.
Compute (0.1 + 0.2) + 0.3 and 0.1 + (0.2 + 0.3) in binary64.
- 0.1 + 0.2 gives 0x3FD3333333333334 (the previous section); adding the stored 0.3 and rounding gives a binary64 number whose value, rounded to 16 significant digits, is 0.6000000000000001.
- 0.2 + 0.3 rounds to exactly 0.5; adding the stored 0.1 and rounding gives the binary64 number nearest to 0.6, the one stored for 0.6 itself.
Answer: the two orders give different numbers, exactly 0.600000000000000088817841970012523233890533447265625 and 0.59999999999999997779553950749686919152736663818359375, one ulp (2⁻⁵³) apart.
Compute (8 + 0.5) + 0.5 and 8 + (0.5 + 0.5) in the toy format, where the step between 8 and 16 is 1.
- 8 + 0.5 = 8.5 lies halfway between 8 = 1.000₂ × 2³ and 9 = 1.001₂ × 2³: a tie, which goes to the even 8. Then 8 + 0.5 is 8 again.
- 0.5 + 0.5 = 1 exactly, and 8 + 1 = 9 exactly.
Answer: 8 and 9. The exact sum is 9: the first order loses both halves.
Let x > 0 be a normal floating-point number below the largest one, and let 0 < y < ½ ulp(x). Then x + y, rounded to nearest, is x: the small number is absorbed. When y = ½ ulp(x), the result is x if x is even and the next number up otherwise.
The next number above x is x + ulp(x) (lesson 2.5, Theorem 3, also when x + ulp(x) is a power of 2). The exact sum x + y lies between x and x + ½ ulp(x), so x is the nearer neighbor. When y is exactly ½ ulp(x), the sum is a tie and ties to even decides.
A binary32 variable starts at 0 and has 1 added to it again and again. Where does it stop growing?
- Every whole number up to 2²⁴ = 16,777,216 is a binary32 number (lesson 2.5, Proposition 5), so each addition is exact up to there.
- At 2²⁴ the ulp is 2²⁴⁻²³ = 2, so 1 is exactly half an ulp. 2²⁴ = 1.0₂ × 2²⁴ has the fraction 0, which is even, so 2²⁴ + 1 rounds back to 2²⁴.
Answer: at 16,777,216. Every later addition of 1 is absorbed, and the variable never changes again. In binary64 the same happens at 2⁵³.
Add step by step
- Round
- 1
- Score
- 0
- Streak
- 0
Compute (10 + 1.75) + 1.5 in the toy format, rounding after each addition. Step 1: tap the toy number that 10 + 1.75 rounds to.
Subtraction has its own hazard. Subtracting two nearly equal numbers is often exact, yet the result can be far from the true answer, because it exposes the rounding errors already in its operands.
In binary64, compute (0.1 + 0.2) − 0.3.
- 0.1 + 0.2 is 0x3FD3333333333334 and 0.3 is 0x3FD3333333333333 (previous section). Their difference is one ulp, 2⁻⁵⁴, exactly.
- 2⁻⁵⁴ ≈ 5.55 × 10⁻¹⁷. The true value of 0.1 + 0.2 − 0.3 is 0.
Answer: 2⁻⁵⁴ instead of 0. The subtraction itself made no new error; it brought to the front the roundings of storing 0.1, 0.2 and 0.3 and of the addition. Relative to the true result 0 the error is unbounded.
Goldberg calls this catastrophic cancellation when the operands are subject to rounding errors,[26] and benign cancellation when they are exactly known;[27] in his example, too, the subtraction introduced no error of its own but exposed the error of earlier operations.[28] How to rearrange formulas so that cancellation does no harm, and how to add many numbers without losing the small ones, are the lessons “Catastrophic cancellation” and “Summing many numbers accurately” of the unit Numerical Computing.
Comparing floating-point numbers
A test of equality between two floating-point numbers compares their exact values. The test itself is exact, but as 0.1 + 0.2 shows, two computations of the same mathematical quantity need not give the same number, and every operation can add a new rounding error.[29] Python’s documentation recommends its function math.isclose for comparing inexact values.[30]
Two numbers a and b are close within a relative tolerance r and an absolute tolerance t when |a − b| ≤ max(r × max(|a|, |b|), t). This is the rule of math.isclose, whose default relative tolerance is 10⁻⁹, about 9 decimal digits.[31]
A relative tolerance suits most comparisons because rounding errors are relative (Theorem 1): the same r serves numbers near 10⁻⁶ and near 10⁶. It fails near 0. Comparing x with 0 by the relative test alone checks |x| ≤ r × |x|, which is false for every nonzero x, so an absolute tolerance is needed there.[32] Two further rules come from the special values: a NaN is equal to nothing, and == returns false if either operand is NaN;[33] +0 and −0 compare equal.[34]
Decide with r = 10⁻⁹ and t = 0 whether 0.1 + 0.2 is close to 0.3, and whether (0.1 + 0.2) − 0.3 is close to 0. What changes with t = 10⁻¹²?
- |(0.1 + 0.2) − 0.3| = 2⁻⁵⁴ ≈ 5.55 × 10⁻¹⁷, and r × 0.3 = 3 × 10⁻¹⁰. Since 5.55 × 10⁻¹⁷ ≤ 3 × 10⁻¹⁰, they are close.
- For x = 2⁻⁵⁴ and 0: |x − 0| = x, but r × max(x, 0) = r × x < x and t = 0. Not close, although x is tiny.
- With t = 10⁻¹²: x ≤ 10⁻¹², so x is close to 0.
Answer: close; not close; close once an absolute tolerance is given. The tolerance must fit the sizes of the numbers involved.
Are 0.5 + 0.25 and 0.75 equal in binary64? And 2⁵⁰ + 3 and 2⁵⁰ + 1 + 2?
- 0.5, 0.25 and 0.75 are stored exactly (they have finite binary numerals with few bits), and their exact sum 0.75 is a binary64 number, so no rounding happens: equal.
- All the whole numbers involved are below 2⁵³, so every step is exact (lesson 2.5, Proposition 5): equal.
Answer: equal, both times. When every input is exactly representable and every exact result is too, floating-point arithmetic is exact and == is the right test. Tolerances are for results that were rounded.
Exercises
Round (a) 5.3, (b) 5.25, (c) 0.03 and (d) 15.6 into the toy format, to nearest with ties to even.
▸ Show solution
(a) 5.3 lies between 5 and 5.5 (step ½), 0.3 and 0.2 away: 5.5.
(b) 5.25 lies halfway between 5 = 1.010₂ × 2² and 5.5 = 1.011₂ × 2²: a tie, and 5 is even. Result 5.
(c) 0.03 lies between 0 and 132 = 0.03125, nearer to 0.03125: the subnormal number 0 000 001.
(d) 15.6 ≥ 15.5: it overflows to +∞.
Round −5.3 into the toy format under each of the four rounding modes.
▸ Show solution
−5.3 lies between −5.5 and −5, 0.2 from −5.5 and 0.3 from −5. To nearest: −5.5. Down (toward −∞): −5.5. Up (toward +∞): −5. Toward zero: −5.
For binary32, find one ulp of 1 and of 1,000, and Goldberg’s machine epsilon. How large can the rounding error of a number near 1,000 be?
▸ Show solution
binary32 has p = 24. One ulp of 1 is 2⁻²³ ≈ 1.19 × 10⁻⁷. 2⁹ ≤ 1,000 < 2¹⁰, so one ulp of 1,000 is 2⁹⁻²³ = 2⁻¹⁴ ≈ 6.10 × 10⁻⁵.
Goldberg’s ε is 2⁻²⁴ ≈ 5.96 × 10⁻⁸. A number near 1,000 is therefore stored with an error of at most 2⁻¹⁵ ≈ 3.05 × 10⁻⁵, and with a relative error of at most ε.
Compute (1 + 0.0625) + 0.0625 and 1 + (0.0625 + 0.0625) in the toy format. Which is right?
▸ Show solution
Near 1 the toy step is ⅛. (1 + 0.0625) + 0.0625: 1.0625 is halfway between 1 = 1.000₂ and 1.125 = 1.001₂, so it rounds to the even 1; again 1 + 0.0625 rounds to 1. Result: 1.
1 + (0.0625 + 0.0625): 0.125 is the exact sum and a toy number (the subnormal 0 000 100), and 1 + 0.125 = 1.125 exactly. Result: 1.125, which is also the exact sum.
What do 2⁵³ + 1, 2⁵³ + 2 and 2⁵³ + 3 give in binary64?
▸ Show solution
At x = 2⁵³ the binary64 ulp is 2⁵³⁻⁵² = 2. Adding 1 gives the tie 2⁵³ + 1, between 2⁵³ (fraction 0, even) and 2⁵³ + 2; it rounds to 2⁵³ (Proposition 2).
Adding 2 gives 2⁵³ + 2 exactly. Adding 3 gives 2⁵³ + 3, a tie between 2⁵³ + 2 (odd last bit) and 2⁵³ + 4 (even): 2⁵³ + 4.
Are a = 10⁻²⁰ and b = 2 × 10⁻²⁰ close with r = 10⁻⁹ and t = 0? With t = 10⁻¹²? What does the example show?
▸ Show solution
|a − b| = 10⁻²⁰ and r × max(|a|, |b|) = 10⁻⁹ × 2 × 10⁻²⁰ = 2 × 10⁻²⁹. With t = 0 they are not close: b is twice a, and relative to their size they differ by 50%.
With t = 10⁻¹², 10⁻²⁰ ≤ 10⁻¹², so they are close. The absolute tolerance hides every difference below it, so it must be chosen for the problem.
Show that when two positive floating-point numbers with y ≤ x are added (normal range), the rounding error of the sum is at most one ulp of x.
▸ Show solution
Let x > 0 be a binary format number with exponent E and y > 0 with y ≤ x. The exact sum lies between x and 2x < 2ᴱ⁺², so its exponent is E or E + 1, and its error after rounding is at most ½ ulp of the sum ≤ ½ × 2ᴱ⁺¹⁻ᵖ⁺¹ = 2ᴱ⁻ᵖ⁺¹ = one ulp of x.
So adding a positive number no larger than x changes x + y by at most one ulp of x, even when the sum moves to the next exponent. If y is less than half an ulp of x, Proposition 2 shows that the whole of y is lost: the error is then y itself.
Summary
- IEEE 754 rounds by default to nearest, ties to even: the closest number of the format, and in a tie the one whose last bit is 0. The other modes round down, up or toward zero. Addition, subtraction, multiplication, division and square root are exactly rounded: computed exactly, then rounded once.
- One ulp of a number with exponent E and precision p is 2ᴱ⁻ᵖ⁺¹. Rounding to nearest errs by at most ½ ulp, and in the normal range by a relative error of at most Goldberg’s machine epsilon ε = 2⁻ᵖ (2⁻⁵³ in binary64). C and Python call 2¹⁻ᵖ, one ulp of 1, epsilon.
- In binary64, 0.1 + 0.2 is a tie that rounds up to 0x3FD3333333333334, one ulp above the 0x3FD3333333333333 stored for 0.3, so the two are not equal; in binary32 the same sum happens to equal 0.3.
- Rounding at every step makes the order of operations matter: addition is not associative, and a number smaller than half an ulp of another is absorbed by it. Subtracting nearly equal results exposes their earlier errors.
- Compare rounded results with a relative tolerance, plus an absolute one near 0; remember that NaN equals nothing and +0 = −0. Exact comparison is right when every value and every result is exactly representable.
Sources
- 1.Intel Corporation, “Intel 64 and IA-32 Architectures Software Developer’s Manual, Volume 1: Basic Architecture (order number 253665-084US)”, Intel (June 2024).Section “4.8.4, “Rounding””. cdrdv2-public.intel.com/825744/253665-sdm-vol-1.pdf · open at the quoted passage.
“To round this result (a), the processor first selects two representable fractions b and c that most closely bracket a in value (b < a < c).”
Used for: when an exact result cannot be stored, the processor chooses between the two numbers of the format that most closely bracket it (the section opens: only a subset of the real numbers can be represented). - 2.Intel Corporation, “Intel 64 and IA-32 Architectures Software Developer’s Manual, Volume 1: Basic Architecture (order number 253665-084US)”, Intel (June 2024).Section “4.8.4, “Rounding””. cdrdv2-public.intel.com/825744/253665-sdm-vol-1.pdf · open at the quoted passage.
“The IEEE Standard 754 defines four rounding modes (see Table 4-8): round to nearest, round up, round down, and round toward zero. The default rounding mode (for the Intel 64 and IA-32 architectures) is round to nearest.”
Used for: IEEE 754 defines four rounding modes, round to nearest, up, down and toward zero; round to nearest is the default. - 3.ISO/IEC JTC 1/SC 22/WG 14, “N3220: working draft of ISO/IEC 9899:2024 (C23), the C programming language standard. A free draft that the C committee lists as similar to the published standard, not the ISO text itself”, open-std.org, the site of the ISO C standards committee (22 February 2024).Section “Annex F, F.3, “Operations”, paragraph 10 (p. 515)”. open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf · open at the quoted passage.
“roundTiesToEven, roundTiesToAway, roundTowardZero, and roundTowardPositive, respectively, for binary floating-point arithmetic. Support for the roundTiesToAway attribute for binary floating-point arithmetic, and hence for the FE_TONEARESTFROMZERO macro, is optional.”
Used for: the standard’s five rounding-direction attributes (the list begins with roundTowardNegative: “represent the ISO/IEC 60559 rounding-direction attributes roundTowardNegative, …”), among them roundTiesToAway, whose support C makes optional for binary arithmetic; ISO/IEC 60559 is IEEE 754–2019 (Annex F.1). - 4.Intel Corporation, “Intel 64 and IA-32 Architectures Software Developer’s Manual, Volume 1: Basic Architecture (order number 253665-084US)”, Intel (June 2024).Section “4.8.4, “Rounding” (Table 4-8, “Rounding Modes and Encoding of Rounding Control (RC) Field”, round to nearest (even))”. cdrdv2-public.intel.com/825744/253665-sdm-vol-1.pdf · open at the quoted passage.
“Rounded result is the closest to the infinitely precise result. If two values are equally close, the result is the even value (that is, the one with the least-significant bit of zero).”
Used for: round to nearest: the closest number; of two equally close numbers, the even one, whose least significant bit is 0. - 5.James Gosling and others, “The Java Language Specification, Java SE 25 Edition, Chapter 15: Expressions”, Oracle (2025).Section “15.4, “Floating-point Expressions””. docs.oracle.com/javase/specs/jls/se25/html/jls-15.html · open at the quoted passage.
“Under the round to nearest rounding policy, inexact results must be rounded to the representable value nearest to the infinitely precise result; if the two nearest representable values are equally near, then the value whose least significant bit is zero is chosen.”
Used for: the same rule in the Java specification, which adds that it corresponds to IEEE 754’s default rounding-direction attribute for binary arithmetic, roundTiesToEven. - 6.James Gosling and others, “The Java Language Specification, Java SE 25 Edition, Chapter 15: Expressions”, Oracle (2025).Section “15.4, “Floating-point Expressions””. docs.oracle.com/javase/specs/jls/se25/html/jls-15.html · open at the quoted passage.
“The round to nearest rounding policy corresponds to the default rounding-direction attribute for binary arithmetic in IEEE 754, roundTiesToEven. The roundTiesToEven rounding-direction attribute was known as the "round to nearest" rounding mode in the 1985 version of the IEEE 754 Standard.”
Used for: IEEE 754 calls the rule roundTiesToEven, its default for binary arithmetic; the 1985 version called it round to nearest. - 7.Intel Corporation, “Intel 64 and IA-32 Architectures Software Developer’s Manual, Volume 1: Basic Architecture (order number 253665-084US)”, Intel (June 2024).Section “4.8.4, “Rounding” (Table 4-8, “Rounding Modes and Encoding of Rounding Control (RC) Field”)”. cdrdv2-public.intel.com/825744/253665-sdm-vol-1.pdf · open at the quoted passage.
“Rounded result is closest to but no greater than the infinitely precise result. Round up (toward +∞) 10B Rounded result is closest to but no less than the infinitely precise result. Round toward zero (Truncate) 11B Rounded result is closest to but no greater in absolute value than the infinitely precise result.”
Used for: round down (toward −∞, the first sentence) takes the closest number not greater than the exact result, round up the closest not less, round toward zero the closest not greater in absolute value. - 8.Intel Corporation, “Intel 64 and IA-32 Architectures Software Developer’s Manual, Volume 1: Basic Architecture (order number 253665-084US)”, Intel (June 2024).Section “4.8.4, “Rounding””. cdrdv2-public.intel.com/825744/253665-sdm-vol-1.pdf · open at the quoted passage.
“The round up and round down modes are termed directed rounding and can be used to implement interval arithmetic.”
Used for: round up and round down are called directed rounding and serve interval arithmetic (bounds for the true result). - 9.Intel Corporation, “Intel 64 and IA-32 Architectures Software Developer’s Manual, Volume 1: Basic Architecture (order number 253665-084US)”, Intel (June 2024).Section “4.8.4, “Rounding””. cdrdv2-public.intel.com/825744/253665-sdm-vol-1.pdf · open at the quoted passage.
“Rounding introduces an error in a result that is less than one unit in the last place (the least significant bit position of the floating-point value) to which the result is rounded.”
Used for: whatever the mode, the rounding error is less than one unit in the last place. - 10.David Goldberg, “What Every Computer Scientist Should Know About Floating-Point Arithmetic”, ACM Computing Surveys 23(1) (1991), pp. 5–48; cited from its edited reprint in Sun Microsystems’ Numerical Computation Guide (May 2000), Appendix D, pp. 173–252.Section “The IEEE Standard”: “Formats and Operations” (“Operations”), p. 193 (the whole topic: pp. 193–194). doi.org/10.1145/103162.103163 · read it online.
“The IEEE standard requires that the result of addition, subtraction, multiplication and division be exactly rounded. That is, the result must be computed exactly and then rounded to the nearest floating-point number (using round to even).”
Used for: IEEE 754 requires addition, subtraction, multiplication and division to be exactly rounded: computed exactly, then rounded to nearest with ties to even. - 11.David Goldberg, “What Every Computer Scientist Should Know About Floating-Point Arithmetic”, ACM Computing Surveys 23(1) (1991), pp. 5–48; cited from its edited reprint in Sun Microsystems’ Numerical Computation Guide (May 2000), Appendix D, pp. 173–252.Section “Rounding Error”: “Exactly Rounded Operations” (footnote 1), p. 185 (the whole topic: pp. 185). doi.org/10.1145/103162.103163 · read it online.
“Also commonly referred to as correctly rounded.”
Used for: exactly rounded results are also called correctly rounded. - 12.David Goldberg, “What Every Computer Scientist Should Know About Floating-Point Arithmetic”, ACM Computing Surveys 23(1) (1991), pp. 5–48; cited from its edited reprint in Sun Microsystems’ Numerical Computation Guide (May 2000), Appendix D, pp. 173–252.Section “Rounding Error”, p. 175 (the whole topic: pp. 175). doi.org/10.1145/103162.103163 · read it online.
“It gives an algorithm for addition, subtraction, multiplication, division and square root, and requires that implementations produce the same result as that algorithm.”
Used for: the standard gives the same requirement for square roots, so that implementations produce the same results. - 13.David Goldberg, “What Every Computer Scientist Should Know About Floating-Point Arithmetic”, ACM Computing Surveys 23(1) (1991), pp. 5–48; cited from its edited reprint in Sun Microsystems’ Numerical Computation Guide (May 2000), Appendix D, pp. 173–252.Section “Rounding Error”: “Exactly Rounded Operations”, p. 185 (the whole topic: pp. 185). doi.org/10.1145/103162.103163 · read it online.
“Another school of thought says that since numbers ending in 5 are halfway between two possible roundings, they should round down half the time and round up the other half. One way of obtaining this 50% behavior to require that the rounded result have its least significant digit be even.”
Used for: requiring the last digit of the rounded result to be even sends halfway cases down half the time and up the other half. - 14.David Goldberg, “What Every Computer Scientist Should Know About Floating-Point Arithmetic”, ACM Computing Surveys 23(1) (1991), pp. 5–48; cited from its edited reprint in Sun Microsystems’ Numerical Computation Guide (May 2000), Appendix D, pp. 173–252.Section “Rounding Error”: “Exactly Rounded Operations”, p. 185 (the whole topic: pp. 185). doi.org/10.1145/103162.103163 · read it online.
“Reiser and Knuth [1975] offer the following reason for preferring round to even.”
Used for: Goldberg gives a reason for preferring round to even that Reiser and Knuth published in 1975 (his Theorem 5). - 15.David Goldberg, “What Every Computer Scientist Should Know About Floating-Point Arithmetic”, ACM Computing Surveys 23(1) (1991), pp. 5–48; cited from its edited reprint in Sun Microsystems’ Numerical Computation Guide (May 2000), Appendix D, pp. 173–252.Section “Rounding Error”: “Exactly Rounded Operations”, p. 185 (the whole topic: pp. 185). doi.org/10.1145/103162.103163 · read it online.
“This example suggests that when using the round up rule, computations can gradually drift upward, whereas when using round to even the theorem says this cannot happen.”
Used for: with round-up, a computation that repeatedly subtracts and adds back the same number can drift upward; with round to even it cannot (Theorem 5). - 16.James Gosling and others, “The Java Language Specification, Java SE 25 Edition, Chapter 15: Expressions”, Oracle (2025).Section “15.4, “Floating-point Expressions””. docs.oracle.com/javase/specs/jls/se25/html/jls-15.html · open at the quoted passage.
“A floating-point operation that overflows produces a signed infinity. A floating-point operation that underflows produces a subnormal value or a signed zero.”
Used for: an operation that overflows produces an infinity of the right sign; one that underflows produces a subnormal number or a signed zero. - 17.David Goldberg, “What Every Computer Scientist Should Know About Floating-Point Arithmetic”, ACM Computing Surveys 23(1) (1991), pp. 5–48; cited from its edited reprint in Sun Microsystems’ Numerical Computation Guide (May 2000), Appendix D, pp. 173–252.Section “Rounding Error”: “Relative Error and Ulps”, p. 177 (the whole topic: pp. 177–178). doi.org/10.1145/103162.103163 · read it online.
“The term ulps will be used as shorthand for “units in the last place.” If the result of a calculation is the floating-point number nearest to the correct result, it still might be in error by as much as .5 ulp.”
Used for: ulps are units in the last place; even the floating-point number nearest to the correct result may be in error by as much as 0.5 ulp. - 18.David Goldberg, “What Every Computer Scientist Should Know About Floating-Point Arithmetic”, ACM Computing Surveys 23(1) (1991), pp. 5–48; cited from its edited reprint in Sun Microsystems’ Numerical Computation Guide (May 2000), Appendix D, pp. 173–252.Section “Rounding Error”: “Relative Error and Ulps”, p. 177 (the whole topic: pp. 177–178). doi.org/10.1145/103162.103163 · read it online.
“relative error, which is simply the difference between the two numbers divided by the real number.”
Used for: the relative error is the difference divided by the real number. - 19.“math — Mathematical functions”, The Python Standard Library, Python 3 documentation, Python Software Foundation.Section “math.ulp(x)”. docs.python.org/3/library/math.html · open at the quoted passage.
“Return the value of the least significant bit of the float x:”
Used for: Python’s math.ulp returns the value of the least significant bit of a float: one ulp. - 20.David Goldberg, “What Every Computer Scientist Should Know About Floating-Point Arithmetic”, ACM Computing Surveys 23(1) (1991), pp. 5–48; cited from its edited reprint in Sun Microsystems’ Numerical Computation Guide (May 2000), Appendix D, pp. 173–252.Section “Rounding Error”: “Relative Error and Ulps”, p. 178 (the whole topic: pp. 177–178). doi.org/10.1145/103162.103163 · read it online.
“Setting ε = (β/2)β⁻ᵖ to the largest of the bounds in (2) above, we can say that when a real number is rounded to the closest floating-point number, the relative error is always bounded by e, which is referred to as machine epsilon.”
Used for: Goldberg’s machine epsilon ε = (β/2)β⁻ᵖ, which is 2⁻ᵖ for β = 2, bounds the relative error of rounding to the closest floating-point number (the reprint prints the second ε as an italic e). - 21.“15. Floating-Point Arithmetic: Issues and Limitations”, The Python Tutorial, Python 3 documentation, Python Software Foundation.
“The errors in Python float operations are inherited from the floating-point hardware, and on most machines are on the order of no more than 1 part in 2**53 per operation.”
Used for: on most machines the error of each float operation is no more than 1 part in 2⁵³. - 22.ISO/IEC JTC 1/SC 22/WG 14, “N3220: working draft of ISO/IEC 9899:2024 (C23), the C programming language standard. A free draft that the C committee lists as similar to the published standard, not the ISO text itself”, open-std.org, the site of the ISO C standards committee (22 February 2024).Section “5.2.5.3.3, “Characteristics of floating types <float.h>”, paragraph 33 (p. 28), and its EXAMPLE 2 (pp. 29–30)”. open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf · open at the quoted passage.
“the difference between 1 and the least normalized value greater than 1 that is representable in the given floating type, b¹⁻ᵖ”
Used for: C’s DBL_EPSILON is the difference between 1 and the next larger representable value, b¹⁻ᵖ; the standard’s example for IEC 60559 binary64 gives DBL_EPSILON = 0X1P-52, that is 2⁻⁵² ≈ 2.2204460492503131 × 10⁻¹⁶. - 23.“sys — System-specific parameters and functions”, The Python Standard Library, Python 3 documentation, Python Software Foundation.Section “sys.float_info”. docs.python.org/3/library/sys.html · open at the quoted passage.
“difference between 1.0 and the least value greater than 1.0 that is representable as a float.”
Used for: Python’s sys.float_info.epsilon (DBL_EPSILON) is the difference between 1.0 and the next larger float. - 24.“15. Floating-Point Arithmetic: Issues and Limitations”, The Python Tutorial, Python 3 documentation, Python Software Foundation.
“Starting with Python 3.1, Python (on most systems) is now able to choose the shortest of these and simply display 0.1.”
Used for: since version 3.1 Python displays the shortest of the decimals that convert to the same binary value (for 0.1 + 0.2 that is 0.30000000000000004). - 25.David Goldberg, “What Every Computer Scientist Should Know About Floating-Point Arithmetic”, ACM Computing Surveys 23(1) (1991), pp. 5–48; cited from its edited reprint in Sun Microsystems’ Numerical Computation Guide (May 2000), Appendix D, pp. 173–252.Section “Systems Aspects”: “Languages and Compilers” (“Ambiguity”), p. 209 (the whole topic: pp. 209). doi.org/10.1145/103162.103163 · read it online.
“Due to roundoff errors, the associative laws of algebra do not necessarily hold for floating-point numbers.”
Used for: because of roundoff, the associative laws of algebra do not necessarily hold for floating-point numbers. - 26.David Goldberg, “What Every Computer Scientist Should Know About Floating-Point Arithmetic”, ACM Computing Surveys 23(1) (1991), pp. 5–48; cited from its edited reprint in Sun Microsystems’ Numerical Computation Guide (May 2000), Appendix D, pp. 173–252.Section “Rounding Error”: “Cancellation”, p. 180 (the whole topic: pp. 180–181). doi.org/10.1145/103162.103163 · read it online.
“Catastrophic cancellation occurs when the operands are subject to rounding errors.”
Used for: catastrophic cancellation: subtracting nearly equal operands that are subject to rounding errors. - 27.David Goldberg, “What Every Computer Scientist Should Know About Floating-Point Arithmetic”, ACM Computing Surveys 23(1) (1991), pp. 5–48; cited from its edited reprint in Sun Microsystems’ Numerical Computation Guide (May 2000), Appendix D, pp. 173–252.Section “Rounding Error”: “Cancellation”, p. 180 (the whole topic: pp. 180–181). doi.org/10.1145/103162.103163 · read it online.
“Benign cancellation occurs when subtracting exactly known quantities.”
Used for: benign cancellation: subtracting exactly known quantities. - 28.David Goldberg, “What Every Computer Scientist Should Know About Floating-Point Arithmetic”, ACM Computing Surveys 23(1) (1991), pp. 5–48; cited from its edited reprint in Sun Microsystems’ Numerical Computation Guide (May 2000), Appendix D, pp. 173–252.Section “Rounding Error”: “Cancellation”, p. 180 (the whole topic: pp. 180–181). doi.org/10.1145/103162.103163 · read it online.
“The subtraction did not introduce any error, but rather exposed the error introduced in the earlier multiplications.”
Used for: in Goldberg’s example the subtraction introduced no error but exposed the errors of earlier operations. - 29.“15. Floating-Point Arithmetic: Issues and Limitations”, The Python Tutorial, Python 3 documentation, Python Software Foundation.
“you do need to keep in mind that it’s not decimal arithmetic and that every float operation can suffer a new rounding error.”
Used for: every floating-point operation can add a new rounding error. - 30.“15. Floating-Point Arithmetic: Issues and Limitations”, The Python Tutorial, Python 3 documentation, Python Software Foundation.
“the math.isclose() function can be useful for comparing inexact values:”
Used for: Python’s documentation recommends math.isclose for comparing inexact values. - 31.“math — Mathematical functions”, The Python Standard Library, Python 3 documentation, Python Software Foundation.Section “math.isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0)”. docs.python.org/3/library/math.html · open at the quoted passage.
“If no errors occur, the result will be: abs(a-b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol).”
Used for: the closeness rule |a − b| ≤ max(r × max(|a|, |b|), t) of math.isclose; its default relative tolerance is 1e-09, about 9 decimal digits (the same paragraph). - 32.“math — Mathematical functions”, The Python Standard Library, Python 3 documentation, Python Software Foundation.Section “math.isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0)”. docs.python.org/3/library/math.html · open at the quoted passage.
“When comparing x to 0.0, isclose(x, 0) is computed as abs(x) <= rel_tol * abs(x), which is False for any nonzero x and rel_tol less than 1.0. So add an appropriate positive abs_tol argument to the call.”
Used for: comparing x with 0 by the relative test alone is false for every nonzero x, so an absolute tolerance is needed. - 33.James Gosling and others, “The Java Language Specification, Java SE 25 Edition, Chapter 4: Types, Values, and Variables”, Oracle (2025).Section “4.2.3, “Floating-Point Types and Values””. docs.oracle.com/javase/specs/jls/se25/html/jls-4.html · open at the quoted passage.
“The equality operator == returns false if either operand is NaN.”
Used for: a NaN is equal to nothing: == returns false if either operand is NaN. - 34.James Gosling and others, “The Java Language Specification, Java SE 25 Edition, Chapter 4: Types, Values, and Variables”, Oracle (2025).Section “4.2.3, “Floating-Point Types and Values””. docs.oracle.com/javase/specs/jls/se25/html/jls-4.html · open at the quoted passage.
“Positive zero and negative zero compare equal, so the result of the expression 0.0==-0.0 is true”
Used for: +0 and −0 compare equal.