liveBook · Manning

Float64

As explained in JavaScript in Depth

The IEEE 754 double-precision binary floating-point format that represents real numbers using 64 bits.

Why does JavaScript print 0.30000000000000004 for 0.1 + 0.2? A Number behaves as an IEEE 754-2019 double-precision, 64-bit floating-point value. Decimal fractions such as 0.1 repeat in binary, so their stored values are approximations before the addition even begins. section 3.1 section 3.5

The 64 bits have three jobs: one bit records the sign, 11 encode an exponent with a bias of 1023, and 52 store significand bits. For the ordinary values illustrated here, an implicit leading 1 supplies another bit of precision. The exponent scales the significand by a power of two; it does not make decimal fractions exact. section 3.1.1

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.

Locate the sign, exponent, and significand fields before following a value through arithmetic. section 3.1.1

To add 0.1 and 0.2, first align their exponents, then add their binary significands and round the result back to the available precision. The rounded result differs from the stored approximation of 0.3. This is a representation effect, not a JavaScript-specific arithmetic exception. section 3.1.1 section 3.5

Table 3.3 Representing 0.1 and 0.2 as floating-point numbers
Value
Sign
Exponent
Significand
0.1
0
01111111011
1.1001100110011001100110011001100110011001100110011010
0.2
0
01111111100
1.1001100110011001100110011001100110011001100110011010

Compare the encoded exponents and repeating significand patterns of 0.1 and 0.2. section 3.1.1

Two practical boundaries follow from the finite significand:

  • Floating-point precision is about 53 binary digits, not a fixed number of decimal places. The largest safe integer is 2^53 − 1; above it, not every integer has a distinct exact representation. section 3.5
  • For results expected to be close rather than identical, compare their difference against a tolerance appropriate to their scale. Number.EPSILON can help construct one; it is not a universal tolerance for every magnitude. section 3.1.1

The language specifies Number behavior, not that every arithmetic operation must physically process a full floating-point value. V8 can keep small integers in an optimized SMI representation; changing numeric types in arrays or loops can lose that optimization. Treat the 64-bit format as the behavioral contract, and the runtime representation as an implementation choice. section 3.4 section 3.5

How JavaScript in Depth covers it

How deep it goes
Works through sign, biased exponent, significand, binary fraction addition, and rounding; distinguishes specified behavior from V8 storage optimizations.
Examples
Traces 0.1 + 0.2 through binary encoding and addition, then tests approximate equality and integers near 2^53.
What it assumes
Builds binary place value from decimal place value before introducing the floating-point fields.
What you can do after
Diagnose/debug rounding surprises and unsafe integers; choose scale-aware comparisons while recognizing runtime-specific integer optimizations.
How it connects
Connects IEEE 754, JavaScript Number, Number.EPSILON, safe integers, BigInt, and V8 SMI optimization.

Also explained in

Compare how 8 books explain float64 →

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.