liveBook · Manning

Floating-point arithmetic

As explained in Math for Web Design

A floating-point number is a finite-precision numerical value represented with a significand and an exponent, allowing fractional quantities and a wide range of magnitudes.

Floating-point arithmetic represents numbers with finite precision, allowing JavaScript to use one number type for integers, fractions, and very large or small values. JavaScript stores regular numbers as 64-bit IEEE 754 values, so some decimal fractions are approximations rather than exact values. section 2.2.1 section 2.3.1

The calculation that almost matches

The expression 0.1 + 0.2 === 0.3 returns false because 0.1 and 0.2 become rounded binary approximations before JavaScript adds them. For UI calculations such as spacing, animation timing, or alignment, compare values with a small tolerance: Math.abs((0.1 + 0.2) - 0.3) < Number.EPSILON. section 2.3 section 2.3.1

Precision becomes especially important when calculations drive interface behavior:

  • Round values when animation timing, visual snapping, spacing, or alignment must settle on predictable results. section 2.3.2
  • Format values for display with a specified number of decimal places instead of exposing internal approximations. section 2.3.2 section 2.9
  • Check integer boundaries with Number.isSafeInteger(); values beyond Number.MAX_SAFE_INTEGER and Number.MIN_SAFE_INTEGER are not reliably precise. section 2.2.1
0.1 + 0.2 === 0.3

Run the equality test, then compare its false result with the EPSILON-based tolerance check to see how finite precision affects JavaScript calculations. section 2.3 section 2.3.1

How Math for Web Design covers it

How deep it goes
Explains IEEE 754 storage, repeating binary fractions, rounding, safe integer limits, and tolerance checks without pursuing floating-point internals further.
Examples
Uses 0.1 + 0.2, unsafe integer increments, EPSILON comparisons, UI alignment, animation timing, and visual snapping.
What it assumes
Builds from JavaScript arithmetic and integer values while introducing binary representation and finite precision through concrete calculations.
What you can do after
Diagnose precision-sensitive UI calculations and apply rounding, formatting, safe-integer checks, or EPSILON-based comparisons.
How it connects
Connects floating-point arithmetic to JavaScript Number, IEEE 754, binary representation, Number.EPSILON, rounding, and BigInt.

Also explained in

Compare how 20 books explain floating-point arithmetic →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Math for Web Design. The section links point to the parts of the book it draws on.