foundation15 min studyLesson 1 in Engineering

Bits, numbers, and the cost of approximation

Connect binary representation, rounding, tensor precision, and numerical error before choosing a data type.

On this page

What you’ll understand

  • Decode an unsigned binary number and calculate its range.
  • Distinguish storage precision from accuracy of a computed answer.
  • Choose numerical comparisons that reflect a problem's scale.

A computer does not store an abstract real number. It stores a finite pattern whose meaning depends on a format. That distinction explains why a model's weights consume memory, why a sensor reading can be rounded, and why two mathematically equivalent calculations can disagree. Start by asking what a stored pattern represents, then ask which errors the application can tolerate.

A bit pattern needs an interpretation

A bit has two states. An unsigned integer interprets a sequence of bits as a sum of powers of two: the rightmost position contributes one, the next two, then four. The pattern 1101 therefore denotes thirteen. With eight bits there are 256 distinct patterns, so unsigned values range from zero through 255. Signed integers, characters, and image pixels assign different meanings to patterns; bytes alone do not announce their interpretation.

A byte is eight bits. Distinguish decimal storage units, such as one megabyte meaning one million bytes, from binary units, such as one mebibyte meaning 1,048,576 bytes. If a tensor has one million elements and each stored element occupies two bytes, its raw element payload is two megabytes. Shape metadata, alignment, allocator overhead, gradients, and temporary buffers add costs outside that calculation. State the boundary before comparing memory estimates.

x=∑i=0b−1di2i,0≤x≤2b−1x=\sum_{i=0}^{b-1}d_i2^i,\qquad 0\leq x\leq2^b-1
For an unsigned b-bit integer, each digit d_i is either zero or one. x is a dimensionless integer; this equation does not describe signed or floating-point encodings.↔ Scroll wide equations horizontally.

Floating point trades exactness for range

Floating-point formats represent a sign, a scaled significand, and an exponent, with special encodings for exceptional values. Like a finite decimal expansion of one third, many finite binary fractions approximate rather than equal their intended real value. Decimal 0.1 is a familiar example. Printing a short decimal can hide this approximation; a neat display does not turn the underlying computation into exact decimal arithmetic.

Precision describes how finely a format distinguishes nearby values. Accuracy describes closeness to the desired answer, which also depends on the algorithm, input quality, and model assumptions. A highly precise calculation can accurately compute the wrong physical model. Conversely, lower precision may suffice for some neural-network operations after validation. Cancellation, repeated accumulation, overflow, and very different operand magnitudes deserve attention; selecting a smaller type is an experiment with consequences, not a universally safe optimization.

erel=∣x^−x∣∣x∣(x≠0)e_{\rm rel}=\frac{|\widehat{x}-x|}{|x|}\quad(x\neq0)
Relative error is dimensionless. x is an exact or trusted reference and x-hat is the approximation in the same units. Near zero, an application-specific absolute tolerance is also needed.↔ Scroll wide equations horizontally.

Turn tolerances into an engineering decision

For a measured length, an absolute error of one micrometre may matter more than a relative percentage. For values spanning many orders of magnitude, a relative tolerance may be more useful. Combine both intentionally when testing approximate outputs. Exact identifiers and counts should generally remain exact; a tolerance appropriate for a simulated temperature is inappropriate for deciding whether two user IDs are equal.

Keep a trusted baseline when changing precision. Compare representative outputs and difficult edge cases, record memory savings, and inspect whether downstream decisions change. The lab plots the exact real-valued model y=ax2y=ax^2. Change coefficient aa or input position xx and inspect the tangent: greater slope magnitude means small input changes produce larger output changes locally. This illustrates sensitivity, not binary rounding or an accelerator. Predict where an input approximation would matter most, then distinguish that mathematical observation from evidence about an actual precision change.

Budget an embedding table

An embedding table contains 50,000 rows of 256 elements. Compare four-byte and two-byte element payloads, ignoring all overhead.

  1. Multiply rows by width: 50,000 × 256 gives 12,800,000 stored values. The count has units of elements, not bytes.

  2. At four bytes per value the payload is 51,200,000 bytes, or 51.2 MB. At two bytes per value it is 25,600,000 bytes, or 25.6 MB.

  3. The payload halves, but this says nothing by itself about prediction quality or total training memory. Validate output differences against a reference and separately budget optimizer state and temporary allocations.

The result

The smaller representation saves 25.6 MB of raw payload. Accepting it requires numerical and task-quality checks, not just a successful memory allocation.

Explore the model

Interactive lab

Interactive lab · function

Sensitivity before rounding

Change coefficient a and input x for y=ax². Inspect the slope 2ax to predict sensitivity to a small input perturbation. The graph does not simulate discrete floating-point formats or measure numerical error.

Sensitivity before roundingThis lab uses only y = ax². Playback traces the curve within the fixed integration interval from 0 to x; it does not estimate numerical error or change the chosen point. Current readings: y at chosen x: 0.608; Tangent slope · 2ax: 1.35; Signed area · ax³/3: 0.182. All parameters are adjustable with the labeled controls below.−2.4−1.201.22.4−9−4.504.59xy(x, ax²)
y = ax²TangentSigned integral
y at chosen x
0.608
Tangent slope · 2ax
1.35
Signed area · ax³/3
0.182
t = 0 s

This lab uses only y = ax². Playback traces the curve within the fixed integration interval from 0 to x; it does not estimate numerical error or change the chosen point. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.

Look forA sensitive mathematical map can amplify input error; storage precision alone does not determine output accuracy.

Check your understanding

Two calculations differ by 0.000001. What can you conclude about their acceptability?

Try it yourself

A buffer holds 4,096 unsigned 16-bit values. Calculate its element payload and largest representable value.

Take a moment to make a prediction or work it out on paper. Open a hint when you need one.

Give me a hint

Convert bits per value into bytes, and count all available unsigned patterns including zero.

Compare with the solution

Sixteen bits occupy two bytes, so the payload is 8,192 bytes, exactly 8 KiB. There are 65,536 patterns; because zero is included, the maximum value is 65,535. Both claims assume packed values without container overhead.

Take it with you

  • A format defines the interpretation and limits of a bit pattern.
  • Precision, numerical accuracy, and application quality are different questions.
  • Memory estimates and comparison tolerances require explicit assumptions.

Read further

Original sources and deeper explanations to follow at your own pace.

Optional progress, saved in this browser when available. Revisit any lesson at any time.

Lesson not marked complete.