impera Open Impera

A levels Computer Science · Data representation

Floating point

1Why floating point?

With a fixed number of bits, fixed point has to choose between a large range (big numbers) and good precision (small fractions). Floating point lets the binary point "float", so it can store very large and very small numbers.

A floating-point number has two parts, like standard form:

$$\text{value} = \text{mantissa} \times 2^{\text{exponent}}$$

  • The mantissa holds the significant digits (and sign).
  • The exponent says how far to move the binary point.

In A-level questions, both are usually stored in two's complement, with the binary point just after the first bit of the mantissa.

Check yourself

What does the exponent in a floating-point number do?

  • Holds the significant digits

  • Counts the bits

  • Says how far to move the binary point

  • Stores the sign only

Show the answer

Says how far to move the binary point

The power of 2.

2Mantissa and exponent

A floating-point number with an 8-bit mantissa and a 4-bit exponent, both in two's complement. The binary point sits after the mantissa's first bit.

Worked example

A positive number

  1. Mantissa 0.1101000, exponent 0011.
  2. Exponent = 3, so move the point 3 places right: 0110.1000.
  3. Value = 4 + 2 + 0.5 = 6.5. (Check: 0.8125 × 2³ = 6.5.)
Worked example

A negative mantissa

  1. Mantissa 1.0110000, exponent 0010.
  2. Mantissa value = −1 + 0.25 + 0.125 = −0.625.
  3. Value = −0.625 × 2² = −2.5.
Worked example

A negative exponent

  1. Mantissa 0.1100000, exponent 1110 (= −2).
  2. Value = 0.75 × 2⁻² = 0.1875.
Check yourself

What is the value of mantissa 0.1010000 with exponent 0010?

  • 5

  • 2.5

  • 1.25

  • 0.625

Show the answer

2.5

0.625 × 4.

3Normalisation

A floating-point number is normalised when the first two bits of the mantissa are different:

  • positive numbers start 0.1;
  • negative numbers start 1.0.

Normalising gives the maximum precision for the mantissa bits, and gives each value one unique representation.

Worked example

Normalising

  1. Mantissa 0.0011010, exponent 0101 (5).
  2. Shift the mantissa 2 places left to get 0.1101000.
  3. Each left shift needs the exponent reduced by 1: 5 − 2 = 3.
  4. Normalised: 0.1101000, exponent 0011.
Check yourself

Is the mantissa 1.1010000 normalised?

  • No, a negative normalised mantissa starts 1.0

  • Yes, because it starts with 1

  • Only if the exponent is positive

  • Yes, because the first two bits are different

Show the answer

No, a negative normalised mantissa starts 1.0

First two bits must differ.

4Converting denary to floating point

Worked example

12.25 (8-bit mantissa, 4-bit exponent)

  1. 12.25 = 1100.01 in binary.
  2. Move the point to give 0.110001: this is 4 places left, so the exponent is 4.
  3. Mantissa 0.1100010, exponent 0100.
Worked example

−3.75

  1. 3.75 = 011.11. Two's complement of this: 100.01 (check: −4 + 0.25 = −3.75).
  2. Normalise to 1.0001: the point moves 2 places left, so the exponent is 2.
  3. Mantissa 1.0001000, exponent 0010.
Check yourself

What is the exponent when 5.5 (101.1) is normalised?

  • 5 (0101)

  • 3 (0011)

  • 1 (0001)

  • 2 (0010)

Show the answer

3 (0011)

0.1011 × 2³.

5Range, precision and errors

With a fixed total number of bits:

  • more mantissa bits → better precision;
  • more exponent bits → greater range.
ErrorMeaning
rounding errorthe number cannot be stored exactly
absolute errorthe size of the difference between the true and stored values
relative errorabsolute error ÷ true value
overflowthe number is too large for the exponent
underflowthe number is too small (too close to 0) to represent
Worked example

Storing 6.7

  1. With a 7-bit fraction in the mantissa and exponent 3, the closest value is 0.1101011 × 2³ = 6.6875.
  2. Absolute error = 6.7 − 6.6875 = 0.0125.
  3. Relative error = 0.0125 ÷ 6.7 = 0.19%.
Check yourself

Which part of a floating-point number should get more bits for better precision?

  • The exponent

  • Neither

  • The sign

  • The mantissa

Show the answer

The mantissa

Significant digits.

6Floating point versus fixed point

Fixed pointFloating point
simpler and faster to processslower; needs more complex hardware
smaller rangemuch larger range
constant absolute precisionconstant relative precision
good for money (pence)good for science and graphics

Real computers use the IEEE 754 standard: a 32-bit float has 1 sign bit, 8 exponent bits and 23 mantissa bits.

Because of rounding errors, two floating-point numbers that "should" be equal may not be: in Python, 0.1 + 0.2 == 0.3 is False. Compare with a small tolerance instead.

Check yourself

Why should money usually be stored as an integer number of pence rather than a float?

  • Floats are always much slower to add and subtract

  • Floats can introduce rounding errors with decimal fractions

  • Integers always have a larger range than floats do

  • Floats cannot store negative numbers such as refunds

Show the answer

Floats can introduce rounding errors with decimal fractions

0.1 is inexact.

Now test yourself

This lesson has a 8-question quiz and 5 exam-style questions with mark schemes in Impera. Free, with your progress saved.

Practise in Impera