4. IEEE 754 Floating Point

The standard that rules every number in your program

Every number in JavaScript is an IEEE 754 double-precision float. So is every float in Python, every double in C, every number in Lua, every value from Julia's default numeric type. The format was standardised in 1985 and has been dominant ever since. Understanding it explains why 0.1 + 0.2 !== 0.3, why comparing floats with == is dangerous, why dividing by zero gives Infinity instead of crashing, and dozens of other surprises that bite programmers who treat floating-point numbers as if they were real numbers.

The Three Fields

A 32-bit (float32) IEEE 754 number is split into three parts — one sign bit, eight exponent bits, and twenty-three mantissa (significand) bits.

Diagram

32-bit Float Layout

S 1 bit 31 Exponent 8 bits 30 23 Mantissa (significand) 23 bits 22 0
32-bit Float Layout

A 64-bit (float64) number follows the same layout but with an 11-bit exponent and 52-bit mantissa:

Diagram

64-bit Double Layout

S 1 b 63 Exponent 11 bits 62 52 Mantissa (significand) 52 bits 51 0
64-bit Double Layout

The sign bit is dead simple: 0 means positive, 1 means negative. The exponent and mantissa together encode the magnitude — but they interact in a subtle way that produces the format's power and its quirks.

Bias Encoding

Two's complement would be the obvious choice for the exponent — it's how we store signed integers everywhere else. IEEE 754 instead uses bias encoding (also called excess-N or offset binary).

The stored exponent field is an unsigned integer. To get the actual exponent, you subtract a fixed bias:

  • float32: bias = 127, so stored value 127 means exponent 0, stored value 130 means exponent +3, stored value 124 means exponent −3.
  • float64: bias = 1023, so stored value 1023 means exponent 0.

Why not two's complement? Because bias encoding has a crucial property: floats can be compared as unsigned integers. The largest float has the largest bit pattern. Sorting floating-point numbers in hardware or comparing them with integer logic becomes trivial — and that matters enormously for CPU design, SIMD instructions, and lookup-table tricks.

Bias encoding for float32 (bias = 127)

Stored bits (decimal)Actual exponentMultiplier
0−126 (special: denormal)—
1−1262−126 ≈ 1.18 × 10−38
127020 = 1
130+323 = 8
254+1272127 ≈ 1.70 × 1038
255+128 (special: Inf/NaN)—

The Implicit Leading 1

Scientific notation writes numbers as 1.something × 10^n. IEEE 754 does the same thing in binary. Every normalized float has the form:

The normalized form

value = (−1)sign × 1.mantissa × 2exponent − bias

The leading 1 before the binary point is always there for a normalized number — so there is no reason to store it. The mantissa field stores only the fractional part after the binary point. This free bit is called the implicit leading 1 or hidden bit.

The payoff is real: a 23-bit mantissa field gives you 24 bits of effective precision. For float32 that is about 7 significant decimal digits. For float64 (52-bit mantissa) you get 53 effective bits, or about 15–17 significant decimal digits.

Decoding the float32 for the value 0.75

Bits:    0 01111110 10000000000000000000000
         │ │        │
         │ │        └── mantissa = 0.1 (binary) = 0.5
         │ └─────────── exponent stored = 126, actual = 126 − 127 = −1
         └───────────── sign = 0 (positive)

value = (−1)^0 × 1.1 (binary) × 2^−1 = 1.5 × 0.5 = 0.75 ✓

Special Values

The all-zeros and all-ones exponent patterns are reserved. They encode four categories of special values:

Diagram

Special Value Map (float32)

Sign Exponent (stored) Mantissa Value 0 0000 0000 000...000 +0 1 0000 0000 000...000 −0 s 0000 0000 ≠ 000...000 Denormal 0 1111 1111 000...000 +Infinity 1 1111 1111 000...000 −Infinity s 1111 1111 ≠ 000...000 NaN
Special Value Map (float32)

+0 and −0 are distinct bit patterns but compare equal under ==. They differ only when you divide: 1 / +0 = +Infinity, 1 / -0 = -Infinity.

Denormals (subnormals) fill the gap near zero. For a normal float the implicit leading bit is 1, giving a minimum magnitude of about 1.18 × 10⁻³⁸ for float32. Without denormals there would be an abrupt jump to zero there — a cliff. Denormals use exponent 0 and drop the implicit leading 1, using the form 0.mantissa × 2^−126. This produces gradual underflow: very small numbers lose precision bit by bit instead of snapping to zero all at once.

NaN (Not a Number) results from operations like 0/0, Inf − Inf, or sqrt(−1). There are actually millions of distinct NaN bit patterns (any non-zero mantissa with all-ones exponent) but they all behave identically: NaN propagates through arithmetic, and NaN == NaN is false — the only value that does not equal itself.

