Floating Point Addition & Subtraction (OCR A-Level Computer Science): Revision Notes
Floating Point Addition & Subtraction
Overview
Floating-point arithmetic is crucial for handling real numbers in computing, allowing for both very large and very small values. However, adding and subtracting floating-point numbers involves extra steps compared to integers due to the mantissa and exponent structure. It's important to understand how to correctly align and normalise floating-point numbers during arithmetic operations, especially when dealing with both positive and negative values.
Structure of Floating-Point Numbers
- Mantissa: Represents the significant digits of the number.
- Exponent: Determines the position of the binary point, effectively scaling the number.
- Sign Bit:
- 0 indicates a positive number.
- 1 indicates a negative number (using two's complement for the mantissa).
Steps for Floating-Point Addition and Subtraction
To perform addition or subtraction with floating-point numbers:
- Align the Exponents:
- If the exponents differ, shift the mantissa of the smaller exponent to the right until the exponents match.
- Add or Subtract the Mantissas:
- Perform the addition or subtraction on the aligned mantissas.
- Normalise the Result:
- Adjust the mantissa and exponent to ensure the mantissa is normalised (starts with 01 for positive or 10 for negative values).
- Handle Overflow or Underflow:
- Ensure the result fits within the given bit constraints for mantissa and exponent.
Examples
Example 1: Addition of Two Positive Floating-Point Numbers Add and
Step 1: Align Exponents:
Larger exponent:
Shift the mantissa of to match :
Step 2: Add Mantissas:
Step 3: Normalise:
The result is already normalised:
Final Answer:
Example 2: Subtraction of a Positive and Negative Number Subtract from
Step 1: Align Exponents:
Larger exponent:
Shift the mantissa of
Step 2: Subtract Mantissas:
Step 3: Normalise:
Final Answer:
Note Summary
Common Mistakes
- Misaligning Exponents: Ensure the mantissas are adjusted correctly to match the exponents before adding or subtracting.
- Ignoring Sign Bits: For negative numbers, remember to handle signs correctly (e.g., using two's complement).
- Skipping Normalisation: Always normalise the result to maintain proper floating-point representation.
Key Takeaways
- Floating-Point Arithmetic: Involves aligning exponents, performing arithmetic on mantissas, and normalising results.
- Positive & Negative Numbers: Be cautious with sign handling during subtraction and addition.
- Normalisation: Ensures precision and correct representation after calculations.