Mục lục bài họcĐang ở d02-b2
Overflow, Round-off and Digitizing Analog 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.
Khi ngôn ngữ lưu số nguyên bằng số bit cố định, có những kết quả không vừa. Cố xử lý một số nằm ngoài phạm vi đã định là lỗi tràn số (overflow). Chuyện gì xảy ra tiếp theo tuỳ hệ thống: có ngôn ngữ dừng và báo lỗi, có ngôn ngữ chỉ giữ các bit thấp nên giá trị “quay vòng”, có ngôn ngữ chặn ở giá trị lớn nhất. Với số 8 bit không dấu quay vòng, $250+10=260$ được lưu thành $260-256=4$. Tràn số không phải lúc nào cũng làm đáp số cuối cùng sai: cùng hệ đó, $200+100$ ra 44, rồi 44 trừ 50 lại quay vòng về 250 — đúng đáp số thật. Đó là may mắn, không phải đúng đắn.
Số thực gây vấn đề thứ hai. Giữa hai số thực bất kỳ có vô số số khác, nhưng số bit cố định chỉ cho hữu hạn mẫu, nên đa số số thực được lưu bằng giá trị xấp xỉ gần nhất biểu diễn được. Phần chênh lệch là sai số làm tròn (round-off error). Số thập phân 0,1 không có biểu diễn nhị phân chính xác, nên cộng 0,1 mười lần trong nhiều ngôn ngữ ra 0,9999999999999999 chứ không đúng 1. Hai hệ quả đề hay hỏi: so sánh số thực bằng dấu = có thể cho kết quả bất ngờ, và sai số làm tròn có thể tích luỹ qua nhiều phép tính.
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.
Dữ liệu tương tự (analog) có giá trị thay đổi liên tục chứ không theo bậc rời: cao độ giọng nói, độ sáng trên bầu trời hoàng hôn, nhiệt độ trong ngày. Để lưu, thiết bị lấy mẫu tín hiệu — đo giá trị cách đều theo thời gian — và lưu mỗi lần đo bằng số bit cố định. Kết quả là một bản xấp xỉ số của tín hiệu tương tự, lại một ví dụ của trừu tượng hoá.
Hai lựa chọn quyết định chất lượng bản xấp xỉ. Tần số lấy mẫu (số mẫu mỗi giây) quyết định thời gian được chia mịn đến đâu; độ sâu bit (số bit mỗi mẫu) quyết định mỗi lần đo có bao nhiêu mức phân biệt. Tăng cái nào cũng làm bản số gần bản gốc hơn và làm tệp to hơn. Kích thước tệp tỉ lệ với cả hai:
$$\text{số bit} = \text{mẫu/giây} \times \text{bit/mẫu} \times \text{số kênh} \times \text{số giây}$$Một phút âm thanh stereo chất lượng CD dùng $44{,}100 \times 16 \times 2 \times 60 = 84{,}672{,}000$ bit, tức $10{,}584{,}000$ byte, khoảng 10,6 MB trước khi nén. Vì lấy mẫu chỉ giữ giá trị tại những thời điểm đã chọn, chi tiết nằm giữa hai mẫu bị mất, và không phép xử lý nào về sau khôi phục chính xác được.
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?
(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.
= 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.Đọc xong rồi — làm thử ngay
Bài tập của chương Unit 2 — Data gồm 14 câu trắc nghiệm và 5 đề tự luận. Đáp án hiện ngay khi chọn, miễn phí.