Lesson 5 of 9 · 27m
Floating Point Is a Measurement System, Not Exact Arithmetic
Build the IEEE-754 mental model: rounding error, cancellation, NaN propagation, type precision, conditioning, and tests that tolerate reality without hiding defects.
New scientific programmers often treat floating point as a defective version of real arithmetic. Senior practitioners treat it as a measurement system: finite precision, controlled rounding, known edge values, and error that must be budgeted. Julia exposes IEEE behavior honestly. That is good news, but it means the language will not save you from unstable mathematics.
Every Float64 carries finite precision
0.1 + 0.2 == 0.3 # false
eps(1.0) # 2.220446049250313e-16Float64 has about 15-17 decimal digits of precision. eps(x) gives the spacing to the next representable value near x. The spacing grows with magnitude, so absolute tolerance that works near one is meaningless near one million.
Use isapprox(0.1 + 0.2, 0.3) or the Unicode ≈ operator in tests. Better yet, assert a scientifically meaningful tolerance based on input uncertainty and conditioning.
Cancellation is where digits disappear
Subtraction of nearly equal numbers destroys significant digits:
x = 1.0000000000001
bad = (x - 1.0) * 1e13If both inputs already carry rounding error, their difference may be mostly noise. Rewrite formulas to avoid cancellation when possible: use log1p(x) for log(1 + x) near zero and expm1(x) for exp(x) - 1 near zero.
log1p(1e-12) and expm1(1e-12) exist because numerical analysts already hit the wall you are about to hit.
NaN and Inf are diagnostics, not decoration
IEEE arithmetic defines how exceptional values propagate:
0.0 / 0.0 # NaN
1.0 / 0.0 # Inf
sqrt(-1.0) # DomainError for real inputNaN is infective: comparisons are false and most arithmetic stays NaN. Do not silently filter NaNs until you understand whether they represent impossible input, instability, missing observations, or a solver failure. Those are different incidents with different owners.
Precision is a performance and correctness decision
Float32 halves memory bandwidth and can be much faster on GPUs, but it carries about 7 decimal digits. BigFloat gives arbitrary precision at a large runtime cost. Mixed precision can be excellent when the unstable part is small and corrections accumulate in higher precision; randomly converting everything to BigFloat usually means nobody analyzed the algorithm.
Conditioning is a property of the problem
A well-implemented algorithm cannot fix an ill-conditioned problem. If tiny input perturbations legitimately cause huge output changes, your uncertainty is mathematical before it is computational. In linear algebra, inspect condition numbers; in iterative solvers, inspect residuals rather than only iteration counts.
using LinearAlgebra
A = [1 / (i + j - 1) for i in 1:8, j in 1:8] # Hilbert matrix
cond(A)Field notes
- Never use exact equality for ordinary floating-point results unless the operation is bit-exact by design.
isapproxtolerances are not scientific justification; derive tolerances from units, input uncertainty, and conditioning where possible.- Sum positive and negative terms carefully; pairwise and compensated summation matter most under cancellation.
NaNin output is information. Dropping it before diagnosis destroys evidence.- Lower precision can be correct engineering when measurements are noisy anyway, but verify error on representative data.
Was this lesson helpful?