Revise Computingrevisecomputing.co.uk
Free student revisionSee questionsFor schoolsSchool enquiry
Free revisionSee questionsFor schoolsSchool enquiry
Knowledge/A Level/H446/01 Computer systems/Data Types/Floating point arithmetic

Floating point arithmetic: addition and subtraction

Floating point arithmetic, positive and negative numbers, addition and subtraction

1.4.1 Data Types

Data Types — Note 8 of 10

Candidates should be able to perform addition and subtraction floating point arithmetic including addition and subtraction of both positive and negative numbers.

What you need to know

To add or subtract floating point numbers, their exponents must first be made the same so that the binary points line up. The mantissas are then added (or subtracted using two's complement) and the result is normalised.

What to know

  • Step 1: convert both numbers so their exponents are equal, usually by increasing the smaller exponent and shifting that mantissa RIGHT by the same number of places.
  • Step 2: add the mantissas. For subtraction, find the two's complement of the second mantissa and add it.
  • Step 3: normalise the result, adjusting the exponent.
  • Step 4: check whether the result fits in the bits available; if not, precision may be lost through truncation.
  • Shifting a mantissa right to match exponents can lose bits off the end, causing a loss of precision.
  • Alternative method accepted in exams: convert each number to denary-friendly fixed point binary, add or subtract, then convert back and normalise.

Exam technique

  • Tip:Keep the sign bit correct when shifting a negative mantissa right: new bits on the left must copy the sign bit (1 for negative).
  • Mistake:Adding the exponents together. The exponents are made EQUAL; only the mantissas are added.

Code examples

Adding 5 + 1.5 (8-bit mantissa, 4-bit exponent)
text
5   = 0.1010000 x 2^3
1.5 = 0.1100000 x 2^1  -> shift mantissa right 2: 0.0011000 x 2^3

  0.1010000
+ 0.0011000
= 0.1101000  x 2^3

Already normalised (starts 0.1): 0110.1 = 6.5
← Previous noteRepresentation and normalisation of floating point numbers in binary1.4.1 Data TypesNext note →Bitwise manipulation and masks: shifts, combining with AND, OR and XOR1.4.1 Data Types

Back to Data Types

Data Types

Note 8 of 10

  1. 1Primitive data types: integer, real/floating point, character, string and Boolean
  2. 2Represent positive integers in binary
  3. 3Sign and magnitude and two's complement to represent negative numbers
  4. 4Addition and subtraction of binary integers
  5. 5Represent positive integers in hexadecimal
  6. 6Convert positive integers between binary, hexadecimal and denary
  7. 7Representation and normalisation of floating point numbers in binary
  8. 8Floating point arithmetic: addition and subtraction
  9. 9Bitwise manipulation and masks: shifts, combining with AND, OR and XOR
  10. 10How character sets (ASCII and UNICODE) are used to represent text
Topic overview