FP

Floating Point Numbers

Binary representation of real numbers with fractional parts

Learning Objectives

By the end of this lesson, you will be able to:

  • Describe the format of binary floating-point real numbers
  • Convert binary floating-point real numbers into denary and vice versa
  • Normalize floating-point numbers and understand the reasons for normalization
  • Understand consequences of binary representation being an approximation in certain cases
  • Understand that binary representations can give rise to rounding errors
  • Differentiate between floating-point and fixed-point representations
  • Explain the benefits and limitations of floating-point representation

Key Terms

  • Floating-point representation - a method for representing real numbers in binary using a mantissa and exponent.
  • Mantissa (Significand) - the part of a floating-point number that contains the significant digits.
  • Exponent (Exrad) - the part of a floating-point number that indicates the power to which the radix is raised.
  • Normalization - the process of adjusting the mantissa and exponent so that the mantissa has a specific form for maximum precision.
  • Fixed-point representation - a method for representing real numbers where the position of the decimal point is fixed.
  • Radix - the base of a number system (2 for binary, 10 for decimal).
  • Overflow error - an error that occurs when a calculation produces a number exceeding the maximum value that can be stored.
  • Underflow error - an error that occurs when a calculation produces a number smaller than the minimum value that can be stored.

Real Number System

A real number is one with a fractional part. To write down a value for a real number, we can use a simple representation or we can use an exponential notation (scientific notation).

Why Exponential Notation Matters

The number 25.3 might alternatively be written as:

\(0.253 \times 10^2 \quad \text{or} \quad 2.53 \times 10^1 \quad \text{or} \quad 25.3 \times 10^0 \quad \text{or} \quad 253 \times 10^{-1}\)

In Exponential Notation, the number "234.56" can be written as "0.23456 × 103". This means that we only need to store numbers "0.23456" and "3". The number "0.23456" is called the mantissa or significand and the number "3" is called the exponent or Exrad.

Floating Point Representation

To represent real numbers in a computer, floating point representation is used. The general form is:

\[ \pm M \times R^E \]

A defined number of bits are used for what is called the significand or mantissa, \(\pm M\). The remaining bits are used for the exponent or exrad, \(E\).

Important Note

The radix, \(R\) is not stored in the representation; \(R\) has an implied value of 2 in binary systems. We need to store only 2 numbers: the mantissa and the exponent.

Binary Floating Point Example

Consider the binary number "10111". This could be represented as "0.10111 × 25" or "0.10111 × 2101". Here "0.10111" is the mantissa and "101" is the exponent.

