Numbers for Real
Tap the board to zoom inClick the board to zoom in
Lessons that use this board
NUMBERS FOR REAL
How Computers Work · 02 — negatives, overflow, and floating point: where finite bits get SHARP edges
a byte holds only 256 patterns. so every number a computer stores is finite — negatives need a trick, big sums WRAP, and fractions are approximations. know where they break.
1 · UNSIGNED & OVERFLOW
n bits (unsigned) = 0 to 2^n - 1. a byte = 0 to 255. arithmetic is MODULAR — it wraps on a circle:
0
255
128
127
255 + 1 → 0 (wraps!)
unsigned 255 + 1 = 0. the top bit just falls off the end (carry lost). same idea for any width.
REAL WORLD — int32 overflow at 2,147,483,647:
· YouTube's view counter broke on 'Gangnam Style' → they moved to 64-bit ints.
· the '2038 problem': 32-bit signed Unix time (seconds since 1970) overflows Jan 19, 2038.
2 · SIGNED — TWO'S COMPLEMENT
how do you store a NEGATIVE with only 1s and 0s? the winning scheme: TWO'S COMPLEMENT.
to negate a number: FLIP every bit, then ADD 1.
+5 = 0000 0101
flip = 1111 1010
+1 = 1111 1011 = -5
the top bit still shows the sign (1 = negative) — but the SAME adder now does
signed AND unsigned math. that's the whole point: -x behaves as 2^n - x (mod 2^n).
8-bit signed range: -128 to +127.
1000 0000 = -128 (most negative) 0111 1111 = +127 (max) 1111 1111 = -1
the gotcha: SIGNED overflow wraps to NEGATIVE —
127 + 1 = 1000 0000 = -128
one MORE negative than positive (no -0), and abs(-128) overflows. these edges bite real code.
3 · FLOATING POINT — IEEE 754 (fractions & huge/tiny numbers)
idea: binary scientific notation. value = (-1)^sign × 1.mantissa × 2^(exponent - bias).
same trick as 6.022 × 10^23 — a sign, some significant digits, and an exponent — but in base 2.
float32 (single precision) — 32 bits:
S
E
E
E
E
E
E
E
E
M
M
M
M
M
M
M
M
M
M
M
M
sign
exponent (8 bits, bias 127)
mantissa / fraction (23 bits) — the '...' continues
· SIGN 1 bit · EXPONENT 8 bits, stored + 127 (the 'bias', to allow negatives) · MANTISSA 23 bits (an implicit leading 1).
float64 (double) = 1 + 11 + 52 bits. more mantissa = more PRECISION; more exponent = more RANGE.
the deal: a FIXED number of bits, so you trade range against precision — you can't have infinite of both.
gaps GROW with magnitude: near 1.0 floats are dense; near 10^16 the gap between neighbours exceeds 1.
4 · WHY 0.1 + 0.2 ≠ 0.3
0.1 in binary is a REPEATING fraction — like 1/3 = 0.333... in decimal.
0.1 = 0.0001100110011..._2 (never terminates)
so 0.1 and 0.2 are stored ROUNDED. add the two rounding errors:
0.1 + 0.2 = 0.30000000000000004
the float isn't 'wrong' — it's the closest representable value. but == fails.
RULES THAT SAVE YOU:
· never use float for MONEY → integer cents, or a decimal type.
· never test floats with == → compare within an epsilon (|a-b| < 1e-9).
REAL-WORLD DISASTERS:
· Patriot missile (1991): 0.1s stored as float drifted 0.34s over 100h →
a 600m tracking miss; 28 soldiers killed.
· Ariane 5 (1996): a 64-bit float crammed into a 16-bit int overflowed →
the rocket self-destructed 37s after launch. $370M gone.
special values baked into IEEE 754: ±0, +Inf, -Inf, and NaN (0/0) — which is not even equal to itself.
JARGON, DECODED
unsigned = non-negative, 0..2^n-1 · signed = allows negatives · two's complement = negate by flip-bits-then-+1; one adder does both; range -2^(n-1)..2^(n-1)-1
overflow = a result too big for the width, wraps around (mod 2^n) · modular arithmetic = clock/wheel wraparound · the 2038 problem = 32-bit Unix time overflow
floating point = binary scientific notation (sign · mantissa · exponent) · IEEE 754 = the standard (float32 = 1/8/23, float64 = 1/11/52) · bias = offset that lets the exponent go negative · mantissa = the significant digits
rounding error = a value not exactly representable in binary (0.1) · epsilon compare = |a-b| < tiny instead of == · NaN = 'not a number', ≠ itself · Inf = overflowed-to-infinity
Saved in this browser only.