跳到主要内容

IEEE 754

Sign
Exponent (11Bit)
Fraction (52Bit)
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
00000000000
0000000000000000000000000000000000000000000000000000
Binary:
1.0000000000000000000000000000000000000000000000000000×10(00000000000-1111111111)
Decimal:
1×2(0-1023)
Can be represented exactly in binary
Quick try:

IEEE 754 Double-Precision Floating Point

Languages like JavaScript and Python use the IEEE 754 standard for floating-point arithmetic. This leads to the classic precision question that shows up in interviews and real-world debugging alike:

0.1 + 0.2 == 0.30000000000000004

In JavaScript, there is only a single number type — no separate integer and float types. Even integers are stored as double-precision floats, which introduces integer precision limits such as MAX_SAFE_INTEGER:

> Number.MAX_SAFE_INTEGER
9007199254740991
> Number.MAX_SAFE_INTEGER + 1
9007199254740992
> Number.MAX_SAFE_INTEGER + 2
9007199254740992
> Number.MAX_SAFE_INTEGER + 3
9007199254740994
> Number.MAX_SAFE_INTEGER + 4
9007199254740996

Internal Representation

  1. A double-precision float is 64 bits, divided into three fields: sign (1 bit), exponent (11 bits), and mantissa (52 bits).
  2. Internally, the value is expressed in binary scientific notation. For example, 1536 is stored as 1.1 × 2¹⁰ — the mantissa stores .1 (the fractional part after the implicit leading 1) and the exponent stores 10.
  3. The exponent is stored with an exponent bias of 1023 (binary 01111111111), so the stored exponent = actual exponent + 1023.