Mathematical Foundations

VIII.2 · Numerical Computing

Why can displayed decimals hide a different sum?

Compare exact intended decimal values with exact stored binary rationals. Split the error of addition into operand representation and the arithmetic rounding that follows.

Compare intended decimals with stored arithmetic

Interchange format (software rounding)
Operation

Sign (s)

0

Exponent (11 bits)

01111111101

Fraction (52 bits; normal values have an implicit leading 1)

0011001100110011001100110011001100110011001100110100

binary64: normal, sign 0; Bit model; native JavaScript arithmetic uses binary64. Direct nearest rounding, ties to even; all four formats retain subnormals. A formatted decimal is distinct from the bits and exact rational.

Exact intended sum
3/10
Exact sum of rounded operands
10808639105689191/36028797018963968
Exact stored sum
1351079888211149/4503599627370496
Formatted computed sum
0.30000000000000004
Operand representation contribution
3/180143985094819840
Addition evaluation contribution
1/36028797018963968
Total signed answer error
1/22517998136852480

= exact rationals or classifications · ≈ a formatted numerical display. Exceptional values retain their stated meaning.

x=(−1)s(1+F 2−p) 2E−b,0<E<2e−1x=(-1)^s(1+F\,2^{-p})\,2^{E-b},\quad 0<E<2^e-1
xsub=(−1)sF 21−b−p,E=0x_{\mathrm{sub}}=(-1)^sF\,2^{1-b-p},\quad E=0
Ea=∣x^−x∣,Er=Ea/∣x∣ (x≠0)E_a=|\widehat x-x|,\quad E_r=E_a/|x|\ (x\ne0)
Adjacent representable values; binade boundaries can have asymmetric gaps.
neighborexact valueformatted display
previous5404319552844595/18014398509481984≈ 0.29999999999999999
stored1351079888211149/4503599627370496≈ 0.30000000000000004
next5404319552844597/18014398509481984≈ 0.3000000000000001

Round the first decimal

Direct conversion to the chosen format.

Inspect the recorded values\text{Inspect the recorded values}
Actual intermediate values; = exact rationals, ≈ binary64 displays.
quantityvalue
intended1/10
stored3602879701896397/36028797018963968

Exact decimal text, at most 420 characters; exponent in [-400,400].

Used in sum mode; rounds independently before addition.

Replay two exact decimal conversions and their rounded addition; compare representation, evaluation and total answer error.

Intuition: two opportunities to round

Typing 0.1 asks a binary format to represent an intended real value. Addition then operates on the represented operands, and its exact result may need another rounding. These are separate events. The explorer begins with 0.1+0.2 and retains both in its replay.

Decimal formatting is a third, presentation-only step. A formatter may print a short decimal that round-trips to the stored number, or a fixed number of digits that conceals a difference. Changing display precision does not change the bits. Inspect the exact rational and bit fields when two displays look equal.

Definition: signed, absolute and relative error

For intended x and finite computed x^\widehat x, signed error is x^−x\widehat x-x, absolute error is ∣x^−x∣|\widehat x-x|, and relative error is

∣x^−x∣∣x∣,x≠0.\frac{|\widehat x-x|}{|x|},\qquad x\ne0.

Relative error is undefined at intended zero. ULP error uses a specified representable spacing and differs from relative error; spacing can change at a binade boundary. Infinity and NaN have no finite rational error here. They remain explicit exceptional results.

For addition let a and b be the intended rational decimals, a^\widehat a and b^\widehat b the rounded operands, and s^\widehat s the rounded operation result. The model retains the exact sum of stored operands before rounding it.

Mechanics: three exact differences

Choose single-decimal mode to inspect one conversion, or sum mode to inspect two independent conversions and one operation. In sum mode the inspector reports:

Erepr=(a^+b^)−(a+b),E_{\mathrm{repr}}=(\widehat a+\widehat b)-(a+b), Eeval=s^−(a^+b^),E_{\mathrm{eval}}=\widehat s-(\widehat a+\widehat b), Etotal=s^−(a+b).E_{\mathrm{total}}=\widehat s-(a+b).

The bit board displays the actual stored result. Formats are software-emulated with the same direct nearest/ties-even conversion policy as the representation chapter. Every basic arithmetic operation is not exact merely because these particular operands are representable.

Derivation: how the contributions add

Add and subtract the exact stored-operand sum:

s^−(a+b)=[s^−(a^+b^)]+[(a^+b^)−(a+b)].\widehat s-(a+b)=\bigl[\widehat s-(\widehat a+\widehat b)\bigr]+\bigl[(\widehat a+\widehat b)-(a+b)\bigr].

