Overflow, Round-off and Digitizing Analog Data

AP Computer Science Principles · Unit 2 — Data

Fixed bits create overflow and round-off

When a language stores integers in a fixed number of bits, some results do not fit. Attempting to handle a number outside the defined range is an overflow error. What happens next depends on the system: some languages stop with an error, some keep only the lowest bits so the value "wraps around", some saturate at the largest value. With 8-bit unsigned wrap-around, $250+10=260$ is stored as $260-256=4$. Overflow does not always make the final answer wrong: in the same system, computing $200+100$ gives 44, and subtracting 50 from 44 wraps back to 250 — the true answer. That is luck, not correctness.

Real numbers create a second problem. Between any two real numbers there are infinitely many others, but a fixed number of bits gives only finitely many patterns, so most real numbers are stored as the nearest representable approximation. The difference is a round-off error. The decimal 0.1 has no exact binary representation, so adding 0.1 ten times in many languages gives 0.9999999999999999 rather than exactly 1. Two consequences matter for the exam: comparing real numbers with = can give surprising results, and round-off errors can accumulate over many operations.

Analog data and sampling

Analog data have values that change smoothly rather than in discrete steps: the pitch of a voice, the brightness across a sunset, the temperature over a day. To store them, a device samples the signal — measures its value at regular intervals — and stores each measurement using a fixed number of bits. The result is a digital approximation of the analog signal, another example of abstraction.

Two choices control the quality of the approximation. The sampling rate (samples per second) decides how finely time is divided; the bit depth (bits per sample) decides how many distinct levels each measurement can take. Raising either one makes the digital version closer to the original and makes the file larger. File size is proportional to both:

$$\text{bits} = \text{samples per second} \times \text{bits per sample} \times \text{channels} \times \text{seconds}$$

One minute of CD-quality stereo sound uses $44{,}100 \times 16 \times 2 \times 60 = 84{,}672{,}000$ bits, which is $10{,}584{,}000$ bytes, about 10.6 MB before compression. Because sampling keeps only values at chosen instants, details between samples are lost, and no later processing can recover them exactly.

Lấy mẫu: đo cách đều theo thời gian, làm tròn về mức gần nhấttđường mờ: tín hiệu tương tự (analog) · bậc thang đậm: bản số hoákhoảng cách ngang = chu kỳ lấy mẫu · khoảng cách dọc giữa hai mức = độ phân giải (số bit/mẫu)
Số hoá tín hiệu tương tự. Mỗi chấm là một mẫu đã làm tròn về mức gần nhất. Lấy mẫu dày hơn thì bậc thang hẹp lại; dùng nhiều bit hơn mỗi mẫu thì các mức gần nhau hơn. Cả hai đều làm tệp lớn hơn.

Worked example: Tính kích thước tệp âm thanh khi đổi tham số

A 60-second stereo recording uses 44,100 samples per second and 16 bits per sample. An app converts it to mono, 22,050 samples per second, 24 bits per sample. (1) Find the original size in bytes. (2) Find the new size as a fraction of the original. (3) Which conversion step loses information that cannot be recovered?

Solution

(1) $44{,}100 \times 16 \times 2 \times 60 = 84{,}672{,}000$ bit, chia 8 được $10{,}584{,}000$ byte.

(2) Đi từng thừa số: tần số lấy mẫu giảm một nửa (×$\frac{1}{2}$), độ sâu bit từ 16 lên 24 (×$\frac{3}{2}$), từ 2 kênh xuống 1 (×$\frac{1}{2}$). Tích: $\frac{1}{2} \times \frac{3}{2} \times \frac{1}{2} = \frac{3}{8}$. Kiểm trực tiếp: $22{,}050 \times 24 \times 1 \times 60 / 8 = 3{,}969{,}000$ byte, và $3{,}969{,}000 / 10{,}584{,}000 = 0{.}375 = \frac{3}{8}$.

(3) Giảm tần số lấy mẫu bỏ đi một nửa số mẫu, và gộp hai kênh thành một bỏ đi sự khác biệt trái–phải: hai bước này mất thông tin vĩnh viễn. Tăng độ sâu bit lên 24 không thêm được chi tiết nào, vì giá trị gốc chỉ có 16 bit — tệp to hơn ở thừa số đó mà chất lượng không tăng.

Common mistake

Bẫy “tràn số luôn cho đáp số sai” và “tăng bit là tăng chất lượng”. Với số học quay vòng, một chuỗi phép tính có thể tràn ở giữa mà vẫn ra đúng đáp số cuối; đề thường hỏi giá trị tại từng bước. Còn khi chuyển đổi, tăng độ sâu bit của một bản ghi vốn đã số hoá không khôi phục được chi tiết đã mất. Bẫy thứ ba: nhầm sai số làm tròn (số thực xấp xỉ) với tràn số (vượt phạm vi).

Key takeaway

Chốt: số bit cố định → phạm vi giới hạn → tràn số; số thực chỉ lưu xấp xỉ → sai số làm tròn, có thể tích luỹ, so sánh bằng = không an toàn. Dữ liệu tương tự được lấy mẫu thành bản xấp xỉ số; kích thước = mẫu/giây × bit/mẫu × kênh × giây; chi tiết giữa các mẫu mất vĩnh viễn.

← Bits, Bytes and Binary Numbers · All AP Computer Science Principles lessons · Practice AP Computer Science Principles questions (free, instant scoring) · Data Compression: Lossless and Lossy →