About

2.4 Floating-Point Representation & IEEE 754

Theory: Floating-Point Representation & IEEE 754

Integers alone are not enough for scientific or business computing. This section shows how computers store very large, very small and fractional numbers by borrowing the idea of scientific notation, first with the lecture’s simple 14-bit model, then with the real-world IEEE 754 standard (Null & Lobur, Ch. 2).

Why Floating Point?

With 16-bit two’s complement integers the largest value is +32,767+32{,}767. Two everyday needs break this immediately:

The answer is floating-point representation, which is scientific notation in binary: 32,000,000=32×10632{,}000{,}000 = 32 \times 10^6 needs only the digits “32” and the exponent “6”.

Floating-point calculations can be done using only integer hardware, by software that stores the parts separately and manipulates them. This is called floating-point emulation (floating-point values are not stored as such; programs make it seem as if they are). Most of today’s computers instead have specialized floating-point hardware (an FPU), which is far faster.

The Three Parts of Scientific Notation

+⏟sign 1.25⏟mantissa (significand)×10−1⏞exponent\underbrace{+}_{\text{sign}}\ \underbrace{1.25}_{\text{mantissa (significand)}} \times 10^{\overbrace{-1}^{\text{exponent}}}
PartMeaning
Signpositive or negative
Significand (mantissa)the significant digits; its length sets the precision
Exponentthe power of the base; its size sets the range

The radix point “floats”: the same number can be written many ways (17=17.0×100=1.7×101=0.17×10217 = 17.0 \times 10^0 = 1.7 \times 10^1 = 0.17 \times 10^2), and the exponent records where the point really is. In binary, 17=100012×20=1000.12×21=⋯=1.00012×2417 = 10001_2 \times 2^0 = 1000.1_2 \times 2^1 = \cdots = 1.0001_2 \times 2^4.

Normalization

To make the representation unique and use the significand bits efficiently, numbers are normalized: the exponent is adjusted so there are no leading 0s, giving the form

1.xxxxxxxx×2yyyy1.\text{xxxxxxxx} \times 2^{\text{yyyy}}

For example 0.01×2−1=0.1×2−2=1.0×2−30.01 \times 2^{-1} = 0.1 \times 2^{-2} = 1.0 \times 2^{-3}.

Shifting rule: moving the binary point left by kk places adds kk to the exponent; moving it right by kk places subtracts kk. Example: 1001.101=1.001101×231001.101 = 1.001101 \times 2^3 (point moved left 3).

Since a normalized nonzero binary number always starts with “1.”, that 1 need not be stored. Only the fraction bits after the point are kept; the leading 1 is implicit (a “hidden bit”), which gains one bit of precision for free.

The Lecture’s 14-bit Model

FieldBitsContents
Sign10 = positive, 1 = negative
Exponent5power of 2
Significand8the 8 fraction bits after the implicit leading 1
13sign (1 bit)
12–8exponent (5 bits)
7–0significand (8 bits)

Step 1: the simple (unbiased) model

Store the exponent as a plain unsigned 5-bit number. For 17=1.0001×2417 = 1.0001 \times 2^4: sign 0, exponent 4=001004 = 00100, fraction 00010001 padded to 8 bits =00010000= 00010000:

17  →  0  00100  0001000017 \;\to\; 0\ \ 00100\ \ 00010000

Disadvantage: a 5-bit unsigned field only holds 00 to 3131, so there is no way to store a negative exponent, e.g. 1.0001×2−41.0001 \times 2^{-4}. Every small number (between 0 and 1) is impossible.

Step 2: the biased (excess-16) model

To allow negative exponents, store the exponent plus a fixed bias. With 5 bits there are 32 values (00 to 3131); choosing the bias near the middle, 16, gives an excess-16 representation:

stored exponent=true exponent+16,true exponent=stored−16\text{stored exponent} = \text{true exponent} + 16, \qquad \text{true exponent} = \text{stored} - 16
Stored exponentTrue exponent
00 to 1515negative (−16-16 to −1-1)
1616zero
1717 to 3131positive (+1+1 to +15+15)

Advantage: biased exponents are plain unsigned numbers, so comparing or sorting floating-point values is easier and faster: a larger true exponent always has a larger stored pattern. For example −1→15=01111-1 \to 15 = 01111 and −2→14=01110-2 \to 14 = 01110; since 01111>0111001111 \gt 01110 as unsigned numbers, −1>−2-1 \gt -2 is preserved.