This identity is exact in rational arithmetic and does not assume the operation was exact. Correct rounding bounds the local operation error by half a local lattice gap in ordinary finite cases; it does not bound every longer calculation by the same single error. Overflow and representability boundaries need their own interpretation.

A decimal terminates in binary precisely when its reduced denominator is a power of two. The rational 1/10 has a factor of five in its denominator, so its binary expansion does not terminate. Finite binary storage must select an approximation.

Worked example: 0.1 plus 0.2

The stored binary64 values are

a^=360287970189639736028797018963968,b^=360287970189639718014398509481984.\widehat a=\frac{3602879701896397}{36028797018963968},\qquad\widehat b=\frac{3602879701896397}{18014398509481984}.

The intended sum is 3/103/10. The exact stored-operand sum lies halfway between two binary64 values at that scale, and ties-to-even selects the upper one:

s^=13510798882111494503599627370496.\widehat s=\frac{1351079888211149}{4503599627370496}.

Its common short decimal display is 0.30000000000000004. The representation contribution is 3/1801439850948198403/180143985094819840, the evaluation contribution is 1/360287970189639681/36028797018963968, and their sum is 1/225179981368524801/22517998136852480. Replay inspects those three exact quantities instead of inferring error from rounded text.

Single-decimal mode also shows 9007199254740993, which rounds to 9007199254740992 in binary64. Large integers eventually exceed the consecutive-integer precision range; being an integer does not guarantee representability.

Worked example: Compare intended decimals with stored arithmetic

Sign (s)

0

Exponent (11 bits)

01111111101

Fraction (52 bits; normal values have an implicit leading 1)

0011001100110011001100110011001100110011001100110100

binary64: normal, sign 0; Bit model; native JavaScript arithmetic uses binary64. Direct nearest rounding, ties to even; all four formats retain subnormals. A formatted decimal is distinct from the bits and exact rational.

Exact intended sum
3/10
Exact sum of rounded operands
10808639105689191/36028797018963968
Exact stored sum
1351079888211149/4503599627370496
Formatted computed sum
0.30000000000000004
Operand representation contribution
3/180143985094819840
Addition evaluation contribution
1/36028797018963968
Total signed answer error
1/22517998136852480

= exact rationals or classifications · ≈ a formatted numerical display. Exceptional values retain their stated meaning.

x=(−1)s(1+F 2−p) 2E−b,0<E<2e−1x=(-1)^s(1+F\,2^{-p})\,2^{E-b},\quad 0<E<2^e-1
xsub=(−1)sF 21−b−p,E=0x_{\mathrm{sub}}=(-1)^sF\,2^{1-b-p},\quad E=0
Ea=∣x^−x∣,Er=Ea/∣x∣ (x≠0)E_a=|\widehat x-x|,\quad E_r=E_a/|x|\ (x\ne0)
Adjacent representable values; binade boundaries can have asymmetric gaps.
neighborexact valueformatted display
previous5404319552844595/18014398509481984≈ 0.29999999999999999
stored1351079888211149/4503599627370496≈ 0.30000000000000004
next5404319552844597/18014398509481984≈ 0.3000000000000001

Round the first decimal

Direct conversion to the chosen format.

Inspect the recorded values\text{Inspect the recorded values}
Actual intermediate values; = exact rationals, ≈ binary64 displays.
quantityvalue
intended1/10
stored3602879701896397/36028797018963968

Select the recorded intermediate values. Previous, next and direct replay navigation preserve immutable numerical history.

Executable reference code

Fraction exposes the exact represented operands and answer, and verifies the decomposition independently. The NumPy example repeats the operation in three concrete dtypes. Its printed result, exact rational and error are distinct outputs; the low-precision arrays are real NumPy dtypes while this browser explorer performs explicitly emulated rounding.

code/python/rounding_representation_error.py
"""Representation error and addition error as independent exact fractions."""
from fractions import Fraction
from floating_point_representation import convert, exact
if __name__ == '__main__':
    a, b = Fraction('0.1'), Fraction('0.2')
    stored_a = exact(convert('0.1', 'binary64'), 'binary64')
    stored_b = exact(convert('0.2', 'binary64'), 'binary64')
    answer = Fraction.from_float(0.1 + 0.2)
    representation = stored_a + stored_b - (a+b)
    evaluation = answer - (stored_a+stored_b)
    assert representation + evaluation == answer - Fraction(3, 10)
    assert answer == Fraction(1351079888211149, 4503599627370496)
    print('intended', a+b, 'stored', answer, 'representation', representation, 'evaluation', evaluation)
