IEEE 754 Floating Point

Slides 57–66 — the biggest block on your exam. The whole topic is one formula plus one packing procedure. Get those two right and every question falls out.

1. Why we need it

Integers can't express:

  • numbers with fractions — 3.1416
  • very small numbers — 0.00000000023
  • very large numbers — 3.15576×1046-3.15576 \times 10^{46}

So we borrow scientific notation, in base 2:

value=(1)sign×significand×2exponent\text{value} = (-1)^{\text{sign}} \times \text{significand} \times 2^{\text{exponent}}
  • More bits for the significand (a.k.a. mantissa) ⇒ more accuracy.
  • More bits for the exponent ⇒ more range.

2. Normalized form

A binary number is normalized when there is exactly one non-zero digit before the binary point — and in base 2 the only non-zero digit is 1. So every normalized number looks like 1.yyy×2zz1.\text{yyy} \times 2^{zz}. (Zero is the exception: it's normalized to significand 0.)

Slide the binary point until one 1 sits to its left; each place you slide it adjusts the exponent.

BinaryNormalizedWhy
0.0011.0×231.0 \times 2^{-3}point moved 3 places right ⇒ exponent −3
101.0011.01001×221.01001 \times 2^{2}point moved 2 places left ⇒ exponent +2
10.111.011×211.011 \times 2^{1}moved 1 place left ⇒ exponent +1
−101.0011011.01001101×22-1.01001101 \times 2^{2}sign is carried along untouched

Move the point left → exponent goes up. Move it right → exponent goes down.

3. The two formats

Single precision — one word, 32 bits

signbit 311 bitexponentbits 30–238 bitssignificand / mantissabits 22–023 bits
IEEE 754 single precision — bias = 127

Double precision — two words, 64 bits

signbit 631 bitexponent11 bits11 bitssignificand52 bits (20 upper + 32 lower)52 bits
IEEE 754 double precision — bias = 1023
SignExponentSignificandBias
Single1823127
Double111521023

4. The master formula ★

value=(1)sign×(1+significand)×2(exponentbias)\text{value} = (-1)^{\text{sign}} \times (1 + \text{significand}) \times 2^{(\text{exponent} - \text{bias})}

Three things are quietly happening in that formula. Understand all three:

(a) The sign bit

0 = positive, 1 = negative. That's it — it is not two's complement.

(b) The hidden leading 1

Every normalized number starts 1. — so storing that 1 would waste a bit. It is implied. If the significand field holds 1001…, the real significand is 1.10011.1001\ldots. That's why the formula says (1+significand)(1 + \text{significand}).

(c) The biased exponent

Stored exponent = real exponent + bias. To read a number back: real exponent = stored exponent − bias.

Why bias instead of two's complement? Because the exponent field then sorts naturally: all-0s is the smallest exponent and all-1s the largest, so plain integer comparison of the bit patterns compares the floating-point values. Bias is 127 (single) / 1023 (double).

5. Converting the fraction to binary

Whole part: repeated division by 2. Fractional part: repeated multiplication by 2, recording the integer digit that pops out each time, read top to bottom.

0.3125 × 2 = 0.625   → 0
0.625  × 2 = 1.25    → 1
0.25   × 2 = 0.50    → 0
0.5    × 2 = 1.00    → 1      (fraction hit 0, stop)

Read top → bottom:  0.3125₁₀ = .0101₂
So 11.3125₁₀ = 1011.0101₂
Shortcut for exam-friendly numbers: the fraction columns are 21=0.52^{-1}=0.5, 22=0.252^{-2}=0.25, 23=0.1252^{-3}=0.125, 24=0.06252^{-4}=0.0625. So 0.75 = 0.5 + 0.25 = .11 and 0.3125 = 0.25 + 0.0625 = .0101.

6. Packing: decimal → IEEE 754 ★

The four steps.
  1. Convert the decimal to binary.
  2. Normalize it: ±1.yy×2zz\pm 1.\text{yy} \times 2^{zz}.
  3. Stored exponent = zz+biaszz + \text{bias}, converted to binary.
  4. Write sign · exponent · the yy digits, then pad with zeros to fill the field.

Example 1 — represent −0.75 in single precision (slide 62)

1. decimal → binary:      −0.75  =  −0.11
2. normalize:             −0.11  =  −1.1 × 2⁻¹        ⇒ real exponent = −1
3. stored exponent:       127 + (−1) = 126 = 01111110₂
4. sign = 1  (negative)
   significand field = the digits after "1." = 1, padded with zeros

   1 01111110 10000000000000000000000
1sign = −1 bit01111110126 = 127 + (−1)8 bits10000000000000000000000the “1” of 1.123 bits
−0.75 in IEEE 754 single precision

Example 2 — represent −10.75 in single AND double precision (slide 67)

1. decimal → binary:   −10.75 = −1010.11
2. normalize:          −1010.11 = −1.01011 × 2³      ⇒ real exponent = 3
3a. SINGLE  exponent = 127  + 3 = 130  = 10000010₂
3b. DOUBLE  exponent = 1023 + 3 = 1026 = 10000000010₂
4. sign = 1 ; significand digits = 01011, then zeros

SINGLE (32 bits):
   1 10000010 01011000000000000000000

DOUBLE (64 bits):
   1 10000000010 0101100000000000000000000000000000000000000000000000
Notice what changes between single and double: only the bias and the field widths. The sign bit and the significand digits 01011 are identical — double just pads with more zeros. So always solve single first, then re-bias for double.

7. Unpacking: IEEE 754 → decimal ★

Run the formula backwards. Given 1 01111110 10000000000000000000000:

sign      = 1              → negative
exponent  = 01111110       = 126 → real exponent = 126 − 127 = −1
significand field = 1000…  → significand = .1  → (1 + .1) = 1.1

value = (−1)¹ × 1.1 × 2⁻¹
      = −1.1 × 2⁻¹
      = −0.11        (shift the point one place right)

Now .11 in decimal:   2⁻¹ + 2⁻² = 0.5 + 0.25 = 0.75

value = −0.75  ✔
Unpacking checklist: (1) split the 32 bits 1 / 8 / 23. (2) sign bit → ±. (3) exponent field → decimal, then subtract the bias. (4) put 1. in front of the significand field. (5) shift the binary point by the exponent. (6) convert the binary fraction to decimal by adding 2k2^{-k} columns.

8. Special values

Some exponent patterns are reserved rather than being ordinary numbers:

Single exponentSingle fractionDouble exponentObject represented
0000 (sign bit may be 0 or 1)
0nonzero0± denormalized number
1–254anything1–2046± ordinary floating-point number
25502047± infinity
255nonzero2047NaN (Not a Number)
  • Zero is special-cased — the hidden leading 1 means the format can't otherwise express 0. Exponent all-0 and fraction all-0 ⇒ the value is 0.
  • ±∞ lets software continue past a divide-by-0 instead of taking an interrupt; the program just prints ∞.
  • NaN is the result of genuinely invalid operations like 0/00/0 or \infty - \infty. Its purpose is to let programmers postpone tests and decisions to a convenient point later in the program.

9. Overflow and underflow

Overflow — a positive exponent grows too large to fit in the exponent field.
Underflow — a negative exponent grows too large (too negative) to fit.

Both can raise interrupts, exactly like integer overflow. The slide's examples:

OVERFLOW
   1.1 × 2¹²⁸
 + 1.1 × 2¹²⁸
 -----------
  11.0 × 2¹²⁸  =  1.1 × 2¹²⁹

  stored exponent would be 129 + 127 = 256 — cannot fit in 8 bits.

UNDERFLOW
   1.111 × 2⁻¹²⁶
 − 1.1   × 2⁻¹²⁶
 ---------------
   0.011 × 2⁻¹²⁶  =  1.1 × 2⁻¹²⁸

  stored exponent would be −128 + 127 = −1 — the exponent field
  holds only non-negative values, so this cannot be represented.

Both failures are the same failure: after normalizing the result, the biased exponent fell outside 02550 \ldots 255.

Section checklist
  • Write the formula (1)s(1+sig)×2ebias(-1)^s (1+\text{sig}) \times 2^{e - \text{bias}} from memory.
  • 1/8/23 with bias 127 · 1/11/52 with bias 1023.
  • Normalize to 1.yy×2zz1.\text{yy} \times 2^{zz} — the leading 1 is hidden.
  • Add the bias when packing, subtract it when unpacking.
  • Know the 5 special-value rows and the overflow/underflow definitions.