IEEE 754双精度的工作原理

JavaScript数字是64位IEEE 754「双精度」浮点数。64位分为三个字段:

  • 符号位 (1 bit) — 0 for positive, 1 for negative
  • 指数 (11 bits) — stored with a bias of 1023. Raw value 0 means denormalized; 2047 means Infinity or NaN.
  • 尾数 (52 bits) — the fractional part. There's an implicit leading 1 (for normal numbers), so you get 53 bits of precision total.

The value is: ±1.mantissa × 2(exponent - 1023)

This is why 0.1 + 0.2 !== 0.3 — 0.1 and 0.2 can't be represented exactly in binary, so the sum accumulates a tiny rounding error. Try clicking "0.1 + 0.2" and "0.3" to compare their bits.