Computer Science › Section B · Data storage › Topic B.7
Storing fractions: floating-point notation
To store a value such as 6.5 or 0.3125, a computer must record where the radix point goes as well as the digits. Floating-point notation does this, and its limits explain why 0.1 + 0.2 is not exactly 0.3 on almost every computer.
Storing where the point goes
Two's complement fixes the radix point at the right end of the pattern, so it can only hold whole numbers. A fraction needs its digits and the position of its point, and the clever idea is borrowed from scientific notation. Writing \(-0.00042\) as \(-4.2 \times 10^{-4}\) splits the number into three parts: a sign, the significant digits, and an exponent saying how far to move the point. Floating-point notation stores exactly these three parts, in binary. Because the exponent can change, the point "floats": the same few digits can describe a huge number or a tiny one.
A small format to learn on
Real formats use 32 or 64 bits, but an 8-bit teaching format shows every idea with far less clutter. Bit 1 is the sign bit: 0 for non-negative, 1 for negative. Bits 2 to 4 are the exponent field, read in 3-bit excess notation with a bias of 4, so 000 to 111 mean −4 to 3. Bits 5 to 8 are the mantissa field, the significant digits, with the radix point imagined just to their left. The value is
where e is the exponent. A positive e moves the point e places right; a negative e moves it left.
Worked example: decoding a byte
Decode 10111010. 1. Split it into fields: sign 1, exponent 011, mantissa 1010. 2. Read the exponent in excess-4: 011 is 3, and 3 − 4 = −1. 3. Place the point to the left of the mantissa: .1010. 4. An exponent of −1 moves the point one place left, giving .01010. 5. Add the weights: \(\frac{1}{4} + \frac{1}{16} = \frac{5}{16} = 0.3125\). 6. The sign bit is 1, so the value is −0.3125. Check: \(0.625 \times 2^{-1} = 0.3125\), since .1010 is \(\frac{1}{2} + \frac{1}{8} = 0.625\).
Normalised form
Without a further rule, one value could have several patterns. The value 0.25, which is .01 in binary, could be stored as mantissa 0100 with exponent 0 (pattern 01000100), or as mantissa 1000 with exponent −1 (pattern 00111000). The convention is normalised form: the mantissa always starts with the leftmost 1 of the binary number. That makes each value's pattern unique and wastes no mantissa bits on leading zeros, so precision is as high as it can be. The value zero has no 1 to start from, so it gets a special pattern: all bits 0.
Worked example: encoding 6.5
1. Write 6.5 in binary: 110.1. 2. Copy the bits into the mantissa starting from the leftmost 1: 1101. 3. Find the exponent: the point sits to the left of the mantissa, at .1101, and must move 3 places right to give 110.1, so the exponent is 3. 4. Encode 3 in excess-4: 3 + 4 = 7, which is 111. 5. The value is positive, so the sign bit is 0. 6. The byte is 0 111 1101, or 01111101. Check by decoding: .1101 moved three places right is 110.1, which is 6.5. This happens to be close to the format's largest value, 01111111, which is 111.1 or 7.5.
Truncation error
Try storing 4.75, which is 100.11 in binary. It has five significant bits, but the mantissa holds four, so the last 1 is lost and the byte stores 100.1, which is 4.5. Losing part of a value because the mantissa is too short is called truncation error or round-off error. Real hardware usually rounds to the nearest storable value instead of simply chopping, which halves the worst error but cannot remove it. Notice also that storable values are spaced unevenly: with exponent 3 the step between neighbours is 0.5, while with exponent −1 it is \(\frac{1}{32}\).
Values that never fit
Some values cause error in any length of mantissa. As the topic "The binary system" showed, 0.1 is a repeating binary fraction, so every binary floating-point format stores a value slightly off. In Python, 0.1 + 0.2 displays 0.30000000000000004, and adding 0.1 ten times gives 0.9999999999999999 rather than 1.0. Errors this small rarely matter in a physics simulation, but they matter a great deal for money. A common remedy is to count in whole cents and store integers, which two's complement holds exactly: $19.99 becomes 1999 cents.
Order of addition matters
In the 8-bit format, add 6 + 0.25 + 0.25 from left to right. First, 6 + 0.25 = 110.01, which needs five bits, so it truncates back to 110.0, or 6. Adding the second 0.25 truncates again, and the total is 6. Now add the small values first: 0.25 + 0.25 = 0.5, then 6 + 0.5 = 110.1, which fits, so the total is the correct 6.5. Each small value alone was too small to register against 6; together they were not. The rule is to add small values together before adding them to large ones. For the same reason, compare floating-point results for closeness, not exact equality.
Real formats: IEEE 754
Almost all modern hardware follows the IEEE 754 standard. Single precision uses 32 bits: 1 sign bit, 8 exponent bits with a bias of 127 (not 128), and 23 mantissa bits. Double precision uses 64: 1, 11 with a bias of 1023, and 52. Unlike our toy, IEEE normalises to the form 1.fff and leaves the leading 1 unstored, so the precision is 24 and 53 bits, about 7 and 16 decimal digits. For example, −6.5 is \(-1.101 \times 2^2\), stored in single precision as sign 1, exponent 129 (10000001) and fraction 101 followed by twenty 0s. Some exponent patterns are reserved for zero, infinity and "not a number". Python's float is normally a double.
Common mistakes to avoid
Reading the exponent field as plain binary instead of excess notation shifts the point by 4 places too many. Copying bits into the mantissa from the radix point rather than from the leftmost 1 breaks normalised form: 0.25 should use mantissa 1000, not 0100. Forgetting that the mantissa is a fraction, placing its point after the first bit instead of before it, doubles every value. Testing whether two floating-point results are exactly equal fails on results such as 0.1 + 0.2 and 0.3. And storing money in floating point invites rounding errors; use whole cents.
Key takeaways
Floating-point notation stores a sign, an exponent and a mantissa, like binary scientific notation. In the 8-bit teaching format, the exponent is 3-bit excess-4 and the mantissa is a 4-bit fraction, normalised to start with its leftmost 1, with zero as all 0s. A mantissa that is too short causes truncation (round-off) error, and repeating fractions such as 0.1 can never be stored exactly. Add small values first, compare for closeness, and keep money as integers. IEEE 754 single and double precision give about 7 and 16 decimal digits.
Flashcards
Flashcard
Sign, exponent and mantissa, like scientific notation
Test your understanding
Question 1 of 5
In the 8-bit format (1 sign bit, 3-bit excess-4 exponent, 4-bit mantissa), what value does 11001100 represent?
Question 2 of 5
How is 2.25 stored in the same 8-bit format?
Question 3 of 5
Which value suffers a truncation error in the 8-bit format with a 4-bit mantissa?
Question 4 of 5
Why does 0.1 + 0.2 not give exactly 0.3 in Python?
Question 5 of 5
You must add one large floating-point value to many small ones. Which order is most accurate?
