Computer ScienceCore24 min read

Floating-Point Representation

Trading precision for range, and why 0.1 cannot be stored exactly

This topic appears in:

01

Why fixed point runs out

Fixed-point representation puts the binary point at an agreed position — say eight bits before and eight after. It is simple and exact for the values it covers, but the range is fixed: those eight fractional bits cannot represent anything smaller than 1/256, and the whole part cannot exceed 255.

Floating point solves this the way scientific notation does. Instead of fixing the point, it stores where the point goes alongside the digits. A number becomes a mantissa — the significant digits — and an exponent saying how far to shift the point.

The trade-off is unavoidable and is the point of the topic. For a fixed total number of bits, giving more to the exponent widens the range but leaves fewer for the mantissa, reducing precision. You cannot have both.

value = mantissa × 2^exponentboth stored in two's complementmore mantissa bits → greater PRECISIONmore exponent bits → greater RANGEfor a fixed word length, one costs the otherexactly the same idea as scientific notation, in base 2
mantissa
the significant digitsnormalised to sit just after the point
exponent
the shifthow many places to move the binary point
two's complement
the signed formatused for both parts at this level
02

Normalisation, and why it is required

The same value can be written many ways: 0.0101 × 2³ and 0.101 × 2² are the same number. Leaving that choice open wastes mantissa bits on leading zeros, which is precision thrown away, and it makes comparing two numbers awkward.

Normalisation fixes a single canonical form. The rule is stated in terms of the first two bits, which differ according to the sign, and it is worth memorising in exactly that form.

A positive normalised mantissa begins 0.1 — sign bit 0, then a 1. A negative normalised mantissa begins 1.0 — sign bit 1, then a 0. In both cases the first two bits must differ, and that is the quickest test.

normalised positive:0.1xxxxxnormalised negative:1.0xxxxxrule: the first two bits must be DIFFERENTto normalise:shift the mantissa left one placesubtract 1 from the exponentrepeat until the first two bits differshifting left costs one from the exponent each time
0.1…
positive normalisedsign 0, then a 1
1.0…
negative normalisedsign 1, then a 0
left shift
multiplies the mantissa by 2compensated by decreasing the exponent
Worked example