code/python/rounding_representation_error_numpy.py
import numpy as np
from fractions import Fraction
if __name__ == '__main__':
    for dtype in (np.float16, np.float32, np.float64):
        a, b = dtype('0.1'), dtype('0.2'); result = dtype(a+b)
        stored = Fraction.from_float(float(result))
        print(dtype.__name__, 'display', result, 'exact stored', stored, 'error', stored-Fraction(3, 10))
        assert stored != Fraction(3, 10)
    assert float(np.float64('9007199254740993')) == 9007199254740992

Explore: predictions near a boundary

Compare 0.1 alone, its sum with 0.2, and the large-integer preset. Predict whether the error is conversion error, operation error, or both before opening the replay. Switch the sum between formats and compare exact stored sums rather than assuming more range implies more precision.

Use FP16 at the halfway preset, then just above halfway. Change only the last decimal digits and observe the stored bit change. Try zero as the intended input: absolute error still has a meaning, but relative error cannot divide by zero.

Common mistakes and counterexamples

A stored decimal display and the intended decimal are different mathematical objects. Two computations can share a rounded display but differ in bits; the same bits can be formatted in many ways. Adding finite stored operands can round even when each conversion has already completed.

Subtracting displayed decimal strings is not a trustworthy measurement of representation error. The exact rational difference is the measurement in this explorer. Likewise, ordinary equality is appropriate for exact bits and discrete counts, while approximate-answer tests need a justified tolerance.

Numerical considerations

The sum model performs exact rational addition of encoded operands, then one direct rounding to the chosen format. Binary64 parity checks include the actual JavaScript 0.1+0.2 result. Inputs that already round to infinity cannot enter the finite-operand sum experiment; the range explorer explains that failure separately.

The direct conversion avoids an intermediate binary format. Halfway rounding examples should cover more than ordinary small decimals: include subnormal carries, signed zero, large integers and overflow thresholds. Error budgets apply to particular numerical evaluations, never to bit or rational comparisons.

Engineering and ML connections

Dataset decimals, text-based parameters and binary tensor entries can refer to different values. An error record should state the intended input, its storage conversion and the operation sequence. This is especially useful when low-precision storage feeds higher-precision accumulation: a more accurate accumulator cannot reconstruct operand digits already discarded by conversion.

Inspect the corresponding representation and error policy before interpreting a computed value.

Exercises

Predict first, then calculate or test the stated claim. Every answer includes corrective feedback and a worked solution.

calculation

Exactly binary

Give the reduced denominator of the exact decimal 0.125.

calculation

Absolute and relative error

Intended x=10, computed answer=10.25. Give absolute error, then relative error.

calculation

Two error contributions

Operand representation contributes 1/1000 and evaluation contributes −1/2000. Give total signed error.

explanation

Displayed text

A formatter prints 0.1. What follows?

Choose one answer

explanation

Relative error at zero

Intended x=0 and the numerical answer is nonzero. Which error can be used without division by zero?

Choose one answer

Summary and checkpoints

Separate intended decimals, stored bits and formatted displays. Reproduce the exact error decomposition for 0.1+0.2, explain undefined relative error at zero, and identify a large integer that cannot be stored exactly. A basic operation can contribute additional rounding after representation.

The map of the book

97 of 97 chapters written · eight Parts

demo Foundation demo Try the live vector example Open as a page

Part I

Mathematical Language

The notation and mental objects everything else uses.

9 of 9 chapters written

  1. I.1 Scalars, quantities, units, powers and roots available
  2. I.2 Coordinates and points available
  3. I.3 Vectors: components, magnitude, and direction reference chapter available
  4. I.4 Algebraic expressions, equations and inequalities available
  5. I.5 Functions as input → transformation → output available
  6. I.6 Essential function families available
  7. I.7 Sets, intervals, and relations available
  8. I.8 Indexed quantities and summation available
  9. I.9 The notation map for ML available

Part II

Linear Algebra

Matrices as transformations: the strongest foundation in the book.

19 of 19 chapters written

  1. II.1 Vector addition and subtraction available
  2. II.2 Scalar multiplication and vector scaling available
  3. II.3 Norms, magnitude, and normalization available
  4. II.4 Dot product, angle, and orthogonality available
  5. II.5 Projection and distance to a direction available
  6. II.6 Linear combinations and span available
  7. II.7 Linear independence, basis, and coordinates available
  8. II.8 Matrices as linear transformations available
  9. II.9 Matrix × vector available
  10. II.10 Matrix × matrix as composition available
  11. II.11 Transpose and orthogonality available
  12. II.12 Determinant: orientation and area/volume scaling available
  13. II.13 Linear systems as intersecting constraints available
  14. II.14 Gaussian elimination and row operations available
  15. II.15 Rank and invertibility available
  16. II.16 Inverse matrices available
  17. II.17 Eigenvalues and eigenvectors available
  18. II.18 Orthogonal bases and least-squares geometry available
  19. II.19 Singular Value Decomposition available

