Page 9: Floating-Point Representation (IEEE 754) and Numeric Precision Pitfalls
Using binary floating-point numbers directly for currency, invoices, or ledger balances leads to penny discrepancies and broken equality checks.
Almost every developer remembers the first time they typed `0.1 + 0.2` into a console and saw `0.30000000000000004` instead of `0.3`. This behavior is not a flaw specific to JavaScript—it occurs in C, C++, Java, Python, Go, and Rust because modern CPU floating-point units implement the IEEE 754 binary floating-point standard.
Just as the fraction $1/3$ cannot be written as a finite decimal in base-10 ($0.333333\dots$), the decimal fraction $1/10$ ($0.1$) cannot be written as a finite fraction in base-2 ($0.0001100110011\dots_2$). Because a 64-bit floating-point number has only 53 bits of significand precision, the repeating binary fraction must be rounded at bit 53. When two rounded approximations are added together, the tiny rounding residuals become visible.
9.1 Anatomy of a 64-Bit IEEE 754 Double-Precision Float
A 64-bit floating-point number (called `double` in C/Java and `number` in JavaScript) encodes values in binary scientific notation: $(-1)^{\text{sign}} \times 1.\text{mantissa} \times 2^{\text{exponent} - 1023}$.
Bit 63 stores the sign (`0` for positive, `1` for negative), bits 62–52 store the 11-bit biased exponent, and bits 51–0 store the 52-bit fraction (which together with an implicit leading `1` provides 53 bits of precision). Consequently, the largest exact integer representable without skipping odd numbers is $2^{53} - 1 = 9,007,199,254,740,991$ (`Number.MAX_SAFE_INTEGER`).
9.2 Representing Currency Safely with Minor Units (Cents/Paise)
For monetary calculations, never store dollars or rupees as floating-point decimals like `19.99`. Instead, store all monetary amounts as exact integers in the smallest currency unit (e.g., `1999` cents or paise) or use an arbitrary-precision decimal library.
Because integers up to 9 quadrillion (`Number.MAX_SAFE_INTEGER`) have zero rounding error in IEEE 754, adding, subtracting, and comparing integer cents is 100% exact.