Mini-example

17=1.0001×2417 = 1.0001 \times 2^4: stored exponent 16+4=20=1010016 + 4 = 20 = 10100, so 17→0 10100 0001000017 \to 0\ 10100\ 00010000.

−1.0100×2−9-1.0100 \times 2^{-9}: sign 1, stored exponent 16−9=7=0011116 - 9 = 7 = 00111, fraction 0100000001000000, so it is stored as 1 00111 010000001\ 00111\ 01000000.

Procedure: decimal → 14-bit (excess-16)

  1. Sign bit: 0 if positive, 1 if negative. Work with the absolute value from now on.
  2. Convert to binary (integer part by division, fraction part by multiplication).
  3. Normalize to 1.f×2e1.f \times 2^e.
  4. Exponent field =e+16= e + 16 in 5-bit binary (must lie in 00 to 3131).
  5. Significand field = the bits of ff, padded with 0s on the right (or truncated) to 8 bits.

The IEEE 754 Standard

Set by the Institute of Electrical and Electronics Engineers (IEEE) in 1985, to make floating-point programs portable between processors and to encourage numerically oriented software. It uses the same ideas as the 14-bit model: sign, biased exponent and fraction with an implicit leading 1.

FormatTotalSignExponentFractionBiasSpecial exponent
Single precision32 bits1823127255 (all ones)
Double precision64 bits1115210232047 (all ones)

The value of a normal number is

(−1)S×(1.F)2×2 E−bias(-1)^S \times (1.F)_2 \times 2^{\,E - \text{bias}}

where SS is the sign bit, EE the stored (biased) exponent and FF the stored fraction.

31sign (1 bit)
30–23exponent (8 bits)
22–0fraction (23 bits)
Single precision (32 bits)
63sign (1 bit)
62–52exponent (11 bits)
51–0fraction (52 bits)
Double precision (64 bits)

Special values (single precision; for double replace 255 by 2047):

Exponent EEFraction FFMeaning
00zero: +0+0 if S=0S = 0, −0-0 if S=1S = 1 (two zeros)
1 to 254anythingnormal number (−1)S×1.F×2E−127(-1)^S \times 1.F \times 2^{E-127}
2550infinity: +∞+\infty or −∞-\infty depending on SS
255nonzeroNaN (“not a number”), flags an error, e.g. 0/00/0 or −1\sqrt{-1}

Because zero has two representations (+0+0 and −0-0) and because results are approximations, programmers should avoid testing a floating-point value for equality to zero (see section 2.5).

Procedure: decimal → IEEE single

  1. Sign bit SS.
  2. Convert ∣x∣|x| to binary and normalize to 1.f×2e1.f \times 2^e.
  3. E=e+127E = e + 127 as an 8-bit unsigned number.
  4. FF = bits of ff, padded to 23 bits.
  5. Write S E FS\,E\,F as 32 bits and group into 4s for hex.

Procedure: IEEE single → decimal: expand the hex into 32 bits, split 1∣8∣231 | 8 | 23, check for special values, otherwise compute (−1)S×1.F×2E−127(-1)^S \times 1.F \times 2^{E-127}.

Mini-example

1.01.0: S=0S = 0, 1.0=1.0×201.0 = 1.0 \times 2^0, E=0+127=01111111E = 0 + 127 = 01111111, F=0F = 0: 0 01111111 000⋯0=0011 1111 1000 0000⋯=3F800000160\ 01111111\ 000\cdots0 = 0011\,1111\,1000\,0000 \cdots = \text{3F800000}_{16}.


Worked Examples

Exercise 1

Store 171017_{10} in the lecture’s 14-bit floating-point model (a) with an unsigned (unbiased) exponent and (b) with an excess-16 exponent. Why was the bias introduced?

Show solution ↓
Solution

Convert and normalize: 17=16+1=100012=1.00012×2417 = 16 + 1 = 10001_2 = 1.0001_2 \times 2^4 (point moved left 4 places).

  • Sign: positive → 00
  • Fraction after the leading 1: 00010001 → padded to 8 bits: 0001000000010000

(a) Unbiased: exponent field =4=00100= 4 = 00100.

17  →  0  00100  0001000017 \;\to\; 0\ \ 00100\ \ 00010000

(b) Excess-16: exponent field =4+16=20=10100= 4 + 16 = 20 = 10100.