Normalise the floating-point number with mantissa 0.0011010 and exponent 0110 (both two's complement).

  1. Check the first two bits: 0.0 — they are the same, so it is not normalised.A positive number must begin 0.1 to be normalised.
  2. Shift the mantissa left one place: 0.011010, and decrease the exponent by 1 to 0101.Shifting left doubles the mantissa, so the exponent must fall by one to keep the value unchanged.
  3. First two bits are now 0.0 still — shift again: 0.110100, exponent 0100.Repeat until the leading bits differ.
  4. The mantissa now begins 0.1, so it is normalised. Mantissa 0.110100, exponent 0100.Two shifts were needed, so the exponent dropped by two from 0110 to 0100.

Mantissa 0.110100, exponent 0100

Floating point is base-2 scientific notation. The same reasoning that lets a decimal number be written as digits plus a power of ten applies here with powers of two — and the same fixed budget of digits limits the precision.

Shift left, exponent down

The direction trips people constantly. Shifting the mantissa left multiplies it by two, so the exponent must go down by one to leave the value unchanged. If you find yourself increasing the exponent while shifting left, the value has been multiplied by four rather than left alone.

03

Why 0.1 cannot be stored exactly

In binary, a fraction terminates only if its denominator is a power of two. One half, one quarter and three eighths are all exact. One tenth is not — 0.1 in binary is 0.0001100110011… repeating forever, exactly as one third is 0.333… in decimal.

Since only a fixed number of mantissa bits are available, the sequence must be cut short, and the stored value is very slightly wrong. This is why 0.1 + 0.2 does not print as 0.3 in most languages — the tiny errors accumulate and become visible.

The practical consequence is a rule worth carrying into any programming: never test floating-point values for exact equality. Test whether the difference is smaller than some small tolerance instead. And for money, use integer counts of the smallest unit rather than floating point at all.

Consequences to state in an exam

  1. Any value whose denominator is not a power of two recurs in binary and must be truncated.
  2. Rounding errors accumulate through repeated calculation.
  3. Never compare floating-point values with =; compare the difference with a tolerance.
  4. Store currency as an integer number of the smallest unit to avoid the problem entirely.
  5. More mantissa bits reduce but never eliminate the error.
  6. Overflow is a value too large for the exponent; underflow is one too small to represent.
04

Converting a stored number back to decimal

The reverse task — given a mantissa and exponent, state the decimal value — is asked at least as often as normalising, and it has a fixed method worth rehearsing.

Read the exponent first, remembering it is in two's complement so a leading 1 means it is negative. That tells you how far to move the binary point. Then read the mantissa, again in two's complement, with the point after the sign bit. Move the point by the exponent and evaluate the resulting binary fraction.

For a negative mantissa, the safest route is to negate it first — invert every bit and add one — evaluate the positive value, then apply the minus sign at the end. Trying to read place values directly from a two's complement fraction is where most errors happen.

Worked example

A number has mantissa 0.1101 and exponent 0011. State its decimal value.

  1. The exponent 0011 is positive: +3. So the binary point moves three places right.A positive exponent makes the value larger; a leading 0 confirms it is positive in two's complement.
  2. The mantissa begins 0.1, so it is positive and normalised. Moving the point: 0.1101 becomes 110.1.Shifting the point right by three places is the same as multiplying by 2³.
  3. Evaluate 110.1 in binary: 4 + 2 + 0 + 0.5.Place values left of the point are 4, 2, 1; the first place right of it is ½.
  4. The value is 6.5.A quick check: the mantissa is a bit over 0.8, and 0.8 × 8 = 6.6, so 6.5 is the right order.

6.5

Place values on both sides of the point

Left of the binary point the place values double outwards — 1, 2, 4, 8. Right of it they halve — ½, ¼, ⅛, 1/16. Writing those headings above the digits before evaluating takes a few seconds and removes almost every arithmetic slip in this topic.

Practice questions

5 questions · 16 marks · full working on every one

Try each one on paper first, then open the working. The marks are shown where they are actually awarded, because that is where they are actually lost.

Short questions

3 · 6 marks

Two marks each, in the style of the short-question section of the paper. Answer in two or three lines.

SQ1[2 marks]
State the effect on range and precision of increasing the number of bits allocated to the mantissa, keeping the total word length the same.
Model answer

Precision increases, because more significant bits are stored. Range decreases, because fewer bits remain for the exponent, so the binary point cannot be shifted as far.

Examiner tip. One mark each. The trade-off within a fixed word length is the whole question.

SQ2[2 marks]
State how you can tell whether a two's complement floating-point number is normalised.
Model answer

The first two bits of the mantissa must differ. A positive normalised number begins 0.1; a negative one begins 1.0.

Examiner tip. Giving the general rule and both cases secures both marks.

SQ3[2 marks]
Explain why normalisation is performed on floating-point numbers.
Model answer

It removes leading zeros (or leading ones for negatives) from the mantissa, so no bits are wasted and the maximum precision is retained for a given word length. It also gives each value a unique representation, which makes comparison and arithmetic straightforward.

Examiner tip. Two distinct reasons — precision and uniqueness — one mark each.

Solved numericals

1 · 4 marks

Full working, one step per line, with the marks shown where they are awarded.

N1[4 marks]
A floating-point number has mantissa 0.0001101 and exponent 0101. Normalise it, showing each step.
Full working
  1. The first two bits are 0.0, so the number is not normalised.Stating the test explicitly.[1]
  2. Shift left 1: 0.001101, exponent 0100. Shift left 2: 0.011010, exponent 0011.Each left shift reduces the exponent by one.[1]
  3. Shift left 3: 0.110100, exponent 0010.Three shifts were needed to bring a 1 next to the point.[1]
  4. Mantissa 0.110100, exponent 0010 — now normalised, since it begins 0.1.The exponent has fallen by 3 from 0101 to 0010.[1]

Mantissa 0.110100, exponent 0010

Exam questions

1 · 6 marks

Multi-part questions with a full mark scheme.

Q1[6 marks]
(a) Explain why the decimal value 0.1 cannot be stored exactly in binary floating point.
(b) State one consequence for a program that adds 0.1 to a running total ten thousand times.
(c) State how a programmer should test two floating-point values for equality, and why.
(d) Define overflow and underflow in this context.
Mark scheme
  1. (a) In binary a fraction terminates only if its denominator is a power of two. 0.1 is one tenth, so it recurs as 0.000110011… forever.The power-of-two condition is the reason.[1]
  2. The mantissa has a fixed number of bits, so the sequence is truncated and the stored value is slightly inaccurate.The truncation is what creates the error.[1]
  3. (b) The small error is repeated and accumulates, so the total drifts measurably from 1000.Accumulation is the point, not the single error.[1]
  4. (c) Test whether the absolute difference is less than a small tolerance, rather than using exact equality.The method must be stated concretely.[1]
  5. Because the stored values are approximations, two numbers that should be equal may differ in their last bits.The reason earns the second mark.[1]
  6. (d) Overflow: the value is too large for the exponent to represent. Underflow: the value is too small (too close to zero) to represent.Both are needed for the mark.[1]

(a) recurring binary, truncated; (b) accumulating drift; (c) compare against a tolerance; (d) too large / too small