liveBook · Manning

IEEE 754

As explained in JavaScript in Depth

The 2019 revision of the IEEE standard that specifies floating-point arithmetic formats and operations.

Why does 0.1 + 0.2 differ from 0.3?

In JavaScript, `0.1 + 0.2` prints `0.30000000000000004`, so `0.1 + 0.2 === 0.3` is false. This is a consequence of Number’s floating-point representation, not a broken addition operator. section 3.0

ECMAScript defines Number as a 64-bit double-precision floating-point value conforming to IEEE 754-2019, the 2019 revision of the Standard for Floating-Point Arithmetic. The standard supplies the rules behind the numeric format; ECMAScript specifies the relevant behavior for JavaScript. BigInt is a separate numeric type for integers that exceed Number’s reliable integer range. section 3.1 section 3.0

A decimal literal must be interpreted, converted to binary, and encoded into bytes. In the double-precision layout, one bit holds the sign, eleven hold the exponent, and 52 hold the significand. For a normalized value, the exponent sets the scale while the significand supplies the binary digits of its precision. This is different from ordinary integer binary encoding or two’s complement. 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.

Use the 64-bit layout to locate the sign, exponent, and significand before tracing a calculation. section 3.1.1

The decimal fractions 0.1 and 0.2 require repeating binary digits, but the significand has finite space. Their stored values are therefore approximations. To add them, align their exponents, add the significands, and encode the result at the format’s available precision. The resulting Number need not equal the Number obtained by converting the decimal literal `0.3`. section 3.1.1

Practical consequences:

  • Do not assume decimal-looking fractions compare exactly after arithmetic. A tolerance comparison can be useful; a fixed `Number.EPSILON` threshold and a magnitude-scaled threshold are both illustrated, rather than one universal test. section 3.1.1
  • Do not assume every large integer is distinguishable as a Number: counting past 9,007,199,254,740,991 becomes unreliable. Consider BigInt when that integer range matters. section 3.0 section 3.1.1

How JavaScript in Depth covers it

How deep it goes
Works through bit layout and binary addition, while leaving the full IEEE 754 standard outside its scope.
Examples
Traces `0.1 + 0.2`, binary significands, tolerance functions, and large adjacent integer literals.
What it assumes
Builds binary place values from bits and bytes rather than assuming familiarity with the IEEE standard.
What you can do after
Diagnose/debug floating-point equality surprises and recognize when Number cannot reliably distinguish large integers.
How it connects
Connects floating-point representation to ECMAScript Number, binary encoding, BigInt, and precision-aware comparisons.

Also explained in

Compare how 2 books explain IEEE 754 →

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.