Chapter 2: Integers and Floats — The Secrets of Numbers
Vol 3: Computer Core Expedition · Chapter 2
Metadata Card
| Attribute | Value |
|---|---|
| Difficulty | (Advanced) |
| Prerequisites | Chapter 1 (Bits, Bytes, Number Bases) |
| Keywords | Two's Complement, Unsigned Overflow, Signed Overflow, IEEE 754, Floating Point Precision, Rounding Mode |
Your Progress
"Descending deeper, the Core is paved with numbers — integers and floats. But these aren't the numbers you learned in Variable Village — their underlying representation hides countless traps."
Encounter 1: Unsigned Integers — 0 to 2ⁿ-1
Unsigned range: 0 to 2ⁿ - 1Unsigned overflow is well-defined in C — modular arithmetic (wrapping).
c
uint8_t x = 255;
x = x + 1; // wraps to 0
x = x - 1; // wraps to 255Encounter 2: Signed Integers — Two's Complement
Two's complement solves three problems:
- No negative zero
- Same adder for add/subtract
- Natural comparison
For n-bit signed:
Positive: 0 to 2ⁿ⁻¹ - 1
Negative: -1 to -2ⁿ⁻¹
8-bit example:
01111111 → 127 (max)
00000001 → 1
00000000 → 0
11111111 → -1
10000000 → -128 (min)Key insight: x - y = x + (~y + 1) — one adder handles both.
Encounter 3: Signed Overflow is Undefined Behavior
In C, signed integer overflow is undefined behavior. Compilers may assume it never happens and optimize accordingly.
c
int foo(int x) {
// Compiler can assume x+1 > x is always true
if (x + 1 <= x) {
handle_overflow(); // May be deleted!
}
return x + 1;
}Encounter 4: IEEE 754 Floating Point
Formula: (-1)ˢ × 1.m × 2ⁱᵉ⁻ᵇⁱᵃˢ⁾| Format | Bits | Sign | Exponent | Mantissa | Precision |
|---|---|---|---|---|---|
| float | 32 | 1 | 8 | 23 | ~7 digits |
| double | 64 | 1 | 11 | 52 | ~15-16 digits |
Encounter 5: 0.1 + 0.2 ≠ 0.3
0.1 in binary is an infinitely repeating fraction. No finite-precision float can represent it exactly.
Golden Rule: Never compare floats with ==. Always use an epsilon:
c
#define EPSILON 1e-9f
int feq(float a, float b) {
return fabsf(a - b) < EPSILON;
}Common Pitfalls
- Accumulation error: 10⁷ iterations of
+= 0.1fcan accumulate significant error - Catastrophic cancellation:
1e20 + 1.0f - 1e20= 0 in single precision - Non-associativity:
(a + b) + c ≠ a + (b + c)
Verification Checklist
- [ ] Can manually compute two's complement representation
- [ ] Can explain why signed overflow is UB in C
- [ ] Can explain why 0.1 + 0.2 ≠ 0.3
- [ ] Can write a safe float comparison function
Traveler's Notes
- Two's complement: one adder for everything, at the cost of asymmetric range
- Signed overflow = UB (C). Unsigned overflow = well-defined modular arithmetic
0.1 + 0.2 ≠ 0.3is a math fact, not a computer bug
→ Next Stop Preview
Chapter 3: Digital Logic — From Logic Gates to Computers