TExcelTAD
Home › Numerical Methods › Floating-Point Numbers

Floating-Point Numbers

Computers store real numbers in floating point: a sign, a fixed number of significant binary digits, and an exponent, the binary version of scientific notation. Almost every number in a numerical computation is rounded to this format, so its properties set the limits of everything else in this course.

The double-precision format

The IEEE 754 double (Python's float, NumPy's float64) stores

x=±(1.b1b2…b52)2×2e,−1022≤e≤1023,x = \pm (1.b_1b_2\ldots b_{52})_2 \times 2^{e}, \qquad -1022 \le e \le 1023,

in 64 bits: 1 sign bit, 11 exponent bits and 52 fraction bits (the leading 1 is implicit, so there are 53 significant bits).

Three numbers describe the format:

Quantity Meaning Value for double
machine epsilon ϵmach\epsilon_{\text{mach}} gap between 1 and the next float 2−52≈2.22×10−162^{-52} \approx 2.22 \times 10^{-16}
largest float overflow threshold ≈1.80×10308\approx 1.80 \times 10^{308}
smallest normal float underflow threshold 2−1022≈2.23×10−3082^{-1022} \approx 2.23 \times 10^{-308}
python · try it yourself
import numpy as np

info = np.finfo(np.float64)
print("eps   :", info.eps, "=", 2.0**-52)
print("max   :", info.max)
print("tiny  :", info.tiny)
print("1 + eps   == 1 ?", 1.0 + info.eps == 1.0)
print("1 + eps/2 == 1 ?", 1.0 + info.eps / 2 == 1.0)
print("float32 eps:", np.finfo(np.float32).eps)
eps   : 2.220446049250313e-16 = 2.220446049250313e-16
max   : 1.7976931348623157e+308
tiny  : 2.2250738585072014e-308
1 + eps   == 1 ? False
1 + eps/2 == 1 ? True
float32 eps: 1.1920929e-07

Floats are not evenly spaced

Between 2e2^e and 2e+12^{e+1} there are exactly 2522^{52} equally spaced floats, so the gap is 2e−522^{e-52}: it doubles every time the exponent goes up. The relative gap stays about ϵmach\epsilon_{\text{mach}} everywhere. np.spacing(x) returns the gap after xx.

python · try it yourself
import numpy as np

for x in (1.0, 1000.0, 1e16, 2.0**53):
    print(f"spacing after {x:.6g}: {np.spacing(x):.6g}, relative {np.spacing(x) / x:.3g}")
print("2**53 + 1 == 2**53 ?", 2.0**53 + 1 == 2.0**53)
spacing after 1: 2.22045e-16, relative 2.22e-16
spacing after 1000: 1.13687e-13, relative 1.14e-16
spacing after 1e+16: 2, relative 2e-16
spacing after 9.0072e+15: 2, relative 2.22e-16
2**53 + 1 == 2**53 ? True

Beyond 253≈9×10152^{53} \approx 9 \times 10^{15} even integers can no longer all be represented, which matters for large IDs stored as floats (for example in JSON read by JavaScript).

Rounding and the fundamental property

Every real number xx in range is rounded to the nearest float fl⁡(x)\operatorname{fl}(x) with

fl⁡(x)=x(1+δ),∣δ∣≤12ϵmach=u,\operatorname{fl}(x) = x(1 + \delta), \qquad \lvert\delta\rvert \le \tfrac{1}{2}\epsilon_{\text{mach}} = u,

where u≈1.1×10−16u \approx 1.1 \times 10^{-16} is the unit roundoff. IEEE arithmetic guarantees that +,−,×,÷+, -, \times, \div and  \sqrt{\ } give the correctly rounded exact result: fl⁡(a∘b)=(a∘b)(1+δ)\operatorname{fl}(a \circ b) = (a \circ b)(1 + \delta). This one rule is the basis of all rounding-error analysis (Rounding Error and Its Propagation).

Decimal fractions such as 0.10.1 are infinite in binary, so they are rounded on input:

python · try it yourself
from decimal import Decimal

print(Decimal(0.1))
print(0.1 + 0.2 == 0.3, abs((0.1 + 0.2) - 0.3))
print(0.1.hex(), (0.5).hex())
0.1000000000000000055511151231257827021181583404541015625
False 5.551115123125783e-17
0x1.999999999999ap-4 0x1.0000000000000p-1

Special values

  • inf results from overflow (1e308 * 10) or division by zero in NumPy; -inf likewise.
  • nan (not a number) results from undefined operations such as inf - inf or 0/0; it is not equal to anything, even itself, so test with np.isnan.
  • Subnormal numbers fill the gap between 00 and 2−10222^{-1022} with reduced precision, so underflow is gradual.
python · try it yourself
import numpy as np

with np.errstate(all="ignore"):
    a = np.array([1e308]) * 10
    b = np.array([0.0]) / 0.0
print(a, b, b == b, np.isnan(b))
print("smallest subnormal:", np.nextafter(0.0, 1.0))
print("5e-324 / 2 =", 5e-324 / 2)
[inf] [nan] [False] [ True]
smallest subnormal: 5e-324
5e-324 / 2 = 0.0

In software

  • float32 (single precision, ϵ≈1.2×10−7\epsilon \approx 1.2 \times 10^{-7}) halves memory and is standard on GPUs; deep learning even uses float16 and bfloat16 (8 bits of precision). Numerical algorithms that are fine in double may fail in single precision.
  • Never compare floats with == after computation; use math.isclose or np.isclose with a tolerance suited to the problem.
  • Money should be stored as integers (cents) or decimal.Decimal, not binary floats.

Exercises

Exercise 1: Spacing

What is the gap between consecutive doubles in [1024,2048)[1024, 2048)?

Answer

2−42≈2.27×10−132^{-42} \approx 2.27 \times 10^{-13}.

Solution

In [210,211)[2^{10}, 2^{11}) the gap is 210⋅2−52=2−422^{10} \cdot 2^{-52} = 2^{-42}.

Exercise 2: Representable integers

Is 253+22^{53} + 2 exactly representable as a double? And 253+12^{53} + 1?

Answer

253+22^{53} + 2 yes; 253+12^{53} + 1 no.

Solution

In [253,254)[2^{53}, 2^{54}) the spacing is 253−52=22^{53 - 52} = 2, so only even integers are representable there.

Exercise 3: Find epsilon by halving (code)

Starting from e = 1.0, halve e while 1.0 + e / 2 > 1.0. Which value do you end with?

Answer

2−52≈2.220446049250313×10−162^{-52} \approx 2.220446049250313 \times 10^{-16}.

Solution
python · try it yourself
e = 1.0
count = 0
while 1.0 + e / 2 > 1.0:
    e /= 2
    count += 1
print(e, count, e == 2.0**-52)
2.220446049250313e-16 52 True

The loop stops after 52 halvings. (1 + 2**-53 rounds to 1 because it is exactly halfway and ties round to the even neighbour.)

Test yourself

Machine epsilon for double precision is about…

  1. 10−810^{-8}
  2. 2.2×10−162.2 \times 10^{-16}
  3. 10−30810^{-308}
  4. 10−3210^{-32}

How does the absolute gap between consecutive floats change as numbers grow?

  1. It stays constant
  2. It grows in proportion to the magnitude
  3. It shrinks
  4. It is random

Which test correctly detects a NaN x?

  1. x == np.nan
  2. np.isnan(x)
  3. x is None
  4. x == 0