Beyond integers: fixed & floating-point
Integers can't hold 6.625 or 10²⁴. Here are the two ways a machine represents fractions and huge numbers — and one human-friendly code (BCD) along the way.
Built from first principles. Secondary topic — good general literacy; the MPU6050 itself reports integers, not floats.
Before you start
What you need first
- Base conversions — binary place value, including writing a number
in the form
1.xxx × 2ⁿ.
What you'll be able to do
- Encode a decimal digit in BCD.
- Read a fixed-point (Q-format) value.
- Lay out a number in IEEE-754 floating-point, and name its pitfalls.
A quick aside
Binary-Coded Decimal (BCD)
Sometimes we want to keep each decimal digit separate — for 7-segment displays, money, or a real-time-clock chip. BCD encodes each decimal digit in its own 4-bit nibble:
| Decimal | 9 | 4 | 7 |
|---|---|---|---|
| BCD | 1001 | 0100 | 0111 |
1010–1111
are invalid in BCD, so it wastes 6 of every 16 nibble values. Use BCD at the human
interface (displays, clocks, money); use plain binary everywhere else.Fixed-point: agree where the point sits
The simplest way to hold a fraction: fix an implied binary point at an agreed place. Qm.n means \(m\) integer bits, then the point, then \(n\) fraction bits. The fraction columns are negative powers of two:
| bit | b3 | b2 | b1 | b0 | • | b−1 | b−2 | b−3 | b−4 |
|---|---|---|---|---|---|---|---|---|---|
| weight | 8 | 4 | 2 | 1 | • | 0.5 | 0.25 | 0.125 | 0.0625 |
Read 0110.1010 as Q4.4
✏️ Try it yourself
In Q4.4 fixed-point, what decimal value does 00010110
represent?
0001 = 1; fraction 0110 = 0.25 + 0.125.
Answer: \(1 + 0.375 = 1.375\).
Floating-point: scientific notation, in binary
We want one format that holds both the mass of an electron (≈\(10^{-30}\) kg) and the mass of the Earth (≈\(10^{24}\) kg). The trick is the one scientists already use — scientific notation — done in base 2:
IEEE 754 — three fields, three jobs
A 32-bit float is just three fields stored side by side:
double is 1 + 11 + 52 = 64).| Field | Job |
|---|---|
| Sign (S) | 0 = positive, 1 = negative |
| Exponent | How big/small — the power of 2, stored biased by +127 |
| Mantissa | The digits after the implied leading "1." |
Store 5.0 in 32-bit IEEE 754
1.xxx × 2ⁿ:01, pad to 23 bits:Three things to watch out for
- Not every decimal is exact in binary float —
0.1 + 0.2 ≠ 0.3. Equality tests likex == 0.3often fail. - Float math is slower than integer math — often 5–20× on a CPU with an FPU.
- Many small microcontrollers have no hardware FPU, so floats are emulated in software — sometimes 100× slower than integers.
✏️ Try it yourself
(a) Encode decimal 47 in BCD. (b) Why does 0.1 + 0.2 == 0.3
return false in most languages?
0100, 7 → 0111, so 47 = 0100 0111.
(b) 0.1 and 0.2 have no exact binary representation; the stored approximations sum to ≈ 0.30000000000000004, not exactly 0.3.
Recap — the whole topic on one screen
| Format | Idea | Use it for |
|---|---|---|
| BCD | Each decimal digit in its own nibble | Displays, clocks, money |
| Fixed-point (Qm.n) | Implied binary point; integer hardware | Fast embedded fractions |
| Floating-point (IEEE 754) | Sign + biased exponent + mantissa | Huge dynamic range |