liveBook · Manning

Floating-point arithmetic

As explained in JavaScript in Depth

An arithmetic operation performed using floating-point numeric representations.

A sum that misses its target

In JavaScript, `0.1 + 0.2` evaluates to `0.30000000000000004`, so `0.1 + 0.2 === 0.3` is false. The result follows from the numeric representation; it is not a JavaScript arithmetic bug. section 3.0

console.log(0.1 + 0.2);  // 0.30000000000000004
console.log(0.3);        // 0.3
console.log(0.1 + 0.2 === 0.3);  // false

Run the three expressions together: the computed sum, the literal `0.3`, and their strict comparison expose the difference. section 3.0

JavaScript `Number` uses 64-bit IEEE 754 Binary64. Its layout divides the bits among a sign, an 11-bit exponent, and a 52-bit significand field. The exponent gives the number its scale; the significand supplies its significant binary digits. That finite field cannot hold every fractional value. In particular, `0.1` and `0.2` have repeating base-2 representations, so their stored values are approximations. section 3.1.1 section 3.5

Figure 3.2 The “double-precision” floating point layout divides the 64 available bits into distinct components, including a sign-bit, eleven exponent bits, and 52 bits for the significand.

The bit layout makes the trade-off visible: bits used to encode scale are not unlimited digits of precision. section 3.1.1

To add the encoded values, align their exponents, add the significands in binary, then fit the result back into the available precision. The rounded result for `0.1 + 0.2` differs from the stored value of the literal `0.3`. Magnitude matters too: Binary64 provides 53 bits of accuracy, not exact representation of every integer across its full range. `Number.MAX_SAFE_INTEGER` is 2^53 − 1, or 9,007,199,254,740,991; beyond it, not every successive integer remains distinguishable. section 3.1.1 section 3.5

Choose a comparison or numeric type according to the calculation:

  • For results expected to be close rather than identical, compare their absolute difference with a tolerance. `Number.EPSILON` is useful near its reference scale; a tolerance scaled to the operands is another illustrated approach. section 3.1.1
  • Use `BigInt` when exact large integers are required, accepting its performance cost and the prohibition on mixing it with `Number` in arithmetic. section 3.5
  • Do not treat Binary64 as exact decimal arithmetic for financial amounts. A proposed `Decimal` type is presented as a future base-10 alternative, not an available replacement. section 3.0 section 3.5

The representation specified for `Number` does not dictate every runtime storage decision. V8 can keep small integers as SMIs instead of processing them as full floating-point values; values outside that optimized case and mixed numeric arrays can lead to slower handling. Integer counters and consistent array contents may help hot paths, but measure before optimizing. This is V8 behavior, not a promise about every JavaScript engine. section 3.4 section 3.4.3

How JavaScript in Depth covers it

How deep it goes
Works through Binary64 fields, approximated decimal fractions, exponent alignment, and rounding, then distinguishes V8 storage optimizations.
Examples
Uses `0.1 + 0.2 === 0.3`, binary significand addition, tolerance functions, and V8-oriented price-array benchmarks.
What it assumes
Builds binary place values from bits before using exponents and significands to explain fractions.
What you can do after
Diagnose/debug with it when decimal-looking sums differ, select approximate comparisons, and recognize when large integers need `BigInt`.
How it connects
Connects IEEE 754 Binary64 to binary encoding, `Number.EPSILON`, safe integers, `BigInt`, and V8 SMIs.

Also explained in

Compare how 21 books explain floating-point arithmetic →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of JavaScript in Depth. The section links point to the parts of the book it draws on.