Part III

Calculus

Change, local approximation, and sensitivity made visible.

15 of 15 chapters written

  1. III.1 Limits through approach available
  2. III.2 Continuity and local behavior available
  3. III.3 Derivative as secant → tangent available
  4. III.4 Derivative as rate of change and local linear model available
  5. III.5 Derivative rules available
  6. III.6 Chain rule available
  7. III.7 Multivariable functions and partial derivatives available
  8. III.8 Gradient and directional derivatives available
  9. III.9 Jacobian available
  10. III.10 Computational graphs and automatic differentiation available
  11. III.11 Hessian and curvature available
  12. III.12 Integrals as accumulation available
  13. III.13 Fundamental derivative/integral connection available
  14. III.14 Numerical differentiation available
  15. III.15 Numerical integration available

Part IV

Probability

Uncertainty through sample spaces, evidence, and repetition.

16 of 16 chapters written

  1. IV.1 Random experiments, outcomes, and sample spaces available
  2. IV.2 Events and probability mass available
  3. IV.3 Counting and finite probability where needed available
  4. IV.4 Conditional probability as restricted sample space available
  5. IV.5 Independence available
  6. IV.6 Bayes' theorem as probability-mass update available
  7. IV.7 Random variables available
  8. IV.8 PMF, PDF, and CDF available
  9. IV.9 Bernoulli and Binomial distributions available
  10. IV.10 Uniform and Gaussian distributions available
  11. IV.11 Expectation available
  12. IV.12 Variance and standard deviation available
  13. IV.13 Joint, marginal, and conditional distributions available
  14. IV.14 Covariance and correlation available
  15. IV.15 Law of Large Numbers available
  16. IV.16 Central Limit Theorem available

Part V

Statistics

From observed data to estimates, intervals, and likelihood.

10 of 10 chapters written

  1. V.1 Data summaries: mean, median, mode available
  2. V.2 Spread: variance, standard deviation, robust interpretation available
  3. V.3 Population, sample, and sampling variability available
  4. V.4 Estimators and estimation available
  5. V.5 Confidence intervals through repeated sampling available
  6. V.6 Covariance matrices and multivariate spread available
  7. V.7 Regression fundamentals available
  8. V.8 Likelihood available
  9. V.9 Maximum Likelihood Estimation available
  10. V.10 Maximum A Posteriori estimation available

Part VI

Optimization

Learning as repeated parameter updates governed by an objective.

10 of 10 chapters written

  1. VI.1 Objective functions and optimization landscapes available
  2. VI.2 Local vs global optima and stationary points available
  3. VI.3 Convexity available
  4. VI.4 Gradient descent flagship chapter available
  5. VI.5 Learning rate and convergence behavior available
  6. VI.6 Momentum available
  7. VI.7 Stochastic and mini-batch gradient descent available
  8. VI.8 Adam intuition available
  9. VI.9 Constraints and projected/penalized intuition available
  10. VI.10 Numerical stopping and optimization diagnostics available

Part VII

Information Theory

Logarithms, entropy, and the loss behind token prediction.

8 of 8 chapters written

  1. VII.1 Logarithms and log probabilities in computation available
  2. VII.2 Self-information available
  3. VII.3 Entropy available
  4. VII.4 Cross entropy and negative log-likelihood available
  5. VII.5 KL divergence available
  6. VII.6 Logits and softmax available
  7. VII.7 Numerical stability: log-sum-exp and stable softmax available
  8. VII.8 From token logits to language-model loss available

Part VIII

Numerical Computing

Why equivalent expressions behave differently on real machines.

10 of 10 chapters written

  1. VIII.1 Floating-point representation available
  2. VIII.2 Rounding and representation error available
  3. VIII.3 Error propagation and conditioning available
  4. VIII.4 Catastrophic cancellation available
  5. VIII.5 Overflow and underflow available
  6. VIII.6 Stable algorithms available
  7. VIII.7 Numerical linear algebra realities available
  8. VIII.8 Sparse matrices and sparse structure available
  9. VIII.9 Vectorized computation available
  10. VIII.10 Reproducibility, random seeds, and nondeterminism available