17  →  0  10100  0001000017 \;\to\; 0\ \ 10100\ \ 00010000

Check (b): exponent 20−16=420 - 16 = 4; significand 1.00012=1.06251.0001_2 = 1.0625; value 1.0625×16=171.0625 \times 16 = 17 ✓.

Why a bias? An unsigned exponent field cannot hold negative exponents, so numbers like 1.0001×2−41.0001 \times 2^{-4} (anything smaller than 1) could not be stored. With excess-16, stored values 00 to 1515 mean exponents −16-16 to −1-1. The bias also keeps exponent fields unsigned, so magnitudes compare like ordinary binary numbers (fast sorting/comparison).

Exercise 2

Express each value in the 14-bit model with an excess-16 exponent:

(a) 9.6259.625 (b) −0.375-0.375 (c) −1.01002×2−9-1.0100_2 \times 2^{-9} (also give its decimal value)

Show solution ↓
Solution

(a) 9=100129 = 1001_2; 0.6250.625: 0.625×2=1.25→10.625 \times 2 = 1.25 \to 1, 0.25×2=0.5→00.25 \times 2 = 0.5 \to 0, 0.5×2=1.0→10.5 \times 2 = 1.0 \to 1, so 0.625=0.10120.625 = 0.101_2.

9.625=1001.1012=1.0011012×239.625 = 1001.101_2 = 1.001101_2 \times 2^3
  • Sign 00
  • Exponent 3+16=19=100113 + 16 = 19 = 10011
  • Fraction 001101001101 → 0011010000110100

Answer: 0 10011 001101000\ 10011\ 00110100. Check: 1.0011012=1+18+116+164=1.2031251.001101_2 = 1 + \tfrac18 + \tfrac1{16} + \tfrac1{64} = 1.203125, and 1.203125×8=9.6251.203125 \times 8 = 9.625 ✓.

(b) 0.3750.375: 0.375×2=0.75→00.375 \times 2 = 0.75 \to 0, 0.75×2=1.5→10.75 \times 2 = 1.5 \to 1, 0.5×2=1.0→10.5 \times 2 = 1.0 \to 1, so 0.375=0.0112=1.12×2−20.375 = 0.011_2 = 1.1_2 \times 2^{-2} (point moved right 2).

  • Sign 11
  • Exponent −2+16=14=01110-2 + 16 = 14 = 01110
  • Fraction 11 → 1000000010000000

Answer: 1 01110 100000001\ 01110\ 10000000. Check: −1.5×2−2=−0.375-1.5 \times 2^{-2} = -0.375 ✓.

(c) Already normalized.

  • Sign 11
  • Exponent −9+16=7=00111-9 + 16 = 7 = 00111
  • Fraction 01000100 → 0100000001000000

Answer: 1 00111 010000001\ 00111\ 01000000. Decimal value: 1.01002=1.251.0100_2 = 1.25, so −1.25×2−9=−1.25/512=−0.00244140625-1.25 \times 2^{-9} = -1.25 / 512 = -0.00244140625.

Exercise 3

Each pattern is in the 14-bit excess-16 model. Find its decimal value.

(a) 0 10010 101000000\ 10010\ 10100000 (b) 1 01110 100000001\ 01110\ 10000000 (c) 0 10001 100000000\ 10001\ 10000000 (d) 0 01100 100110010\ 01100\ 10011001

Show solution ↓
Solution

Value =(−1)S×1.(fraction)2×2exp−16= (-1)^S \times 1.\text{(fraction)}_2 \times 2^{\text{exp} - 16}.

(a) S=0S = 0; exponent 10010=1810010 = 18, true 18−16=218 - 16 = 2; significand 1.1012=1.6251.101_2 = 1.625. 1.625×22=6.51.625 \times 2^2 = 6.5. (Or shift: 1.101×22=110.12=6.51.101 \times 2^2 = 110.1_2 = 6.5.) +6.5+6.5

(b) S=1S = 1; exponent 01110=1401110 = 14, true −2-2; significand 1.12=1.51.1_2 = 1.5. −1.5×2−2=−0.375-1.5 \times 2^{-2} = -0.375. −0.375-0.375

(c) S=0S = 0; exponent 10001=1710001 = 17, true 11; significand 1.12=1.51.1_2 = 1.5. 1.5×2=31.5 \times 2 = 3. +3+3

