JULIA / CHAPTER 11
Numeric representation and error budgets
AdvancedChoose comparisons from the meaning of the quantity.
By the end of this chapter
- Distinguish exact rationals from binary floating point
- Select absolute and relative tolerances intentionally
- Recognize that arithmetic order can affect rounding
The idea
Float64 stores a finite binary approximation of many decimal fractions. Arithmetic introduces rounding, so two mathematically equivalent computations need not produce identical bit patterns. Exact equality remains meaningful when identical representation is required, but measured or computed quantities often need a tolerance derived from their units and error budget. isapprox combines absolute and relative tolerances; choose them intentionally instead of hiding uncertainty behind an arbitrary large threshold.
Near zero, a relative tolerance alone may reject a small residual because there is no useful nonzero scale. An absolute tolerance can express an acceptable error in the measurement’s units. Rational values such as 1//10 represent this fraction exactly within the limits of their integer storage, but they are not a universal substitute for floating-point scientific computation. Also consider overflow, cancellation, and accumulation order.
println(0.1 + 0.2 == 0.3)
println(isapprox(0.1 + 0.2, 0.3))
println(1//10 + 2//10 == 3//10)false true true
The floating-point sum is close but not exactly equal to the stored 0.3. The rational expression represents the fractions exactly here.
Try it yourself
Specify an absolute tolerance for a near-zero sensor residual and explain the chosen units.
Use your own editor for this exercise. Runnable challenges on CodeQuest are available for JavaScript, Python, and SQL.
CHECK YOUR UNDERSTANDING