COMP2300

IEEE 754 example

Example: Converting to IEEE 754 single precision

Convert 0.085 into IEEE 754 single-precision format:

  1. Sign

    The number 0.085 is positive, so the sign bit = 0.

    (-1)0 = 1.

  2. Write the number 0.085 in base-2 scientific notation

    We have to factor 0.085 into a number in the range [1 <= n < 2] and a power of 2.

    0.085 = (-1)0  *  mantissa   * 2 power,    or:
    0.085 / 2power = mantissa.

    We can divide 0.085 by a power of 2 to get the mantissa:

    0.085 / 2-1 = 0.17
    0.085 / 2-2 = 0.34
    0.085 / 2-3 = 0.68
    0.085 / 2-4 = 1.36

    Therefore, 0.085 = 1.36 * 2-4

  3. The exponent

    The power of 2 is -4, and the bias for the single-precision format is 127. This means that the exponent = 127 + (-4) = 123, or 01111011bin

  4. Write the mantissa in binary form

    Because IEEE 754 assumes numbers to be normalised, we know that they are in the interval [1 <= n < 2] and therefore we don't have to store the leading 1.

    The fraction is therefore 0.36. Unfortunately, this is not a "nice" number. All we can do is approximate the value. The IEEE 754 single-precision format allows 23 bits for the fraction.

    Binary fractions look like this:

    0.1 = (1/2) = 2-1
    0.01 = (1/4) = 2-2
    0.001 = (1/8) = 2-3

    To approximate 0.36, we can say:

    0.36 = (0/2) + (1/4) + (0/8) + (1/16) + (1/32) +...
    0.36 = 2-2 + 2-4 + 2-5+...

    0.36ten ~ 0.01011100001010001111011 bin.

    The binary representation of the mantissa is therefore: 01011100001010001111011.

    It's important to notice that you will not get 0.36 exactly. This is why floating-point numbers may have errors when you put them in IEEE 754 format.

  5. Put the things together

    1 bit for the sign, followed by 8 bits for the exponent, and 23 bits for the fraction.

    The IEEE representation of 0.085 is therefore:

    Sign Exponent Mantissa
    Decimal 0 123 0.36
    Binary 0 01111011 01011100001010001111011