(d) S=0S = 0; exponent 01100=1201100 = 12, true −4-4; significand 1.100110012=1+153256=1.597656251.10011001_2 = 1 + \tfrac{153}{256} = 1.59765625. 1.59765625/16=0.0998535156251.59765625 / 16 = 0.099853515625. This is the closest the model gets to 0.10.1 (by truncation), a first taste of the precision limits discussed in section 2.5.

Exercise 4

In the excess-16 model:

(a) Give the stored exponent field for true exponents −1-1, −2-2, 55 and 66, and show that comparing the stored fields as unsigned numbers gives the correct order.

(b) What range of true exponents can be stored? What is the largest positive value of the 14-bit model?

Show solution ↓
Solution

(a) Stored == true +16+ 16:

True exponentStored (decimal)Stored (binary)
−2-21401110
−1-11501111
552110101
662210110

As unsigned numbers 01110<01111<10101<1011001110 \lt 01111 \lt 10101 \lt 10110, which is exactly −2<−1<5<6-2 \lt -1 \lt 5 \lt 6. With a two’s complement exponent instead, −1-1 would be 1111111111 and would wrongly look larger than 6=001106 = 00110.

(b) The 5-bit field holds 00 to 3131, so true exponents are 0−16=−160 - 16 = -16 up to 31−16=+1531 - 16 = +15.

Largest positive value: sign 0, largest exponent 1111111111 (+15+15), all fraction bits 1:

0 11111 11111111=1.111111112×215=(2−2−8)×215=65536−128=654080\ 11111\ 11111111 = 1.11111111_2 \times 2^{15} = (2 - 2^{-8}) \times 2^{15} = 65536 - 128 = 65408

Any result larger than this overflows. (At the small end the true exponent cannot go below −16-16; anything needing a smaller exponent underflows.)

Exercise 5

Convert to IEEE 754 single precision. Give the 32 bits and the hexadecimal form.

(a) −12.375-12.375 (b) 0.156250.15625

Show solution ↓
Solution

(a) −12.375-12.375

  1. Sign: negative → S=1S = 1.
  2. 12=1100212 = 1100_2; 0.375=0.01120.375 = 0.011_2 (from 0.375→0.75→1.5→1.00.375 \to 0.75 \to 1.5 \to 1.0: digits 0, 1, 1). So 12.375=1100.011212.375 = 1100.011_2.
  3. Normalize: 1100.011=1.100011×231100.011 = 1.100011 \times 2^3.
  4. E=3+127=130=1000 00102E = 3 + 127 = 130 = 1000\,0010_2.
  5. F=100011F = 100011 followed by 17 zeros (23 bits).
311S (1 bit)
30–2310000010E (8 bits)
22–010001100000000000000000F (23 bits)

Group into 4s: 1100 0001 0100 0110 0000 0000 0000 00001100\ 0001\ 0100\ 0110\ 0000\ 0000\ 0000\ 0000

Hex: C146000016\text{C1460000}_{16}.

Check: −(1+12+132+164)×2130−127=−1.546875×8=−12.375-(1 + \tfrac12 + \tfrac1{32} + \tfrac1{64}) \times 2^{130 - 127} = -1.546875 \times 8 = -12.375 ✓.

(b) 0.156250.15625

  1. S=0S = 0.
  2. 0.15625×2=0.3125→00.15625 \times 2 = 0.3125 \to 0; 0.3125×2=0.625→00.3125 \times 2 = 0.625 \to 0; 0.625×2=1.25→10.625 \times 2 = 1.25 \to 1; 0.25×2=0.5→00.25 \times 2 = 0.5 \to 0; 0.5×2=1.0→10.5 \times 2 = 1.0 \to 1. So 0.15625=0.0010120.15625 = 0.00101_2.
  3. Normalize: 0.00101=1.01×2−30.00101 = 1.01 \times 2^{-3} (point moved right 3).
  4. E=−3+127=124=0111 11002E = -3 + 127 = 124 = 0111\,1100_2.
  5. F=01F = 01 followed by 21 zeros.
310S (1 bit)
30–2301111100E (8 bits)
22–001000000000000000000000F (23 bits)

Group into 4s: 0011 1110 0010 0000 0000 0000 0000 00000011\ 1110\ 0010\ 0000\ 0000\ 0000\ 0000\ 0000

Hex: 3E20000016\text{3E200000}_{16}.

