4.8.2Numerical Methods

IEEE 754 floating-point standard — significant bits, special values

1,865 words8 min readdifficulty · medium6 backlinks

WHAT is being stored?

The 32-bit (single precision) and 64-bit (double precision) layouts:

Format Total bits Sign ss Exponent EE Fraction ff Bias
single 32 1 8 23 127
double 64 1 11 52 1023
Figure — IEEE 754 floating-point standard — significant bits, special values

HOW the bits become a number (derivation from scratch)

Step 1 — Normalize in binary. Any nonzero real in scientific binary looks like x=±1.b1b2b3×2Ereal.x = \pm 1.b_1b_2b_3\ldots \times 2^{E_{\text{real}}}. Why this step? In base 2 the leading digit of a normalized number is always 1 (it can't be 0, or it wouldn't be the leading digit). So we get that bit for free — it is implicit (the "hidden bit").

Step 2 — Store only the fraction. Since the leading 11 is implied, we store just f=b1b2f = b_1b_2\ldots So the significand is m=1.f=1+i=1pbi2i.m = 1.f = 1 + \sum_{i=1}^{p} b_i\,2^{-i}. Why this step? Storing the hidden 11 would waste a bit and buy a free bit of precision: 23 stored fraction bits behave like 24 significant bits.

Step 3 — Bias the exponent. Exponents can be negative (252^{-5}) or positive (2+52^{+5}). Instead of a sign for the exponent, we store E=Ereal+bias,bias=2k11E = E_{\text{real}} + \text{bias}, \qquad \text{bias}=2^{k-1}-1 where kk = number of exponent bits. Why? A plain unsigned EE then lets simple integer comparison sort floats correctly, and reserves the all-0 and all-1 patterns for special meanings.


Significant bits & precision


Special values (the reserved exponent patterns)

The all-0 and all-1 exponent fields are not ordinary numbers:

EE field fraction ff meaning
all 0 00 ±0\pm 0 (signed zero)
all 0 0\neq 0 subnormal (denormal) number, no hidden 1: x=(1)s(0.f)21biasx=(-1)^s\,(0.f)\,2^{1-\text{bias}}
12541\ldots254 (single) any normal number (formula above)
all 1 00 ±\pm\infty
all 1 0\neq 0 NaN (Not a Number)

Worked examples


Common mistakes (steel-manned)


Recall Feynman: explain to a 12-year-old

Imagine you can only write 7 digits on a tiny card, but you also get a little "shift the dot" note. So you write 60226022 and a note "move the dot 23 places right" to mean a huge number, or "move it left" for a tiny one. The card is the mantissa, the note is the exponent, and a +/+/- box is the sign. Because the card has only a few digits, some numbers like 0.10.1 can't fit perfectly — you get the closest card you can write. Two special notes are saved: one says "this is so big it's basically infinity," and one says "this isn't a real number at all" (like dividing by zero).


Flashcards

IEEE 754 single-precision bit split (sign/exp/frac)?
1 / 8 / 23 bits
IEEE 754 double-precision bit split?
1 / 11 / 52 bits
Value formula for a normal float?
(1)s(1.f)2×2Ebias(-1)^s (1.f)_2 \times 2^{E-\text{bias}}
Bias for single and double precision?
127 and 1023 (i.e. 2k112^{k-1}-1)
What is the "hidden bit"?
The implicit leading 1 of a normalized significand, not stored
Effective significant bits in single precision?
24 (23 stored + 1 hidden) ≈ 7 decimal digits
Definition of machine epsilon?
Gap between 1.0 and next float, ε=2(p1)\varepsilon=2^{-(p-1)}
Machine epsilon (single, double)?
2231.19e72^{-23}\approx1.19e{-7}, 2522.22e162^{-52}\approx2.22e{-16}
How is ±\pm\infty encoded?
Exponent all 1s, fraction = 0
How is NaN encoded?
Exponent all 1s, fraction 0\neq 0
How is a subnormal encoded and valued?
Exp all 0s, f0f\neq0: (1)s(0.f)21bias(-1)^s(0.f)\,2^{1-\text{bias}}
Why do subnormals exist?
Gradual underflow — fill the gap between smallest normal and 0
Quick test for NaN in code?
x != x is true only for NaN
Why isn't 0.1 exact?
Its binary expansion repeats forever; 5 is not a power of 2
Decimal digits from p bits?
plog102p\log_{10}2

Connections

Concept Map

solved by

stores as

formula

Step 1

leading 1 always

Step 2

gains free bit

Step 3

reserves patterns

p times log10 2

defines

two layouts

Finite bits vs infinite reals

IEEE 754 standard

Sign + Mantissa + Exponent

x = -1^s times m times 2^e

Normalize in binary

Hidden bit implicit

Significand 1.f

24 or 53 significant bits

Biased exponent E plus bias

Special values

Decimal digits precision

Machine epsilon 2^-(p-1)

Single 32-bit / Double 64-bit

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, computer ke paas sirf 32 ya 64 bits hote hain, lekin real numbers infinite hote hain. Toh IEEE 754 ek smart jugaad hai: number ko binary scientific notation me likho — (1)s×m×2e(-1)^s \times m \times 2^e. Yaani ek sign bit, ek exponent (kitna shift karna hai), aur ek mantissa (significant digits). Single precision me split hota hai 1 + 8 + 23, aur double me 1 + 11 + 52.

Sabse mazedaar trick hai hidden bit: normalized binary number ka pehla digit hamesha 1 hota hai, toh use store karne ki zaroorat hi nahi — automatic maan lete hain. Isliye 23 stored bits actually 24 significant bits ki tarah kaam karte hain, jo lagbhag 7 decimal digits ke barabar hai. Exponent ko bias ke saath store karte hain (single me 127 add karke), taaki negative exponents bhi unsigned form me aa jaayen aur comparison aasaan ho.

Special values bhi yaad rakho: exponent agar all zeros ho toh number ya to zero hai ya subnormal (bahut chhote numbers, gradual underflow ke liye). Exponent all ones ho toh ya ±\pm\infty (jaise 1/01/0) ya NaN (jaise 0/00/0 ya 1\sqrt{-1}). Ek important baat: NaN kabhi khud ke barabar nahi hota, isliye x != x se NaN check karte hain.

Kyun important hai? Kyunki rounding ki wajah se 0.1+0.20.30.1 + 0.2 \ne 0.3 ho jaata hai computer me — 0.10.1 binary me exactly store hi nahi hota. Numerical methods me yahi chhoti-chhoti errors add hoke bada blunder ban sakti hain, isliye floats ko == se compare mat karo, hamesha tolerance ke saath compare karo.

Test yourself — Numerical Methods

Connections