Learn and teach floating-point rounding#
The problem#
0.1 + 0.2 gives 0.30000000000000004, and students conclude that floating
point is approximate and mysterious. To explain it, you need to show what the
numbers really are, what the exact answer is, and why the computer picked the
result it did.
Why the usual approach falls short#
Printing more digits shows more digits of a rounded number, not the exact
value. decimal and fractions can show exact values, but they do not know
the formats, roundings, and orders that real implementations use.
The solution#
ExactRef shows the exact value of every float, the exact answer of every operation, and the rounding that turns one into the other.
What 0.1 really is#
A Python float is a float64: a binary floating-point number stored in 64 bits.
import exactref as er
# The float64 array [0.1]: value((0,)) gives element 0's exact value, and
# to_fraction() writes it as a Fraction
print("exact value:", repr(er.asarray([0.1]).value((0,)).to_fraction()))
exact value: Fraction(3602879701896397, 36028797018963968)
The float nearest 0.1 is a fraction with a power of two below it, slightly more than 0.1.
Why 0.1 + 0.2 gives 0.30000000000000004#
For an operation, ExactRef gives the exact answer and the reference: the exact
answer rounded once to the output format, which it calls rounded.
from fractions import Fraction
# ExactRef's reference for 0.1 + 0.2 in float64
s = er.add(er.asarray([0.1]), er.asarray([0.2]))
# The exact sum as a Fraction. s.exact gives an interval known to hold the
# exact value (an enclosure), as a midpoint and a radius. Here its radius is 0,
# so its midpoint is the exact value.
exact = s.exact.value((0,)).midpoint.to_fraction()
print("exact sum:", repr(exact))
# Is the exact sum as far from 0.3 as from 0.30000000000000004? Fraction(x)
# is the exact value of the float x.
print("halfway:", exact - Fraction(0.3) == Fraction(0.30000000000000004) - exact)
# The exact sum rounded once to float64, and Python's own sum
print("rounded:", s.rounded.tolist())
print("Python:", 0.1 + 0.2)
exact sum: Fraction(10808639105689191, 36028797018963968)
halfway: True
rounded: [0.30000000000000004]
Python: 0.30000000000000004
The exact sum of the two floats lies exactly halfway between the floats
0.3 and 0.30000000000000004. Round to nearest, ties to even, picks the
one whose last bit is 0, which is 0.30000000000000004. Python is right:
that is the correctly rounded sum, the exact sum rounded once.
The exact sum of the floats 0.1 and 0.2 lies exactly halfway between two float64 values. Ties to even picks the one whose last bit is 0.
The five rounding modes#
A value that a format cannot hold lies between two values it can. The rounding mode picks one of them: the nearest, with a tie going to the even one or away from zero, or the one toward zero, toward +inf, or toward -inf:
The two float32 values around 0.1, and which one each rounding mode picks.
# er.Rounding holds the rounding modes of IEEE 754, the floating-point standard
for mode in er.Rounding:
# The decimal 0.1, exactly, rounded to float32 in this mode
tenth = er.asarray([Fraction(1, 10)], "float32", rounding=mode)
print(f"{mode.name}:", tenth.tolist())
NEAREST_EVEN: [0.10000000149011612]
NEAREST_AWAY: [0.10000000149011612]
TOWARD_ZERO: [0.09999999403953552]
TOWARD_POSITIVE: [0.10000000149011612]
TOWARD_NEGATIVE: [0.09999999403953552]
0.1 lies closer to the upper value, so both ways of rounding to nearest pick it, and so does rounding toward +inf.
How coarse small formats are#
A float is stored as a sign, an exponent that picks a power of two, and
fraction bits that pick a value from that power of two up to the next. float8
e4m3fn is an 8-bit format with 4 exponent bits and 3 fraction bits. Here is
every value it has from 1 up to 2, read from its encodings, the bit patterns
that store them:
# An array of shape (8,) from the encodings 0x38 to 0x3f, read as float8_e4m3fn
values = er.Array.from_encodings("float8_e4m3fn", (8,), list(range(0x38, 0x40)))
print("values:", values.tolist())
values: [1.0, 1.125, 1.25, 1.375, 1.5, 1.625, 1.75, 1.875]
e4m3fn keeps 3 fraction bits, so it has 8 values from each power of two
to the next, and the gap between them doubles at every power of two:
The float8 e4m3fn values from 0.5 to 4: 8 between each power of two and the next, so the gap doubles from 1/16 to 1/8 to 1/4.
Why the order of additions matters#
# A float64 sum whose exact value is 1
r = er.sum([1e16, 1.0, -1e16])
# The exact sum rounded once to float64
print("rounded:", r.rounded.tolist())
# normal() adds in index order, rounding each partial sum to float64
print("normal:", r.normal().tolist())
rounded: 1.0
normal: 0.0
The exact sum is 1, but adding in index order loses the 1.0, because
float64 values near 1e16 are 2 apart. How tolerance works
continues from here: why both answers are correct, and how far a correct
implementation can be from the reference.
Subnormals and underflow#
float32 holds numbers down to 2**-126 with full precision. Below that come
its subnormal values, held with fewer bits of precision, down to about
1.4e-45. A smaller value underflows: it rounds to 0 or to that smallest
value.
# 1e-45 rounds to the smallest float32 above 0
print("1e-45:", er.asarray([1e-45], "float32").tolist())
# 1e-46 is closer to 0, so it rounds to 0
print("1e-46:", er.asarray([1e-46], "float32").tolist())
# Rounding toward positive infinity instead of to nearest
up = er.Rounding.TOWARD_POSITIVE
print("1e-46 rounded up:", er.asarray([1e-46], "float32", rounding=up).tolist())
1e-45: [1.401298464324817e-45]
1e-46: [0.0]
1e-46 rounded up: [1.401298464324817e-45]