Check: 1.25×2124−127=1.25/8=0.156251.25 \times 2^{124 - 127} = 1.25 / 8 = 0.15625 ✓.

Exercise 6

Interpret each IEEE 754 single-precision hex pattern.

(a) 41C80000\text{41C80000} (b) C0A00000\text{C0A00000} (c) 7F800000\text{7F800000} (d) FF800000\text{FF800000} (e) 7FC00000\text{7FC00000} (f) 80000000\text{80000000}

Show solution ↓
Solution

Expand to binary and split 1∣8∣231 | 8 | 23.

(a) 41C80000=0100 0001 1100 1000 0000⋯\text{41C80000} = 0100\,0001\,1100\,1000\,0000\cdots

  • S=0S = 0
  • E=1000 0011=131E = 1000\,0011 = 131, so e=131−127=4e = 131 - 127 = 4
  • F=1001 000⋯F = 1001\,000\cdots, significand 1.100121.1001_2

Value: 1.10012×24=110012=251.1001_2 \times 2^4 = 11001_2 = 25. +25+25

(b) C0A00000=1100 0000 1010 0000⋯\text{C0A00000} = 1100\,0000\,1010\,0000\cdots

  • S=1S = 1
  • E=1000 0001=129E = 1000\,0001 = 129, e=2e = 2
  • F=0100⋯F = 0100\cdots, significand 1.012=1.251.01_2 = 1.25

Value: −1.25×4=−5-1.25 \times 4 = -5. −5-5

(c) 7F800000=0 11111111 000⋯0\text{7F800000} = 0\ 11111111\ 000\cdots0: E=255E = 255, F=0F = 0, S=0S = 0 → +∞+\infty.

(d) FF800000=1 11111111 000⋯0\text{FF800000} = 1\ 11111111\ 000\cdots0: E=255E = 255, F=0F = 0, S=1S = 1 → −∞-\infty.

(e) 7FC00000=0 11111111 1000⋯0\text{7FC00000} = 0\ 11111111\ 1000\cdots0: E=255E = 255, F≠0F \ne 0 → NaN.

(f) 80000000=1 00000000 000⋯0\text{80000000} = 1\ 00000000\ 000\cdots0: E=0E = 0, F=0F = 0, S=1S = 1 → −0-0 (the second zero; 00000000\text{00000000} is +0+0).

Exercise 7

(a) Write 1.01.0 and −2.5-2.5 in IEEE 754 double precision (hex).

(b) Why does the double-precision format reach magnitudes of about 1030810^{308}? What is the approximate largest single-precision value?

Show solution ↓
Solution

(a) Double: 1 sign bit, 11 exponent bits with bias 1023, 52 fraction bits.

1.0=1.0×201.0 = 1.0 \times 2^0: S=0S = 0, E=0+1023=011 1111 1111E = 0 + 1023 = 011\,1111\,1111, F=0F = 0.

0 01111111111 0000...0000
= 0011 1111 1111 0000 ... 0000

Hex: 3FF000000000000016\text{3FF0000000000000}_{16}.

−2.5=−10.12=−1.012×21-2.5 = -10.1_2 = -1.01_2 \times 2^1: S=1S = 1, E=1+1023=1024=100 0000 0000E = 1 + 1023 = 1024 = 100\,0000\,0000, F=01F = 01 then 50 zeros.

1 10000000000 0100...0000
= 1100 0000 0000 0100 0000 ... 0000

Hex: C00400000000000016\text{C004000000000000}_{16}.

(b) With 11 exponent bits and bias 1023, true exponents go up to about ±1023\pm 1023, so magnitudes reach roughly 210242^{1024}. Converting to a power of 10, solve 21024=10x2^{1024} = 10^x:

x=1024log⁡102≈1024×0.30103≈308.25x = 1024 \log_{10} 2 \approx 1024 \times 0.30103 \approx 308.25

So doubles span roughly 10−30810^{-308} to 1030810^{308} in magnitude. Anything beyond is overflow; anything nonzero but closer to 0 than about 10−30810^{-308} is underflow.

Single precision (8-bit exponent, largest true exponent 254−127=127254 - 127 = 127): largest value =(2−2−23)×2127≈3.4×1038= (2 - 2^{-23}) \times 2^{127} \approx 3.4 \times 10^{38}; the smallest normal positive value is 2−126≈1.18×10−382^{-126} \approx 1.18 \times 10^{-38}.