IEEE 754 Floating Point: Mantissa, Exponent, Bias and Hidden Bit

From MediaWiki
Jump to navigation Jump to search

IEEE 754 Floating Point: Mantissa, Exponent, Bias and Hidden Bit

This page explains the IEEE 754 standard for floating-point numbers, which defines how real numbers are represented and calculated on modern CPUs.

Motivation

Fixed-point numbers have constant precision, which limits range and accuracy. For scientific and engineering calculations, numbers may vary from very large to very small. To handle this efficiently, computers use *floating-point representation*, based on the idea of *scientific notation*.

Scientific notation

In decimal scientific notation, a number is expressed as: a × 10ᵇ Example:

  • 1.344 × 10⁴ = 13 440
  • 2.342 × 10⁻⁵ = 0.00002342
  • 4.430 × 10⁰ = 4.430

All have 4 significant digits (mantissa) and a scaling factor (exponent). The same concept is applied in binary, replacing base 10 with base 2.

IEEE 754 structure

A floating-point number is stored as three parts:

Part                | Description
--------------------| ------------
Sign bit            | 0 for positive, 1 for negative
Exponent            | Scales the number by a power of 2 (with bias)
Mantissa (fraction) | Holds the significant digits of the number

Binary value = (−1)ˢ × (1 + mantissa) × 2^(exponent − bias)

Example: Single precision (32-bit) layout: `[Sign:1][Exponent:8][Mantissa:23]`

Bias and exponent encoding

The exponent is stored as an *unsigned integer with bias*. The bias centers the exponent range around zero.

For single precision (8-bit exponent):

  • Maximum exponent = 255
  • Bias = 127 (2⁷ − 1)

For double precision (11-bit exponent):

  • Maximum exponent = 2047
  • Bias = 1023 (2¹⁰ − 1)

Encoding example: Exponent = 2 → stored as 2 + 127 = 129 = 10000001₂.

Hidden bit and normalization

In normalized numbers, the mantissa is always between 1.0 and 2.0. Because the integer part is always 1, it is not stored — this is the *hidden bit*. This provides one extra bit of precision.

Exception: For very small numbers (denormalized), the hidden bit is 0.

Example: Representing 7.5

1. Convert to binary: 7.5 = 111.1₂ = 1.111₂ × 2² 2. Exponent = 2 + 127 = 129 → 10000001₂ 3. Mantissa (after decimal point): 11100000000000000000000 4. Sign = 0

Final bit pattern: 01000000 11110000 00000000 00000000 = 0x40F00000 (IEEE 754 single precision)

Example: Representing 0.5

0.5 = 1.0 × 2⁻¹ Exponent = −1 + 127 = 126 (01111110₂) Mantissa = 0 Sign = 0 Bit pattern: 0 01111110 00000000000000000000000 = 0x3F000000.

Example: 0x41BE6666

Interpretation:

  • Sign = 0 → positive
  • Exponent = 10000011₂ = 131 → 131 − 127 = 4
  • Mantissa = 1.01111100110011001100110₂ ≈ 1.4875

Value = +1.4875 × 2⁴ = +23.8.

Special values

Exponent pattern | Mantissa pattern | Meaning
---------------- | ---------------- | --------
00000000         | 000...0          | +0 or −0
00000000         | ≠0               | Denormalized small value
11111111         | 000...0          | +∞ or −∞
11111111         | ≠0               | NaN (Not a Number)

Value summary

For single precision (8-bit exponent, 23-bit mantissa):

Type         | Exponent            | Value
-------------|---------------------|------------------
Zero         | 00000000            | ±0
Denormalized | 00000000            | ±0.m × 2^(1−bias)
Normalized   | 00000001–11111110   | ±1.m × 2^(e−bias)
Infinity     | 11111111            | ±∞
NaN          | 11111111 with m ≠ 0 | Not a number

Why bias encoding?

Biasing allows floating-point numbers to be compared directly as integers — higher exponents correspond to larger values, simplifying hardware implementation.