Similarly, "0.00010101" can be written as "0.10101 × 2-3". Now the mantissa is "0.10101" and the exponent is "-3" (or "1101" in two's complement if using 4 bits).

Activity 13A: Understanding Floating Point Format

Difficulty: Easy • Estimated time: 5 minutes

Convert the binary number "110.11" into floating-point format. Identify the mantissa and exponent.

Binary number: 110.11
Move binary point to get mantissa between 0.1 and 1.0:
110.11 = 0.11011 × 2^3

Mantissa: 0.11011
Exponent: 3 (binary: 11)

Floating-point format: 0.11011 × 2^11

Check Your Understanding: Floating Point Representation

  • Mantissa (Significand) - contains the significant digits
  • Exponent (Exrad) - indicates the power to which the radix is raised
  • The radix has an implied value of 2 in binary systems
  • It is not stored in the representation
  • The formula is: ±M × 2^E

Converting Denary to Binary Floating-Point

To convert a denary number to binary floating-point, we convert both the integral and fractional parts separately, then combine and normalize.

Example: Convert 2.40625 to Binary Floating-Point

Step 1: Convert integral part (2)

2 in binary = \(10_2\)

Step 2: Convert fractional part (0.40625)

Multiply repeatedly by 2:
\(0.40625 \times 2 = 0.8125\) → 0
\(0.8125 \times 2 = 1.625\) → 1
\(0.625 \times 2 = 1.25\) → 1
\(0.25 \times 2 = 0.5\) → 0
\(0.5 \times 2 = 1.0\) → 1
So \(0.40625 = 0.01101_2\)

Step 3: Combine and normalize

\(2.40625 = 10.01101_2\)
Normalized: \(0.1001101 \times 2^2\)
Mantissa: 0.1001101, Exponent: 2 (\(10_2\) in binary)

Activity 13B: Convert 14.7 to Binary (Approximation)

Difficulty: Medium • Estimated time: 8 minutes

Convert 14.7 into 8-bit floating point format. Note that 0.7 is a repeating fraction in binary.

Step 1: Integral part (14) = 1110 in binary

Step 2: Fractional part (0.7) - repeating pattern:
0.7 × 2 = 1.4 → 1
0.4 × 2 = 0.8 → 0
0.8 × 2 = 1.6 → 1
0.6 × 2 = 1.2 → 1
0.2 × 2 = 0.4 → 0 (pattern repeats)
So 0.7 ≈ 0.1011 0110 0110... in binary

Step 3: Combine with 8-bit limit:
14.7 ≈ 1110.1011 in binary (8-bit approximation)
Step 4: Normalize:
1110.1011 = 0.11101011 × 2^4
Step 5: 8-bit representation:
Mantissa: 0.11101011 (8 bits)
Exponent: 4 = 00000100 (8-bit)
Final: 11101011 00000100

Repeating Fractions in Binary

Just as 1/3 = 0.333... is a repeating fraction in decimal, some fractions repeat in binary. For example, 0.7 (7/10) cannot be represented exactly in binary with a finite number of bits, leading to rounding errors in calculations.

Fixed-Point vs Floating-Point

In fixed-point representation, an overall number of bits is chosen with a defined number of bits for the whole number part and the remainder for the fractional part. The position of the decimal point is fixed.

Fixed-Point Representation

Example: Convert 4.75 to fixed-point binary representation.

4 converts to 100 in binary
0.75 converts to .11 in binary
So 4.75 = 100.11 in binary
With sign bit: 0100.11

For -4.75 (using two's complement):
0100.11 → 1011.01

Floating-Point Representation

Example: Convert 4.75 to floating-point binary representation.

4.75 = 100.11 in binary
Normalize: 0.10011 × 2^3
Mantissa: 0.10011
Exponent: 3 (011 in binary)

In memory (8-bit mantissa, 4-bit exponent):
01001100 0011

General Representation of Numbers

Type Format
Unsigned integer Integer
Signed integer Sign | Integer
Unsigned fixed point Integer | Fraction
Signed fixed point Sign | Integer | Fraction
Floating point Sign | Exponent | Mantissa

Normalization

To achieve maximum precision, we normalize a floating-point number. Precision increases with an increasing number of bits for mantissa, so optimum precision will only be achieved if full use is made of these bits.

Normalization Examples

Example 1: Normalize "10.11011" using 8 bits for mantissa and 8 bits for exponent.

10.11011 = 0.1011011 × 2^10
Mantissa: 0.1011011 (8 bits: 01011011)
Exponent: 2 (10 in binary, 8 bits: 00000010)
Stored as: 01011011 00000010

Example 2: Normalize "0.00000101011".

0.00000101011 = 0.101011 × 2^(-101)
Mantissa: 0.101011 (8 bits: 01010110)
Exponent: -5 (-101 in binary, two's complement: 11111011)
Stored as: 01010110 11111011

Normalizing Negative Numbers

To normalize negative numbers, first normalize the positive version of the number, then apply two's complement to the mantissa.

Example: Normalize "-1011" (binary)
1. Positive version: "1011" = "0.1011 × 2^100"
2. Normalized positive: Mantissa: 01011000, Exponent: 00000100
3. Two's complement of mantissa: 10101000
4. Final: Mantissa: 10101000, Exponent: 00000100
Notice first two bits are different (1 and 0).

Activity 13C: Normalization Practice

Difficulty: Medium • Estimated time: 10 minutes

Normalize the following binary numbers for floating-point representation with 8-bit mantissa and 4-bit exponent:

  1. 110.11
  2. 0.000101
  3. -101.01
  1. 110.11
    = 0.11011 × 2^11
    Mantissa: 01101100 (0.1101100)
    Exponent: 3 (0011)
  2. 0.000101
    = 0.101 × 2^(-11)
    Mantissa: 01010000 (0.1010000)
    Exponent: -3 (1101 in two's complement)
  3. -101.01
    Positive: 101.01 = 0.10101 × 2^11
    Mantissa (positive): 01010100
    Two's complement: 10101100
    Exponent: 3 (0011)

Check Your Understanding: Normalization

  • To achieve maximum precision/accuracy
  • To ensure unique representation for each number
  • To make the best use of available bits
  • To simplify hardware required for arithmetic
  • More mantissa bits → better precision/accuracy
  • More exponent bits → larger range of numbers
  • With fixed total bits, increasing one decreases the other

Floating-Point Problems

Storage of certain numbers is an approximation, due to limitations in the size of mantissa. This problem can be minimised when using programming languages that allow for double precision and quadruple precision.

Overflow Error

If a calculation produces a number which exceeds the maximum possible value that can be stored in mantissa and exponent, an overflow error will be produced. This could occur when trying to divide by a very small number or even 0.

Example:
Maximum exponent value: 127 (8-bit signed)
Calculating: 1.0 × 2^128
Result: Overflow error

Underflow Error

When dividing by a very large number, this can lead to a result which is less than the smallest number that can be stored. This would lead to an underflow error.

Example:
Minimum exponent value: -128 (8-bit signed)
Calculating: 1.0 × 2^(-129)
Result: Underflow error

Rounding Error Example

A student writes a program to output numbers using the following code:

X ← 0.0
FOR i ← 0 TO 1000
  XX + 0.1
  OUTPUT X
ENDFOR

The student is surprised to see that the program outputs: 0.0, 0.1, 0.2, 0.2999999, 0.3999999...

Explanation: 0.1 cannot be represented exactly in binary. 0.1 is represented by a value just less than 0.1. The loop keeps adding this approximate value to the counter until all accumulated small differences become significant enough to be seen.

Special Case: Zero

One of the issues of using normalized binary floating-point numbers is the inability to store the number zero. This is because the mantissa must be 0.1xxxx or 1.0xxxx which does not allow for a zero value. Special representations are needed for zero.

Key Takeaways

  • Floating-point representation uses mantissa and exponent: \(\pm M \times 2^E\)
  • Some decimal fractions (like 0.7) cannot be exactly represented in binary, causing rounding errors
  • Normalization maximizes precision by ensuring the mantissa uses all available bits effectively
  • Trade-off exists between precision (more mantissa bits) and range (more exponent bits)
  • Overflow occurs when a result is too large to be represented
  • Underflow occurs when a result is too small to be represented
  • Fixed-point representation has a fixed decimal point position, while floating-point can represent a wider range of values
  • Two's complement is typically used for representing negative mantissas and exponents
  • Understanding floating-point representation is crucial for numerical computing and avoiding precision errors

Question Bank

Marking Scheme
  • [2 marks] Definition: A method for representing real numbers in binary using a mantissa and exponent in the form ±M × 2^E
  • [3 marks] Reasons for use:
    • To represent very large and very small numbers efficiently
    • To handle numbers with fractional parts in binary systems
    • To provide a standardized way of representing real numbers in computers
Additional Notes for Slow Learners
  • Think of floating-point like scientific notation but for binary numbers
  • Instead of writing 123,000,000 we write 1.23 × 10^8 to save space
  • In binary, instead of 1101.11 we write 1.10111 × 2^3
  • This allows computers to handle numbers from very small (0.0000001) to very large (1,000,000) efficiently
Marking Scheme
  • [2 marks] Similarities:
    • Both can represent numbers with fractional parts
    • Both use binary representation
  • [4 marks] Differences:
    • Fixed-point has fixed decimal point position, floating-point has movable point
    • Floating-point can represent much larger range of values
    • Floating-point uses mantissa and exponent, fixed-point doesn't
    • Floating-point requires normalization, fixed-point doesn't
Additional Notes for Slow Learners
  • Fixed-point is like a ruler - the decimal point is always in the same place
  • Floating-point is like a slide rule - the decimal point can move where needed
  • Fixed-point: Good for money (always 2 decimal places)
  • Floating-point: Good for scientific calculations (very big or very small numbers)
Marking Scheme
  • [1 mark] 2.5 = 10.1 in binary
  • [1 mark] Normalization: 0.101 × 2^2
  • [1 mark] Mantissa: 010100000000 (12 bits)
  • [1 mark] Exponent: 0010 (4 bits)
Answer: 010100000000 0010
Additional Notes for Slow Learners
  • Step 1: Convert 2.5 to binary: 10.1 (2 = 10, 0.5 = 0.1)
  • Step 2: Move binary point: 0.101 × 2^2 (moved 2 places left)
  • Step 3: Mantissa needs 12 bits: 0.101 becomes 0.10100000000
  • Step 4: Exponent 2 in 4-bit binary: 0010
Marking Scheme
  • [1 mark] +6 = 110 in binary
  • [1 mark] Normalized positive: 0.110 × 2^11
  • [1 mark] Two's complement of mantissa: 1010
  • [1 mark] Exponent: 3 (0011)
  • [1 mark] Correct final answer
Answer: 1010 0011
Additional Notes for Slow Learners
  • First work with positive 6: binary 110 = 0.110 × 2^3
  • Mantissa for positive: 0.110 (needs 4 bits: 0110)
  • Two's complement of 0110: invert to 1001, add 1 = 1010
  • Exponent stays 3: 0011
  • Negative numbers: normalize positive first, then convert mantissa
Marking Scheme
  • [2 marks] 0.1 cannot be represented exactly in binary floating-point
  • [1 mark] It is represented by a value slightly less than 0.1
  • [1 mark] Repeated addition accumulates the error
  • [1 mark] After multiple additions, the error becomes visible
Additional Notes for Slow Learners
  • 0.1 in binary is like 1/3 in decimal - it goes on forever (0.0001100110011...)
  • Computers have to cut it off after a certain number of bits
  • This cut-off value is slightly different from 0.1
  • Adding this slightly wrong value 10 times gives 0.9999999 instead of 1.0
  • The tiny error adds up with repeated calculations
Marking Scheme
  • [1 mark each] For each valid advantage (up to 5 marks):
    • Improves precision by using all available mantissa bits
    • Provides unique representation for each number
    • Simplifies comparison of floating-point numbers
    • Makes arithmetic operations more accurate
    • Simplifies hardware implementation
    • Allows for easier error detection
Additional Notes for Slow Learners
  • Normalization is like writing numbers in standard form in math
  • Just like 123,000 = 1.23 × 10^5 is easier to work with
  • It gives every number a "standard" way of being written
  • This makes calculations more predictable and accurate
  • Without normalization, 0.001 × 2^3 and 1.0 × 2^0 would look very different but could be the same number
Marking Scheme
  • [1 mark] Correct conversion: 0.625 = 0.101 in binary
  • [1 mark] Normalization: Already normalized as 0.101 × 2^0
  • [1 mark] Correct floating-point representation
Answer: 0.101 × 2^0
or mantissa: 0.101, exponent: 0
Additional Notes for Slow Learners
  • 0.625 = 5/8 = 1/2 + 1/8 = 0.5 + 0.125
  • In binary: 0.5 = 0.1, 0.125 = 0.001, so 0.625 = 0.101
  • Since it's already between 0.1 and 1.0 in binary, it's normalized
  • Exponent is 0 because we don't need to move the binary point
Marking Scheme
  • [1 mark] Increased range of representable numbers
  • [1 mark] Reduced precision/accuracy (fewer bits for mantissa)
Additional Notes for Slow Learners
  • More exponent bits = can represent bigger/smaller numbers
  • But fewer mantissa bits = less accurate/precise numbers
  • It's like having a car with bigger fuel tank (range) but worse fuel efficiency (precision)
  • You need to choose based on what's more important for your application
Marking Scheme
  • [1 mark] Apply exponent: 0.011 × 2^2 = 1.1 in binary
  • [1 mark] Convert to denary: 1.1 in binary = 1.5 in denary
  • [1 mark] Correct answer: 1.5
Additional Notes for Slow Learners
  • Exponent 2 means move binary point 2 places right
  • 0.011 becomes 001.1 = 1.1 in binary
  • 1.1 in binary = 1 + 0.5 = 1.5 in decimal
  • Always convert the mantissa first, then apply the exponent
Marking Scheme
  • [2 marks] Overflow: Result is too large to be represented (exceeds maximum value)
  • [2 marks] Underflow: Result is too small to be represented (below minimum positive value)

Overflow example: Trying to store 1.0 × 2^128 when maximum exponent is 127
Underflow example: Trying to store 1.0 × 2^(-129) when minimum exponent is -128

Additional Notes for Slow Learners
  • Overflow = number too BIG (over the top)
  • Underflow = number too SMALL (under the bottom)
  • Like trying to pour 2 liters into a 1-liter bottle (overflow)
  • Or trying to measure 0.000001mm with a ruler that only goes down to 1mm (underflow)
  • Both are errors because the number can't be represented with the available bits