A Level Computer Science · Chapter 14 · Component 01: Computer systems
A computer stores 0.1 slightly wrong every time, and floating point explains why while letting the same few bits hold both tiny and enormous numbers.
This chapter shows how binary fractions, fixed point and floating point represent real numbers, using a two's complement mantissa and exponent. You will convert in both directions, normalise, add and subtract, and judge the trade-off between precision and range.
In this chapter · jump to a section
Start here
Your route through this chapter
By the end of this chapter you will be able to:
- convert between denary and binary fractions, and represent numbers in fixed point
- explain the mantissa and exponent of a floating point number, both in two's complement
- convert a floating point number to denary, for positive and negative mantissas and exponents
- normalise a floating point number, and explain why normalisation is used
- convert a denary number into normalised floating point
- add and subtract floating point numbers by matching their exponents
- explain the trade-off between precision and range, and describe rounding errors, overflow and underflow
Time: three sessions, about 4 hours, plus the programming task.
You need first: binary place values and two's complement from Number systems and binary arithmetic, and binary addition with carries.
The big picture
The big picture
Open Python and type 0.1 + 0.2. The answer comes back as 0.30000000000000004. Nothing is broken. The computer is doing exactly what it was designed to do, and this chapter explains why.
Whole numbers are easy to store in binary. Every integer has an exact pattern of bits. Real numbers are harder. Some, like 0.5 and 0.375, have short binary forms. Others, like 0.1, go on forever in binary, just as 1/3 goes on forever in denary as 0.333…
A computer has a fixed number of bits for each number, so it must stop somewhere. The bits it keeps give a value very close to 0.1, but not exactly 0.1. Most of the time the difference is far too small to matter. Occasionally it matters a great deal.
In 1991, during the Gulf War, a Patriot missile battery failed to intercept an incoming Scud missile. Its clock counted time in tenths of a second, stored in binary with a small error each tick. After 100 hours of running, the errors had added up to about a third of a second. At the speed of a Scud, that put the tracking off by hundreds of metres.
The opposite problem is range. Science needs both the mass of an electron, about 9 × 10⁻³¹ kg, and the mass of the Sun, about 2 × 10³⁰ kg. Storing both with a fixed number of digits after the point would need over sixty digits. Floating point solves this by storing the digits and the position of the point separately.
This is the idea of scientific notation, which you already use. The speed of light is 3.00 × 10⁸ m s⁻¹. The 3.00 holds the significant digits, and the power of 10 places the point. Floating point does the same in binary: a mantissa holds the bits and an exponent gives the power of 2.
Floating point is everywhere, even where you do not see it. Every pixel colour blended by a graphics card, every step of a weather forecast and every position in a physics engine is a floating point calculation. A modern graphics card can do trillions of them each second.
Because it is everywhere, its quirks matter. Programmers who understand how floats are stored write code that compares them safely, adds them in a sensible order and chooses integers when exactness matters. Those who do not are surprised by bugs that seem to come from nowhere.
The split between them is a trade-off. Give more bits to the mantissa, and each number is more precise. Give more bits to the exponent, and the range of sizes is wider. Every floating point format, from a phone's graphics chip to a supercomputer, makes this choice.
Section 1
Binary fractions and fixed point
Place values after the point
In denary, the places after the point are tenths, hundredths and thousandths. In binary they are halves, quarters, eighths and sixteenths. Each place is worth half the place to its left, continuing the pattern of the whole-number places.
So binary 0.1 is one half, 0.01 is one quarter and 0.11 is three quarters. Binary 101.101 is 4 + 1 + 0.5 + 0.125 = 5.625. Converting to denary is just adding the place values of the 1 bits.
Common mistake: reading fraction places as tenths. Binary 0.1 is one half, not one tenth, and binary 0.01 is one quarter. The places after a binary point halve each time: ½, ¼, ⅛, 1/16. They are never tenths or hundredths.
Converting a fraction to binary
To convert a denary fraction to binary, repeatedly double it. Each time, the whole-number part, 0 or 1, is the next bit after the point. Keep the fractional part and double again. Stop when the fraction reaches 0 or you run out of bits.
For 0.625: doubling gives 1.25, so the first bit is 1. Doubling 0.25 gives 0.5, so the next bit is 0. Doubling 0.5 gives 1.0, so the next bit is 1 and nothing remains. So 0.625 is 0.101 in binary.
Common mistake: assuming every decimal has an exact binary form. Only fractions whose denominators are powers of two end in binary. Doubling 0.1 never reaches zero: 0.1 is 0.000110011… with 0011 repeating forever, so any stored value is an approximation.
Why some fractions repeat
A denary fraction ends only if its denominator is made of 2s and 5s, the factors of ten. That is why 1/4 = 0.25 ends but 1/3 = 0.333… does not. Binary has only the factor 2, so a fraction ends in binary only if its denominator is a power of two.
So 3/8 ends in binary, as 0.011, but 1/10 does not, because 10 = 2 × 5. The factor 5 makes the digits repeat forever. This is a property of the number base, not a flaw in the computer: any base leaves some fractions repeating.
Fixed point
A fixed point format decides in advance where the binary point goes. An 8-bit format with 4 integer bits and 4 fraction bits stores 0110.1100 as the bits 01101100. The point itself is not stored; the program simply knows where it is.
Negative numbers use two's complement, as for integers. The first bit is worth minus its usual value. In the 4.4 format, the first bit is worth −8, so 1100.1000 is −8 + 4 + 0.5 = −3.5.
Worked example 1: a negative fixed point number
Find the denary value of 1011.0110, an 8-bit two's complement fixed point number with 4 fraction bits.
| Working | Reason |
|---|---|
| Place values: −8, 4, 2, 1, ½, ¼, ⅛, 1/16 | Two's complement: the first bit is negative |
| 1 bits at −8, 2, 1, ¼ and ⅛ | Read each bit against its place value |
| −8 + 2 + 1 + 0.25 + 0.125 | Add the place values of the 1 bits |
| Value: −4.625 | No unit: this is a pure number |
Worked example 2: denary to fixed point
Represent 5.625 in the same 8-bit fixed point format.
| Working | Reason |
|---|---|
| Integer part: 5 = 0101 | Four integer bits, with a 0 sign bit for a positive number |
| Fraction: 0.625 doubles to 1.25, 0.5, 1.0, so bits 101 | Repeated doubling gives the fraction bits |
| Pad to four fraction bits: 1010 | Unused fraction places are 0 |
| Result: 0101.1010, stored as 01011010 | No unit: this is a bit pattern |
The weakness of fixed point
Fixed point is simple and fast, because it is really integer arithmetic. But its range and precision are both fixed. The 4.4 format can store nothing above 7.9375, and nothing finer than 1/16. Storing tiny and huge values together is impossible without an enormous number of bits.
Fixed point has a long history in games and signal processing. Early games consoles had no floating point hardware at all, so their programmers stored positions and speeds in fixed point. Integer arithmetic was fast, and a known range meant no surprises. Many small microcontrollers today still work this way.
The choice of where to put the point is a design decision. More fraction bits give finer steps but a smaller maximum. More integer bits give a larger maximum but coarser steps. This is the same trade-off that floating point makes, except that fixed point makes it once, for every number.
Fixed point is still used where the range is known, such as money in pence, sensor readings and some audio processing. For general scientific work, floating point is the better tool.
Check yourself. What is 0.1 stored as in the 8-bit fixed point format with 4 fraction bits, and what is the error?
Show the answer
Doubling 0.1 gives bits 0001 in the first four places, so it is stored as 0000.0001, which is 0.0625. The error is 0.0625 − 0.1 = −0.0375, because the remaining bits are lost.
Section 2
Floating point: mantissa and exponent
Two parts
A floating point number has two parts. The mantissa holds the significant bits. The exponent says how many places to move the binary point. In this course both are in two's complement, so either can be negative.
The format used most often in exams has an 8-bit mantissa and a 4-bit exponent. The binary point in the mantissa sits just after its first bit. So the mantissa's place values are −1, ½, ¼, ⅛, 1/16, 1/32, 1/64 and 1/128.
The exponent is an ordinary 4-bit two's complement integer, with place values −8, 4, 2 and 1. It ranges from −8 to +7. The value of the whole number is the mantissa multiplied by 2 to the power of the exponent.
Derivation: the value of a two's complement mantissa
Write the mantissa bits as b₀.b₁b₂…b₇. The first bit has place value −1, and each later bit has half the value of the one before.
The exponent e is a two's complement integer. Multiplying by 2ᵉ moves the binary point e places to the right when e is positive, and to the left when e is negative.
So the largest mantissa is 0.1111111, which is 1 − 1/128, and the most negative is 1.0000000, which is exactly −1. Every mantissa lies between −1 and just under +1.
Common mistake: reading the first mantissa bit as +1. In two's complement the first mantissa bit is worth −1. A mantissa starting with 1 is negative. Reading the bit as +1 turns −6.75 into 9.25, so check the first bit before anything else.
Why base two?
Floating point in a computer uses powers of two, not ten, because the hardware is binary. Moving the binary point one place is a shift of the bits, which is very cheap. Multiplying by ten would need a real multiplication every time.
The price is that familiar decimal fractions are awkward. A tenth, a fifth and a hundredth all repeat forever in binary. Only fractions made of halves, quarters, eighths and so on are exact. This one fact explains most of the surprises people meet with floating point.
Mantissa and exponent sizes
A longer mantissa is not always better. Every bit of a register costs space in memory, and wider numbers take longer to move and calculate with. Designers choose the smallest format that gives enough precision and range for the job.
Graphics cards often use half precision, with just 16 bits, because the human eye cannot see tiny colour differences. Scientific simulations use 64 bits or more. Machine learning increasingly uses formats as small as 8 bits, because huge numbers of rough calculations beat fewer precise ones.
Reading a floating point number
There are two methods. The first is to find the mantissa's value, then multiply by 2 to the power of the exponent. The second is to move the binary point by the exponent, then read off the place values. Both give the same answer; the second is often quicker by hand.
Worked example 3: a positive number
Find the denary value of mantissa 01101100 and exponent 0011.
| Working | Reason |
|---|---|
| Exponent 0011 = 3 | A positive two's complement integer |
| Mantissa 0.1101100; move the point 3 places right: 0110.1100 | A positive exponent moves the point right |
| 0110.1100 = 4 + 2 + 0.5 + 0.25 | Add the place values of the 1 bits |
| Value: 6.75 | No unit: this is a pure number |
Worked example 4: a negative mantissa
Find the denary value of mantissa 10010100 and exponent 0011.
| Working | Reason |
|---|---|
| Exponent 0011 = 3 | The point moves three places right |
| 1.0010100 becomes 1001.0100 | The sign bit moves with the point |
| The first bit is now worth −8: −8 + 1 + 0.25 | The leading 1 keeps its negative weight |
| Value: −6.75 | No unit: this is a pure number |
The other method gives the same answer. The mantissa 1.0010100 is −1 + 0.125 + 0.03125 = −0.84375. Multiplying by 2³ = 8 gives −6.75.
Which method to use
Moving the point is usually quicker for exam questions, because the place values stay small and familiar. It also shows the binary form of the answer, which helps when you need to check it. Multiplying is better when the exponent is large, because moving the point many places is easy to miscount.
Whichever you use, write the exponent's denary value first. Most errors in reading floating point numbers come from the exponent, not the mantissa. A quick check of its sign, and of whether the point moves left or right, prevents most of them.
Formats in exam questions
Exam questions state the format in the question. The most common is an 8-bit mantissa with a 4-bit exponent, but 10-bit and 12-bit mantissas also appear. The method is identical for any size. Only the place values of the last mantissa bit and the range of the exponent change.
Negative exponents
A negative exponent moves the binary point to the left. The gaps it opens are filled with copies of the sign bit, so a positive mantissa gains leading 0s and a negative one gains leading 1s.
Worked example 5: a negative exponent
Find the denary value of mantissa 01100000 and exponent 1111.
| Working | Reason |
|---|---|
| Exponent 1111 = −8 + 4 + 2 + 1 = −1 | Two's complement: the first bit is −8 |
| Mantissa 0.1100000 = 0.5 + 0.25 = 0.75 | Add the place values of the 1 bits |
| Move the point 1 left: 0.0110000 | A negative exponent moves the point left |
| Value: 0.75 × 2⁻¹ = 0.375 | No unit: this is a pure number |
Common mistake: forgetting the exponent can be negative. The exponent is two's complement too, so 1111 is −1, not 15. Reading it as 15 moves the point the wrong way and gives an answer thousands of times too large.
Figure 14.1 lets you build a floating point number from its two parts. Change the mantissa and exponent, and watch how the value follows. Then switch on the wrong model to see what reading the sign bit as +1 does.
Reading a floating point number
- Mantissa value
- 0.84375
- Value
- 6.75
- Normalised?
- yes
Check yourself. Find the denary value of mantissa 01110000 and exponent 0010.
Show the answer
The exponent is 2, so the point moves two places right: 0.1110000 becomes 011.10000. That is 2 + 1 + 0.5 = 3.5.
Section 3
Normalisation
One number, many patterns
The same value can often be written in several ways. With an 8-bit mantissa, 6.5 could be 0.1101000 × 2³ or 0.0110100 × 2⁴ or 0.0011010 × 2⁵. Each extra leading 0 wastes a bit of the mantissa, and pushes a useful bit off the right-hand end.
Normalisation chooses the one form that uses the mantissa fully. A normalised positive mantissa starts 01. A normalised negative mantissa starts 10. In both, the first two bits differ, so there are no wasted copies of the sign bit.
Why normalise?
There are three reasons. First, it gives the greatest precision, because every mantissa bit after the sign carries information. Second, each value has exactly one normalised form, which makes comparing numbers simple. Third, it lets a format store the largest possible range of values for its size.
Common mistake: thinking a negative mantissa is normalised when it starts 11. Leading 1s in a negative mantissa are wasted copies of the sign, just like leading 0s in a positive one. A normalised negative mantissa must start 10.
What is lost without normalisation
Suppose 6.5 were stored as 0.0011010 × 2⁵. The two leading 0s fill two mantissa places, so only five bits are left for the number itself. Any value needing more bits than that would lose them off the end.
In the normalised form 0.1101000 × 2³, all seven places after the sign are available. So a normalised mantissa can hold values with two more bits of precision. Over many calculations, that difference keeps errors four times smaller.
How to normalise
To normalise, shift the mantissa left until its first two bits differ. Fill the right-hand end with 0s. Each shift left doubles the mantissa, so reduce the exponent by one for every place shifted, to keep the value the same.
Common mistake: changing the exponent the wrong way when normalising. Shifting the mantissa left multiplies it by 2 for each place, so the exponent must go down by one for each place. Increasing it instead quadruples the value rather than keeping it the same.
Worked example 6: normalising a positive number
Normalise the floating point number with mantissa 00011010 and exponent 0101.
| Working | Reason |
|---|---|
| Mantissa starts 000: two wasted leading 0s | Normalised positive mantissas start 01 |
| Shift left 2 places: 01101000 | Fill the right-hand end with 0s |
| Exponent 5 − 2 = 3, which is 0011 | Two shifts left, so subtract 2 |
| Result: 01101000 0011, which is 0.8125 × 8 = 6.5 | The value is unchanged; only the form has changed |
Worked example 7: normalising a negative number
Normalise mantissa 11101000 with exponent 0011.
| Working | Reason |
|---|---|
| Mantissa starts 111: two wasted leading 1s | Normalised negative mantissas start 10 |
| Shift left 2 places: 10100000 | Shift until the first two bits differ |
| Exponent 3 − 2 = 1, which is 0001 | Two shifts left, so subtract 2 |
| Result: 10100000 0001, which is −0.75 × 2 = −1.5 | The original was −0.1875 × 8 = −1.5, as required |
Normalisation in hardware
A floating point unit normalises after almost every operation. It counts the leading copies of the sign bit, shifts the mantissa left by that many places, and subtracts the count from the exponent. Special circuits do the counting in a single step, so normalising costs very little time.
IEEE 754 takes the idea one step further. A normalised binary number always has a 1 straight after the point, so the standard does not store it at all. This hidden bit gives one extra bit of precision for free. The exam format in this chapter stores every bit, which keeps the working visible.
Zero and the edges
Zero has no normalised form, because no shift will ever make its first two bits differ. Formats treat it as a special case, usually with every bit 0. A shift may also need the exponent to go below its minimum, −8 here; then the number cannot be normalised in this format.
Check yourself. Is the mantissa 11010000 normalised? If not, say how many places it must shift.
Show the answer
It is not normalised, because it starts 11. Shifting left one place gives 10100000, which starts 10, so one shift is needed and the exponent falls by one.
Section 4
Converting denary to floating point
The method for a positive number
To convert a positive denary number, first write it in binary, using repeated doubling for the fraction. Then move the binary point so that it sits just after a 0 sign bit, with a 1 straight after it. Count how far the point moved: that is the exponent. Finally pad the mantissa to its full length.
Worked example 8: a positive number
Convert 12.5 to normalised floating point with an 8-bit mantissa and a 4-bit exponent.
| Working | Reason |
|---|---|
| 12.5 = 1100.1 in binary | 8 + 4 for the integer part, and ½ for the fraction |
| Write it as 0.11001 × 2⁴ | Moving the point 4 places left gives a normalised form |
| Mantissa 01100100, padded to 8 bits | The first two bits, 01, show it is normalised |
| Exponent 4 = 0100; result 01100100 0100 | Converting back to denary gives the original value |
A second method
Some people prefer to find the exponent first. Find the power of two just above the number: for 12.5 it is 16, which is 2⁴. Then the exponent is 4, and the mantissa is 12.5 ÷ 16 = 0.78125. Converting 0.78125 to binary by repeated doubling gives 0.11001.
Both methods give the same result. The first is easier when the binary form is quick to find. The second is easier for numbers with long fractions, because it works with a value between ½ and 1 from the start.
The method for a negative number
For a negative number, convert its positive value first. Then find the two's complement of the mantissa, by flipping every bit and adding 1. Finally normalise again if needed, because a negative mantissa may now start 11.
Common mistake: making a number negative by changing only the first bit. That is sign and magnitude, not two's complement. Setting the first bit of 01100000 to 1 gives 11100000, which is −0.25, not −0.75. Flip every bit and add 1 instead.
Worked example 9: a negative number
Convert −0.375 to normalised floating point with an 8-bit mantissa and a 4-bit exponent.
| Working | Reason |
|---|---|
| +0.375 = 0.011 in binary, so mantissa 0.0110000 with exponent 0 | Start with the positive value |
| Two's complement: flip to 1.1001111, add 1: 1.1010000 | This mantissa now represents −0.375 |
| Starts 11, so shift left 1: 1.0100000, exponent −1 | Normalise: a negative mantissa must start 10 |
| Exponent −1 = 1111; result 10100000 1111 | Converting back to denary gives the original value |
Choosing the exponent
The exponent is simply the number of places the point must move to reach the normalised position. Moving it left, for a number of 1 or more, gives a positive exponent. Moving it right, for a number less than ½, gives a negative exponent.
For 0.15625, the binary form is 0.00101. The first 1 is in the third place after the point. Moving the point two places right gives 0.101, so the exponent is −2. The result is 01010000 1110, which is 0.625 × ¼ = 0.15625.
Truncating or rounding
When the mantissa has too few bits, the extra bits must be dealt with somehow. The simplest method, truncation, just drops them. It is easy to build but always errs in the same direction. For positive numbers, the stored value is always slightly too small.
Rounding to the nearest value is better. It looks at the first dropped bit: if that bit is 1, it adds one to the last kept bit. The error is then never more than half a step, and errors tend to cancel out over many calculations. IEEE 754 rounds to nearest by default.
When a number does not fit
Some numbers need more mantissa bits than there are. Then the extra bits are lost, and the stored value is slightly too small. For 0.1, the 8-bit mantissa holds 0.1100110 with exponent −3, which is 0.099609375.
Some numbers need an exponent that does not fit. With a 4-bit exponent, the largest power is 2⁷, so 1000 cannot be stored at all: that is overflow. A number such as 0.001 would need an exponent below −8: that is underflow.
Checking your conversion
Always convert your answer back. It catches the common slips: a wrong exponent sign, a mantissa that is not normalised, and a negative number made with sign and magnitude. A conversion that does not reproduce the original value has a mistake somewhere.
Check yourself. Convert −6.75 to normalised floating point with an 8-bit mantissa and a 4-bit exponent.
Show the answer
+6.75 is 110.11, which is 0.1101100 × 2³. The two's complement of the mantissa is 1.0010100, which already starts 10. So −6.75 is 10010100 0011.
Section 5
Floating point addition and subtraction
Why exponents must match
You cannot add 3 × 10² and 4 × 10⁵ by adding 3 and 4. First you must write both with the same power of ten. Floating point addition works the same way. The mantissas can only be added once both numbers have the same exponent.
Common mistake: adding mantissas without matching exponents. Adding the mantissas of 6.75 and 2.5 directly, with the larger exponent, gives 11.75 instead of 9.25. The bits being added are in different places, so the exponents must be matched first.
The method
The steps are always the same. First, find the difference between the exponents. Second, shift the mantissa of the number with the smaller exponent right by that many places, copying in the sign bit. Third, add the mantissas in two's complement. Fourth, normalise the result.
Shift the smaller number, not the larger. Shifting right loses bits off the end, and the smaller number has less to lose. Shifting the larger number would throw away its most significant information.
Common mistake: shifting the larger number instead of the smaller. The number with the smaller exponent is the one that must change, by shifting its mantissa right. Shifting the larger number left instead pushes its leading bits out and destroys its value.
Overflow into the sign bit
Adding two positive mantissas can carry a 1 into the sign position. The result then looks negative, but the true answer has simply grown past 1. The fix is to shift the result right one place, putting the carry back as a 0 sign bit, and add 1 to the exponent.
Worked example 10: adding two numbers
Add 01101100 0011 (6.75) and 01010000 0010 (2.5), giving a normalised result.
| Working | Reason |
|---|---|
| Exponents 3 and 2: shift B's mantissa right 1 place: 00101000 | Match the smaller exponent to the larger |
| 0.1101100 + 0.0101000 = 1.0010100 | Add the mantissas bit by bit |
| The carry reached the sign bit: shift right to 0.1001010, exponent 4 | Two positives cannot give a negative |
| Result: 01001010 0100, which is 0.578125 × 16 = 9.25 | This matches the denary sum of 6.75 and 2.5 |
Subtraction
To subtract, add the negative. Find the two's complement of the second mantissa, then add as before. The result may have leading 0s or 1s, so it often needs normalising at the end.
Worked example 11: subtracting
Calculate 01101100 0011 (6.75) minus 01010000 0010 (2.5).
| Working | Reason |
|---|---|
| Negate B's mantissa: 01010000 becomes 10110000 | Two's complement: flip the bits and add 1 |
| Shift right 1 to match exponent 3: 11011000 | Copy in the sign bit when shifting right |
| 0.1101100 + 1.1011000 = 0.1000100, discarding the final carry | Two's complement addition of the mantissas |
| Result: 01000100 0011, which is 0.53125 × 8 = 4.25 | This matches the denary difference of 6.75 and 2.5 |
Why the final carry is discarded
When a positive mantissa and a negative one are added, a carry often comes out of the sign position. In two's complement this carry is simply discarded, exactly as in integer subtraction. The bits left are the correct result.
This is different from the carry into the sign bit when two positives are added. That carry means the result is too big for the mantissa, so it must be kept by shifting right. The rule is simple: two numbers of the same sign can overflow the mantissa, but numbers of opposite signs cannot.
Subtracting nearly equal numbers
Subtraction has a hidden danger. When two nearly equal numbers are subtracted, their leading bits cancel. The result has few significant bits left, and normalising fills the rest with 0s that carry no real information.
This is called cancellation. Imagine two measured values, each correct to three significant figures, that agree in their first two figures. Their difference has only one significant figure left. Numerical programmers rearrange formulas to avoid subtracting nearly equal values wherever they can.
Guard bits
Hardware keeps a few extra bits during a calculation, called guard bits. When a mantissa is shifted right to match exponents, the bits that fall off the end are kept in these extra places. The result is rounded only once, at the end. This makes the final answer as accurate as the format allows.
Lost bits
Shifting right can push bits off the end. Add 0.25 to 64 in this format, and 0.25's mantissa must shift right eight places. Every bit is lost, so the result is still exactly 64. This is why adding many tiny numbers to a large total can lose them entirely.
Check yourself. Before adding numbers with exponents 0101 and 0010, which mantissa is shifted, and by how many places?
Show the answer
The exponents are 5 and 2, a difference of 3. The mantissa of the number with exponent 2 is shifted right 3 places, so both numbers then have exponent 5.
Section 6
Precision, range and errors
The trade-off
With a fixed number of bits, every bit given to the mantissa is a bit taken from the exponent. More mantissa bits mean more precision: values closer together, and smaller rounding errors. More exponent bits mean more range: bigger and smaller numbers.
Derivation: the range of an 8-bit mantissa and a 4-bit exponent
The largest mantissa is 0.1111111, and the largest exponent is 0111, which is 7.
The smallest positive normalised mantissa is 0.1000000, which is ½. The smallest exponent is 1000, which is −8.
So this format covers values from about 0.002 up to 127. Moving two bits from the mantissa to the exponent would raise the largest value to about 2 × 10⁹, but every value would be less precise.
Common mistake: thinking the mantissa sets the range. The exponent sets the range, because it decides how far the binary point moves. The mantissa sets the precision. Adding a mantissa bit halves the gaps between values; adding an exponent bit squares the range.
Figure 14.2 shares twelve bits between the mantissa and the exponent. Move bits from one to the other, and watch the range bar and the stored value change. Then switch on the wrong model, which swaps the roles of the two parts.
Precision against range
- Largest value
- 3.226e+4
- Smallest positive
- 0.00000763
- Stored value
- 3.125
Gaps between values
Floating point values are not evenly spaced. Near zero they are packed closely. Further out the gaps grow, because the same number of mantissa bits is stretched over a bigger interval. With an 8-bit mantissa, the gap near 1 is 1/128, but the gap near 100 is a whole 1.
Rounding errors
When a value has more bits than the mantissa can hold, the extra bits are lost. The stored value is then a little wrong. One such error is tiny, but a calculation that repeats many times can let errors build up, as in the Patriot clock.
This is why floating point numbers should never be compared with ==. A loop that adds 0.1 ten times may end at 0.9999999999999999 rather than 1.0. Instead, test whether the difference is smaller than a tolerance, such as abs(total − 1.0) < 1e-9.
Common mistake: testing floats for exact equality. Rounding errors mean two calculations of the same real value can give slightly different floats. Testing them with == can fail even when the maths is right. Compare the difference with a small tolerance instead.
Overflow and underflow
Both are limits of the exponent, not the mantissa. The mantissa can always be scaled to fit, but the exponent has only so many values. When a calculation needs an exponent beyond them, the format simply cannot express the answer, however many mantissa bits it has.
Overflow happens when a result is too large for the exponent. With a 4-bit exponent, anything above 127 overflows. Underflow happens when a result is too close to zero for the exponent, such as anything below 1/512 here. It is often rounded to zero.
Common mistake: confusing overflow with underflow. Underflow is not a number that is too negative. It is a number too close to zero to represent. A very large negative number, such as −1000 here, is overflow.
Absolute and relative error
There are two ways to measure an error. The absolute error is the difference between the stored value and the true value. Storing 0.1 as 0.099609375 has an absolute error of about 0.00039.
The relative error divides the absolute error by the true value. Here it is 0.00039 ÷ 0.1, about 0.4%. Floating point keeps the relative error roughly constant across its range, because the gaps grow in proportion to the numbers. That is the whole point of a floating binary point.
Fixed point behaves the other way round. Its absolute error is constant, because the gaps never change. So its relative error is tiny for large numbers but huge for small ones. A fixed point format with 1/16 steps stores values near 7.5 to within 1%, but 0.1 to within only 38%.
Floats in your programs
Most languages give you a float type that is an IEEE 754 double. In Python, every number with a decimal point is one. That is why 0.1 + 0.2 shows 0.30000000000000004: the nearest doubles to 0.1 and 0.2 add to a double that is not the nearest one to 0.3.
The order of operations can matter too. Adding a long list of small numbers to a large total, one at a time, loses the small ones. Adding the small numbers together first, then adding the total, keeps them. Sorting values from smallest to largest before summing reduces the error.
For whole numbers, integers are exact and usually faster. Use a float only when you need fractions or a huge range. When a value must be exact, such as money or a count, choose an integer or a decimal type.
A story about standards
Before 1985, every computer maker had its own floating point format. The same program could give different answers on different machines. William Kahan led the work on IEEE 754, which fixed the formats, the rounding rules and the special values for everyone.
The standard also defines infinity and NaN, which means "not a number". Dividing 1 by 0 gives infinity, and 0 divided by 0 gives NaN. These let a calculation carry on and report the problem, instead of crashing the program.
Real formats
Real computers use the IEEE 754 standard. A double-precision number has 64 bits: 1 sign bit, 11 exponent bits and 52 mantissa bits. It stores about 15 to 17 significant figures, and values up to about 10³⁰⁸. IEEE 754 uses a sign bit and a biased exponent rather than two's complement, but the trade-off is the same.
For money, floats are a poor choice. Errors of a fraction of a penny can add up across millions of transactions. Financial software usually stores whole pence as integers, or uses decimal types that store denary digits exactly.
Check yourself. A format has an 8-bit mantissa and a 4-bit exponent. Is 200 overflow, underflow or neither, and why?
Show the answer
It is overflow. The largest value this format can store is 127, so 200 would need an exponent of 8, which does not fit in four bits of two's complement.
Reference
Key algorithms and definitions
| Term or algorithm | What it means | Example | Cost or rule to learn |
|---|---|---|---|
| Binary fraction | Places after the point worth ½, ¼, ⅛ … | 0.101 = 0.625 | Convert a fraction by repeated doubling |
| Fixed point | The point is at an agreed position | 0110.1100 = 6.75 | Fixed range and fixed precision |
| Mantissa | The significant bits, in two's complement | 0.1101100 | First bit worth −1, point after it |
| Exponent | How far to move the point, in two's complement | 0011 = 3, 1111 = −1 | Positive moves right, negative moves left |
| Normalised | First two mantissa bits differ | 01… positive, 10… negative | Shift left and reduce the exponent |
| Addition | Match exponents, add mantissas, normalise | 6.75 + 2.5 = 9.25 | Shift the smaller number right |
| Subtraction | Add the two's complement of the second number | 6.75 − 2.5 = 4.25 | Normalise the result |
| Precision | How close neighbouring values are | Gap 1/128 near 1 | More mantissa bits, more precision |
| Range | Smallest to largest value | 1/512 to 127 | More exponent bits, more range |
| Overflow and underflow | Too large, or too close to zero | 1000 and 0.001 here | Both are exponent limits |
Programming task
Programming task: a floating point decoder
Aim
Write Python functions that decode an 8-bit mantissa and 4-bit exponent into a denary value, and encode a denary value into normalised floating point. Test them on chosen values, including ones that cannot be stored exactly or at all.
Design
Decoding uses the place values directly. A two's complement bit string is first turned into an integer. The mantissa is then divided by 2⁷, and the value is that times 2 to the power of the exponent.
Encoding chooses the exponent first. It halves or doubles the value until it lies between ½ and 1, or between −1 and −½, counting the steps. Then the mantissa is the value times 2⁷, rounded down to a whole number, written in two's complement.
Code
def from_twos(bits):
"""Value of a two's complement bit string."""
value = int(bits, 2)
return value - 2 ** len(bits) if bits[0] == "1" else value
def to_twos(value, width):
"""Two's complement bit string for a whole number."""
return format(value % 2 ** width, f"0{width}b")
def decode(mantissa, exponent):
return from_twos(mantissa) / 2 ** 7 * 2 ** from_twos(exponent)
def encode(value):
"""Normalised 8-bit mantissa and 4-bit exponent, or 'overflow' or 'underflow'."""
if value == 0:
return "00000000", "0000"
e, m = 0, value
while m >= 1 or m < -1:
m, e = m / 2, e + 1
while 0 <= m < 0.5 or -0.5 <= m < 0:
m, e = m * 2, e - 1
if e > 7:
return "overflow"
if e < -8:
return "underflow"
return to_twos(int(m * 2 ** 7 // 1), 8), to_twos(e, 4)
print(decode("01101100", "0011"))
print(encode(-0.375))
The two while loops find the exponent. The first halves values that are too big, and the second doubles values that are too small. Using // 1 rounds down, so bits beyond the eighth are dropped, just as the hardware would drop them.
Testing
| Test | Input | Expected output | Why this test |
|---|---|---|---|
| 1 | decode("01101100", "0011") | 6.75 | A positive number, from worked example 3 |
| 2 | decode("10010100", "0011") | −6.75 | A negative mantissa, from worked example 4 |
| 3 | decode("01100000", "1111") | 0.375 | A negative exponent, from worked example 5 |
| 4 | encode(12.5) | ("01100100", "0100") | A positive number, from worked example 8 |
| 5 | encode(−0.375) | ("10100000", "1111") | A negative number that needs normalising |
| 6 | encode(0.1) | ("01100110", "1101") | Not exact: decodes to 0.099609375 |
| 7 | encode(1000) | "overflow" | Too large for a 4-bit exponent |
Evaluation
The decoder agrees with every worked example, and the encoder's output always decodes back to the input when the input is exact. Test 6 shows the rounding error directly: 0.1 comes back as 0.099609375, an error of about 0.0004.
The encoder rounds down rather than to the nearest value, so its errors are always negative. An improvement would round to nearest, which halves the largest error. Another would accept the numbers of mantissa and exponent bits as parameters, to explore the trade-off.
Practice
Practice ladder
Rung 1: Recall and fluency
Q1. Write down the place values of the first four bits after a binary point.
Worked solution to Q1
They are ½, ¼, ⅛ and 1/16, which are 0.5, 0.25, 0.125 and 0.0625. Each place is worth half the place to its left.
Q2. State the first two bits of a normalised positive mantissa and of a normalised negative mantissa.
Worked solution to Q2
A normalised positive mantissa starts 01, and a normalised negative mantissa starts 10. In both cases the first two bits differ, so no bits are wasted on copies of the sign.
Q3. Define overflow and underflow for floating point numbers.
Worked solution to Q3
Overflow is a result too large in size for the exponent to represent. Underflow is a result too close to zero for the exponent to represent, which is often then stored as zero.
Rung 2: Application
Q4. Convert the 8-bit unsigned fixed point number 0011.1010, with 4 fraction bits, to denary.
Worked solution to Q4
Split the bits at the binary point, which sits after the fourth bit. The integer part 0011 is 3. The fraction bits 1010 are worth one half plus one eighth, which is 0.625. So the value is 3 + 0.625 = 3.625.
Q5. Give the denary value of mantissa 01011000 and exponent 0010.
Worked solution to Q5
The exponent 0010 is positive 2, so the binary point moves two places to the right. The mantissa 0.1011000 becomes 010.11000. Adding the place values of the 1 bits gives 2 + 0.5 + 0.25 = 2.75.
Q6. Find the value of mantissa 11000000 with exponent 0011, and give its normalised form.
Worked solution to Q6
The mantissa 1.1000000 is −1 + 0.5 = −0.5, so the value is −0.5 × 8 = −4. It starts 11, so shift left once to 10000000 and reduce the exponent to 2. The normalised form is 10000000 0010, which is −1 × 4 = −4.
Q7. Convert 0.6875 to normalised floating point with an 8-bit mantissa and a 4-bit exponent.
Worked solution to Q7
Repeated doubling gives 0.6875 = 0.1011 in binary. This is already of the form 0.1…, so the exponent is 0. The result is 01011000 0000, and 0.5 + 0.125 + 0.0625 = 0.6875 confirms it.
Rung 3: Analysis
Q8. Normalise mantissa 11110100 with exponent 0101, and check the value is unchanged.
Worked solution to Q8
The mantissa starts 1111, so it shifts left three places to 10100000. The exponent falls from 5 to 2, which is 0010. The original is −0.09375 × 32 = −3, and the result is −0.75 × 4 = −3, so the value is unchanged.
Q9. Add 01100000 0010 and 01000000 0001, giving a normalised result.
Worked solution to Q9
The values are 3 and 1. Shift the second mantissa right one place to 00100000, with exponent 2. Adding gives 0.11 + 0.01 = 1.00, which carries into the sign bit. Shift right and add 1 to the exponent: 01000000 0011, which is 0.5 × 8 = 4.
Q10. Explain why 0.1 cannot be stored exactly, and give the value stored with an 8-bit mantissa and a 4-bit exponent.
Worked solution to Q10
In binary 0.1 is 0.000110011…, with 0011 repeating forever, so any finite mantissa must cut it short. The normalised form keeps 0.1100110 with exponent −3. That stores 0.099609375, slightly less than 0.1.
Q11. Compare the largest values of a 12-bit format with an 8-bit mantissa and 4-bit exponent and one with a 6-bit mantissa and 6-bit exponent.
Worked solution to Q11
With a 4-bit exponent, the largest value is (1 − 2⁻⁷) × 2⁷ = 127. With a 6-bit exponent, the largest exponent is 31, so the largest value is (1 − 2⁻⁵) × 2³¹ = 2,080,374,784. Two exponent bits multiply the range enormously, but the 6-bit mantissa is less precise.
Rung 4: Stretch
Q12. Work out 01110000 0011 minus 01100000 0001, giving a normalised result.
Worked solution to Q12
The values are 7 and 1.5. Negate the second mantissa to 10100000, then shift it right two places to 11101000, matching exponent 3. Adding gives 0.1110000 + 1.1101000 = 0.1011000, discarding the carry. The result is 01011000 0011, which is 0.6875 × 8 = 5.5.
Q13. Explain why adding 01000000 1111 to 01000000 0111 leaves the larger number unchanged.
Worked solution to Q13
The numbers are 0.25 and 64, with exponents −1 and 7. Matching exponents means shifting the smaller mantissa right eight places. All seven of its bits after the sign fall off the end, so nothing is added and the result is still 64.
Q14. A student says that storing 0.1 with a 12-bit mantissa instead of an 8-bit one makes it exact. Explain whether they are right.
Worked solution to Q14
They are wrong. In binary 0.1 repeats forever, so no finite mantissa can hold it exactly. A longer mantissa keeps more of the repeating bits, so the error becomes smaller, but it never reaches zero.
Exam practice
Exam technique and worked answer workshop
Floating point questions are marked step by step. Write the exponent's denary value, then show the point moving or the mantissa's value, then the answer. A wrong final answer with clear working can still earn most of the marks.
Check two things in every answer. First, is the mantissa normalised: does it start 01 or 10? Second, does your answer convert back to the original value? Most lost marks come from a missed normalisation or a sign error.
For explanations, use the key terms precisely. Say "precision" for how close values are and "range" for how big or small they can be. Link each to the part that controls it: the mantissa for precision and the exponent for range.
Exam question 1 (4 marks · Paper 01 · about 5 minutes)
A floating point format uses an 8-bit two's complement mantissa and a 4-bit two's complement exponent.
(a) Find the denary value of mantissa 01011000 and exponent 0010. [2]
(b) Find the denary value of mantissa 10110000 and exponent 0001. [2]
Mark scheme for exam question 1
- M1 exponent 2, so the point moves two places right: 010.11000
- A1 2.75
- M1 exponent 1 with a negative mantissa: 1.0110000 is −0.625
- A1 −1.25
Exam question 2 (4 marks · Paper 01 · about 5 minutes)
(a) Normalise the floating point number with mantissa 00001101 and exponent 0110. [2]
(b) Give two reasons why floating point numbers are normalised. [2]
Mark scheme for exam question 2
- M1 mantissa shifted left three places: 01101000
- A1 exponent reduced by three to 0011, giving 01101000 0011
- B1 normalisation gives the greatest precision for the bits available
- B1 each value has exactly one normalised form, so values are easy to compare
Exam question 3 (5 marks · Paper 01 · about 7 minutes)
Using an 8-bit two's complement mantissa and a 4-bit two's complement exponent:
(a) Represent 13.25 in normalised floating point. [3]
(b) Represent −2.5 in normalised floating point. [2]
Mark scheme for exam question 3
- M1 13.25 written in binary as 1101.01
- M1 point moved four places: 0.110101 × 2⁴
- A1 01101010 0100
- M1 two's complement of the mantissa for +2.5, 01010000
- A1 10110000 0010
Exam question 4 (5 marks · Paper 01 · about 7 minutes)
Add the floating point numbers 01010000 0011 and 01100000 0001, giving the answer in normalised floating point. Show your working.
Mark scheme for exam question 4
- B1 the exponents are 3 and 1, a difference of 2
- M1 the second mantissa shifted right two places: 00011000
- M1 the mantissas added: 01010000 + 00011000
- A1 01101000 0011
- A1 this is 6.5, which equals 5 + 1.5
Exam question 5 (6 marks · Paper 01 · about 9 minutes)
A company is choosing a 16-bit floating point format for a weather sensor and for a banking system. Discuss how it should divide the bits between the mantissa and the exponent in each case, and whether floating point suits the banking system at all.
Mark scheme for exam question 5
Level 3 (5–6 marks): a clear explanation of how the mantissa controls precision and the exponent controls range. Both contexts are analysed with suitable choices. The answer recognises that floating point suits banking poorly and suggests an alternative, with accurate technical language throughout.
Level 2 (3–4 marks): a correct explanation of the trade-off applied to at least one context, but the analysis of the other context or of banking is limited.
Level 1 (1–2 marks): some relevant points, such as that more mantissa bits give more precision, with little application. The answer may lack structure.
0 marks: no response or no response worthy of credit.
Indicative content:
- more mantissa bits give greater precision, because values are closer together
- more exponent bits give a greater range of sizes
- with a fixed 16 bits, gaining one means losing the other
- a weather sensor measures values in a known, moderate range, such as temperature
- so the sensor should favour the mantissa for precise readings
- banking needs exact values; floating point cannot store many decimal amounts, such as 0.10, exactly
- rounding errors could add up over many transactions
- banking should store whole pence as integers, or use a decimal type
Annotated model answer: exam question 5
"The mantissa holds the significant bits, so more mantissa bits make each value more precise. The exponent decides how far the point moves, so more exponent bits give a wider range. With 16 bits in total, each bit given to one is taken from the other." (The trade-off, linked to each part.)
"A weather sensor reads values such as temperatures from −40 to 50 °C. This range is small and known, so few exponent bits are needed. Most bits should go to the mantissa, so readings are as precise as possible." (The first context analysed.)
"A banking system must store amounts exactly. Floating point cannot: 0.10 in binary repeats forever, so it is stored with a small error. Over millions of transactions these errors could add up to real money." (Why floating point suits banking poorly.)
"So the bank should not use floating point at all. It should store whole pence as integers, which are exact, or use a decimal type that stores denary digits." (A justified alternative.)
This answer reaches Level 3 and scores 6 marks. It explains the principle, applies it to both contexts and reaches a clear judgement.
Exam question 6 (5 marks · Paper 02 · about 7 minutes)
A program adds 0.1 to a variable total, starting from 0, ten times. It then tests if total == 1.0 and prints "done". The message is not printed.
(a) Explain why the message is not printed. [3]
(b) Rewrite the test so that the program prints "done". [2]
Mark scheme for exam question 6
- B1 0.1 has no exact binary representation, so it is stored with a small error
- B1 the error is added on every pass of the loop, so the errors build up
- B1 total ends very close to 1.0 but not exactly equal, so == is false
- M1 compare the difference with a small tolerance
- A1 if abs(total − 1.0) < 0.000001 then print("done")
Annotated model answer: exam question 6
"(a) The value 0.1 repeats forever in binary, so it can only be stored approximately. Each time the loop adds 0.1, it adds a value that is very slightly wrong, and the errors build up. After ten additions, total is extremely close to 1.0, such as 0.9999999999999999, but not equal to it. So the test total == 1.0 is false." (All three B marks: the cause, the build-up and the failed comparison.)
"(b) if abs(total − 1.0) < 0.000001 then print("done")" (The tolerance test earns both marks. Any sensible small tolerance is accepted.)
Exam question 7 (5 marks · Paper 01 · about 7 minutes)
This question uses a format with an 8-bit two's complement mantissa and a 4-bit two's complement exponent.
(a) Calculate the largest positive value it can represent. [2]
(b) Calculate the smallest positive normalised value it can represent. [2]
(c) State what happens when a calculation gives 0.001. [1]
Mark scheme for exam question 7
- M1 largest mantissa 0.1111111 with largest exponent 0111, which is 7
- A1 127
- M1 smallest normalised mantissa 0.1000000 with smallest exponent 1000, which is −8
- A1 1/512, which is 0.001953125
- B1 underflow: it is too close to zero to represent
Exam question 8 (6 marks · Paper 01 · about 9 minutes)
Subtract the floating point number 01000000 0010 from 01101000 0011. Give your answer in normalised floating point and show your working.
Mark scheme for exam question 8
- B1 the second mantissa negated in two's complement: 11000000
- B1 the exponents are 3 and 2, so the negated mantissa shifts right one place
- M1 the shift copies in the sign bit: 11100000
- M1 the mantissas added: 01101000 + 11100000, discarding the carry
- A1 01001000 0011
- B1 this is 4.5, which equals 6.5 − 2
Connections
Synoptic link
Floating point builds directly on Number systems and binary arithmetic, which taught two's complement and binary addition. Every step here, from negating a mantissa to adding with carries, uses those skills on a new kind of number.
Inside the processor, floating point arithmetic is done by dedicated hardware, as described in Processor architecture and performance. In programs, the choice between integer and real data types, and the danger of comparing reals with ==, appear in Programming constructs.
Synoptic question 1. A game stores positions in floating point. Near the origin, players move smoothly, but far away they jump in steps. Explain this using an 8-bit mantissa, comparing the gap between values with exponent 0 and exponent 7.
Answer to synoptic question 1
The gap between neighbouring values is one step in the last mantissa bit, 2⁻⁷ × 2ᵉ. With exponent 0 it is 1/128, but with exponent 7 it is 1, which is 128 times bigger. Far from the origin, positions can only change in whole units, so movement jumps.
Recap
Recap and next step
- Binary places after the point are worth ½, ¼, ⅛ …; fractions like 0.1 repeat forever, so they are stored approximately.
- A floating point number is a two's complement mantissa, with the point after its first bit, times 2 to the power of a two's complement exponent.
- Normalised mantissas start 01 if positive and 10 if negative; normalise by shifting left and reducing the exponent.
- To convert a negative number, convert the positive value, take the two's complement of the mantissa, then normalise.
- To add, shift the mantissa with the smaller exponent right until the exponents match, add, then normalise; subtract by adding the negative.
- Mantissa bits give precision and exponent bits give range; rounding errors, overflow and underflow follow from the fixed number of bits.
Next chapter: Arrays, records, stacks and queues moves from how single values are stored to how collections of values are organised.
For your exam board
OCR A Level Computer Science (H446): this chapter covers floating point in section 1.4.1, Data types, in Component 01, Computer systems: representing and normalising floating point numbers with a two's complement mantissa and exponent, converting to and from denary, and floating point addition and subtraction.
AQA A Level Computer Science (7517): fixed point and floating point with two's complement, normalisation, precision and range, and rounding, absolute and relative errors appear in the fundamentals of data representation section, assessed on Paper 2. AQA does not require floating point addition and subtraction.