Exercises Notebook
Converted from
exercises.ipynbfor web reading.
Number Systems - Exercises
This notebook contains 10 progressive exercises for 01-Number-Systems. Each exercise has a learner workspace followed by a complete reference solution. The goal is fluent foundational math for later linear algebra, calculus, probability, and ML sections.
Code cell 2
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
try:
import seaborn as sns
sns.set_theme(style="whitegrid", palette="colorblind")
HAS_SNS = True
except ImportError:
plt.style.use("seaborn-v0_8-whitegrid")
HAS_SNS = False
mpl.rcParams.update({
"figure.figsize": (10, 6),
"figure.dpi": 120,
"font.size": 13,
"axes.titlesize": 15,
"axes.labelsize": 13,
"xtick.labelsize": 11,
"ytick.labelsize": 11,
"legend.fontsize": 11,
"legend.framealpha": 0.85,
"lines.linewidth": 2.0,
"axes.spines.top": False,
"axes.spines.right": False,
"savefig.bbox": "tight",
"savefig.dpi": 150,
})
np.random.seed(42)
print("Plot setup complete.")
Code cell 3
import numpy as np
import numpy.linalg as la
from decimal import Decimal, getcontext
from itertools import product
np.set_printoptions(precision=8, suppress=True)
np.random.seed(42)
def header(title):
print("\n" + "=" * len(title))
print(title)
print("=" * len(title))
def check_true(name, cond):
ok=bool(cond)
print(f"{'PASS' if ok else 'FAIL'} - {name}")
return ok
def check_close(name, got, expected, tol=1e-8):
ok=np.allclose(got, expected, atol=tol, rtol=tol)
print(f"{'PASS' if ok else 'FAIL'} - {name}")
if not ok:
print(' got =', got)
print(' expected=', expected)
return ok
def powerset(s):
items=list(s)
return [set(items[i] for i in range(len(items)) if mask & (1 << i)) for mask in range(1 << len(items))]
print("Chapter 01 helper setup complete.")
Exercise 1: Classify Number Types
Classify examples as natural, integer, rational, real, or complex.
Code cell 5
# Your Solution
# Exercise 1 - learner workspace
# Write your solution here, then run the reference solution below to compare.
print("Learner workspace ready for Exercise 1.")
Code cell 6
# Solution
# Exercise 1 - Classify Number Types
header("Exercise 1: number classification")
examples=[0, -3, 4/7, np.sqrt(2), 2+3j]
def labels(x):
out=[]
if isinstance(x, complex) and x.imag!=0: return ['complex']
if float(x).is_integer() and x>=0: out.append('natural')
if float(x).is_integer(): out.append('integer')
if x in [0,-3,4/7]: out.append('rational')
out.append('real')
out.append('complex')
return out
for x in examples: print(x, labels(x))
check_true("integer subset real", 'real' in labels(-3))
check_true("nonreal complex detected", labels(2+3j)==['complex'])
Exercise 2: Binary Floating Point
Show why decimal fractions such as are not exact in binary floating point.
Code cell 8
# Your Solution
# Exercise 2 - learner workspace
# Write your solution here, then run the reference solution below to compare.
print("Learner workspace ready for Exercise 2.")
Code cell 9
# Solution
# Exercise 2 - Binary Floating Point
header("Exercise 2: floating point")
x=0.1+0.2
print("0.1 + 0.2 =", repr(x))
check_true("not exactly 0.3", x != 0.3)
check_close("close to 0.3", x, 0.3)
print("machine epsilon:", np.finfo(float).eps)
Exercise 3: Modular Arithmetic
Compute residues and use them for cyclic position encodings.
Code cell 11
# Your Solution
# Exercise 3 - learner workspace
# Write your solution here, then run the reference solution below to compare.
print("Learner workspace ready for Exercise 3.")
Code cell 12
# Solution
# Exercise 3 - Modular Arithmetic
header("Exercise 3: modular arithmetic")
positions=np.arange(12)
res=positions % 5
print("residues", res)
check_close("period repeats", res[:5], res[5:10])
check_true("all residues in range", set(res) <= set(range(5)))
Exercise 4: Complex Plane
Verify Euler rotation by multiplying by .
Code cell 14
# Your Solution
# Exercise 4 - learner workspace
# Write your solution here, then run the reference solution below to compare.
print("Learner workspace ready for Exercise 4.")
Code cell 15
# Solution
# Exercise 4 - Complex Plane
header("Exercise 4: complex rotation")
z=1+0j; theta=np.pi/2
rot=np.exp(1j*theta)*z
print("rotated", rot)
check_close("90 degree rotation real part", rot.real, 0.0, tol=1e-12)
check_close("90 degree rotation imag part", rot.imag, 1.0, tol=1e-12)
Exercise 5: Integer Overflow
Show how fixed-width integers can overflow while Python integers do not.
Code cell 17
# Your Solution
# Exercise 5 - learner workspace
# Write your solution here, then run the reference solution below to compare.
print("Learner workspace ready for Exercise 5.")
Code cell 18
# Solution
# Exercise 5 - Integer Overflow
header("Exercise 5: integer overflow")
a=np.array([127], dtype=np.int8)
b=a+np.array([1], dtype=np.int8)
print("int8 127 + 1 ->", b[0])
check_true("wraps around", int(b[0]) == -128)
check_true("Python int does not wrap", 127+1 == 128)
Exercise 6: Log-Space Products
Compute a tiny product stably using log sums.
Code cell 20
# Your Solution
# Exercise 6 - learner workspace
# Write your solution here, then run the reference solution below to compare.
print("Learner workspace ready for Exercise 6.")
Code cell 21
# Solution
# Exercise 6 - Log-Space Products
header("Exercise 6: log products")
p=np.full(1000,0.99)
prod_direct=np.prod(p)
log_prod=np.exp(np.sum(np.log(p)))
print("direct", prod_direct, "log", log_prod)
check_close("methods agree", prod_direct, log_prod)
Exercise 7: Rational Arithmetic
Use Decimal to compare exact decimal arithmetic with binary floats.
Code cell 23
# Your Solution
# Exercise 7 - learner workspace
# Write your solution here, then run the reference solution below to compare.
print("Learner workspace ready for Exercise 7.")
Code cell 24
# Solution
# Exercise 7 - Rational Arithmetic
header("Exercise 7: decimal arithmetic")
getcontext().prec=30
d=Decimal('0.1')+Decimal('0.2')
f=0.1+0.2
print("Decimal", d, "float", repr(f))
check_true("decimal exact", d == Decimal('0.3'))
check_true("float not exact", f != 0.3)
Exercise 8: Base Conversion
Convert integers to binary and recover them.
Code cell 26
# Your Solution
# Exercise 8 - learner workspace
# Write your solution here, then run the reference solution below to compare.
print("Learner workspace ready for Exercise 8.")
Code cell 27
# Solution
# Exercise 8 - Base Conversion
header("Exercise 8: base conversion")
nums=[3,7,13,42]
for n in nums:
bits=bin(n)[2:]
back=int(bits,2)
print(n,bits,back)
check_true("roundtrip", back==n)
Exercise 9: Mixed Precision Error
Compare dot products in float16 and float64.
Code cell 29
# Your Solution
# Exercise 9 - learner workspace
# Write your solution here, then run the reference solution below to compare.
print("Learner workspace ready for Exercise 9.")
Code cell 30
# Solution
# Exercise 9 - Mixed Precision Error
header("Exercise 9: mixed precision")
rng=np.random.default_rng(0)
x=rng.normal(size=1000)
y=rng.normal(size=1000)
d64=np.dot(x.astype(np.float64), y.astype(np.float64))
d16=np.dot(x.astype(np.float16), y.astype(np.float16)).astype(float)
print("float64", d64, "float16", d16, "abs error", abs(d64-d16))
check_true("float16 less accurate", abs(d64-d16)>1e-3)
Exercise 10: Finite Hash Buckets
Use modular hashing to map token ids into fixed buckets.
Code cell 32
# Your Solution
# Exercise 10 - learner workspace
# Write your solution here, then run the reference solution below to compare.
print("Learner workspace ready for Exercise 10.")
Code cell 33
# Solution
# Exercise 10 - Finite Hash Buckets
header("Exercise 10: finite buckets")
tokens=np.array([101,202,303,404,505])
buckets=(tokens*31+7)%16
print("buckets", buckets)
check_true("valid buckets", np.all((0<=buckets)&(buckets<16)))
check_true("deterministic", np.all(((tokens*31+7)%16)==buckets))