Infinity propagates too: Inf + 1 = Inf, 1 / Inf = 0.

Float Density

Floating-point numbers are not uniformly distributed on the number line. Half of all representable floats lie in the interval [−1, +1]. There are as many floats between 1 and 2 as there are between 2 and 4, between 4 and 8, and so on. Each power-of-two interval contains the same count of values. Near zero, floats are packed densely; near the maximum, they are kilometres apart.

Diagram

Float Density on the Number Line

0 0.01 0.1 10 MAX denser near zero ←→ sparser at large magnitudes
Float Density on the Number Line

The practical implication: when two large floats are subtracted and their values are close, you lose significant bits — the result has far fewer accurate digits than either input. This is called catastrophic cancellation and is one of the most common numerical pitfalls.

Single vs Double Precision

float32 float64
Total bits 32 64
Sign bits 1 1
Exponent bits 8 11
Mantissa bits 23 52
Effective precision 24 bits (~7 decimal digits) 53 bits (~15–17 decimal digits)
Exponent bias 127 1023
Max value ~3.4 × 10³⁸ ~1.8 × 10³⁰⁸
Min normal ~1.2 × 10⁻³⁸ ~2.2 × 10⁻³⁰⁸
Epsilon (1 ULP at 1.0) ~1.19 × 10⁻⁷ ~2.22 × 10⁻¹⁶
Common use GPU, mobile, ML training General-purpose computing

JavaScript and Python's float are always float64. C/C++ float is float32; double is float64. GPUs default to float32 (with float16 becoming common for neural networks).

Why 0.1 Can't Be Represented Exactly

The number 0.1 in binary is a non-terminating repeating fraction — like 1/3 = 0.333... in decimal.

Converting 0.1 to binary

0.1 × 2 = 0.2  → bit 0
0.2 × 2 = 0.4  → bit 0
0.4 × 2 = 0.8  → bit 0
0.8 × 2 = 1.6  → bit 1, carry 0.6
0.6 × 2 = 1.2  → bit 1, carry 0.2  ← cycle starts again
0.2 × 2 = 0.4  → bit 0
0.4 × 2 = 0.8  → bit 0
0.8 × 2 = 1.6  → bit 1, carry 0.6
0.6 × 2 = 1.2  → bit 1, carry 0.2  ← cycle repeats

0.1 (decimal) = 0.0001100110011001100110011... (binary, repeating)

The float64 representation of 0.1 stores 52 mantissa bits of this repeating pattern and then truncates. The actual stored value is:

0.1000000000000000055511151231257827021181583404541015625

That tiny error is about 5.5 × 10⁻¹⁸. By itself it's harmless. But when you add 0.1 + 0.2, both values carry their own rounding errors, and the result (0.30000000000000004) differs from the nearest float64 to 0.3 by one ULP (unit in the last place).

The rule for float comparisons

Never compare floats with == unless you have a specific reason. Use an epsilon comparison: |a − b| < epsilon. Or, if you know the magnitude, compare within a few ULPs using integer-reinterpretation tricks.

The same issue affects any fraction whose denominator (in lowest terms) is not a power of two. 0.5 = 1/2, 0.25 = 1/4, 0.125 = 1/8 — these are exact. 0.1 = 1/10, 0.2 = 1/5, 0.3 = 3/10 — none of their denominators are powers of two, so none are exact in binary floating point.

Key Takeaways

  • IEEE 754 encodes a float as sign · exponent · mantissa. The sign is one bit; the exponent uses bias encoding; the mantissa stores the fractional part of a normalized significand.
  • The implicit leading 1 gives float32 24 bits of precision from a 23-bit field, and float64 53 bits from a 52-bit field.
  • Bias encoding (not two's complement) for the exponent allows floats to be sorted as unsigned integers — a hardware convenience that influenced the design.
  • Reserved exponent patterns produce +0, −0, +Inf, −Inf, NaN, and denormals — all with defined, portable behaviour.
  • Denormals provide gradual underflow near zero instead of a sudden snap to zero.
  • Most decimal fractions — including 0.1 — are non-terminating in binary and are stored as rounded approximations. Never use == to compare floats.
  • Float density is logarithmic: half of all representable floats lie between −1 and +1. Large floats have gaps of thousands between adjacent representable values.

Try It — IEEE 754 Explorer

Enter any decimal number and choose a precision. The widget converts it to the exact IEEE 754 bit pattern, lets you click any bit to toggle it, and shows the decoded value, representation error, and special-value detection.

Bit Pattern

Click any bit to toggle it and see the decoded value update live.

Decoded Breakdown

Statistics


With the bit-level structure of IEEE 754 in hand, the next step is understanding what happens when floats interact with each other — addition, multiplication, rounding modes, and the subtleties of floating-point arithmetic that every numerical software author must know.