Binary Floating Point Operations – part one

floating_points

Have you ever wondered how floating point operations are actually represented on a computer?  No? Me neither 🙂 However I have been reading a book called ‘Mathematics and Physics for Programmers’ by Danny Kodicek and the first chapter covers this.  For one of the exercises it recommends writing code to carry out addition, subtraction, multiplication and division of IEEE floating point numbers that are represented as binary values.  This blog post is the first in a two-part series that describes my implementation of those operations using STL strings.

Representing a floating point value as a binary value

According to the IEEE 754 standard, single precision represents a floating point value that occupies 32 bits / 4 bytes and double precision represents a floating point value that occupies 64 bits / 8 bytes.  They are represented as the float and double POD types in C++ respectively.  I used the float type in the code accompanying these posts.  A single precision binary floating point format consists of 1 sign bit followed by 8 exponent bits and finally 23 mantissa bits.  The sign bit describes whether the value is positive (0) or negative (1).  The exponent describes the position of the decimal point and the mantissa contains the value.  As the significand (most significant bit) of the mantissa is always 1 it is ignored when storing the value (it is implicit during calculations), therefore allowing for 24 bits of precision.

Below is the binary representation of 12.125:32-bit binary float
In order to calculate the mantissa you calculate both the integer and fractional parts separately.

This involves converting the value 12 from base 10 to base 2

    \[1 \times 2^3 + 1 \times 2^2 + 0 \times 2^1 + 0 \times 2^0 = 1100\]

The method for converting the fractional part from base 10 to base 2 is slightly different.
first digit = 0.125 * 2 = 0.25; Integer part is 0 so digit is 0; carry 0.25
second digit = 0.25 * 2 = 0.5; Integer part is 0 so digit is 0; carry 0.5
third digit = 0.5 * 2 = 1.0; Integer part is 1 so digit is 1; Subtract 1.0 from result and carry remainder 1.0 – 1.0 = 0
fourth digit = 0; Stop and ignore

    \[0.125 = 001\]

We add 127 to the exponent to allow us to represent negative numbers.  Therefore the value of the exponent is in the range -128 -> 127

    \begin{align*} 12.125 &= 1100.001 = 1.100001 \times (e^3) \\ \text{exponent} &= 127 + 3 = 130 \\ &= 1 \times 2^7 + 0 \times 2^6 + 0 \times 2^5 + 0 \times 2^4 + 0 \times 2^3 + 0 \times 2^2 + 1 \times 2^1 + 0 \times 2^0 = 10000010 \\ \text{sign} &= 0 \dots \text{for positive value} \end{align*}

Mathematical Operation Setup

For addition, subtraction and multiplication, extracting the integer and fractional parts of the IEEE binary float in order to carry out the operations on each part individually simplifies the logic.  For the integer parts the right hand side of the values need to align, whereas for the fractional parts the left hand side needs to align.  For division the denominator is converted to a whole integer and there is no fractional part to worry about.

Addition

For binary addition, the rules are: 0 + 0 = 0, 1 + 0 = 1, 0 + 1 = 1, 1 + 1 = 10 (carry the 1).  Therefore we can use the bitwise XOR operator (^)  when iterating through the digits, taking into account whether we are carrying a 1 from the previous digit addition.  For example 14.625 + 2.5 = 17.125

    \begin{equation*} \frac{\begin{aligned} 1110&.101 \\ + \; 10&.1 \end{aligned}} {10001.001} \end{equation*}

Fractional Parts: 0.101 + 0.1 = 1.001:

Integer Parts: 1110.0 + 10.0 = 10000.0:

Subtraction

For binary subtraction the rules are: 0 – 0 = 0, 1 – 0 = 1, 0 – 1 = 1 (1 borrowed from next digit on top), 1 – 1 = 0.  In order to simplify the subtraction logic, the largest value is on the left hand side of the – operator and the smallest value is on the right hand side.  After the calculation is complete the result is negated if required.  Only when the top digit is 0 and the bottom digit is 1 do we need to borrow.  The bitwise XOR operator (^) can be used in the other situations.  For example 2.5 - 14.625 = -1 \times 12.125

    \begin{equation*} \frac{\begin{aligned} 1110&.101 \\ - \; 10&.1 \end{aligned}} {1100.001} \end{equation*}

Fractional Parts: 0.101 - 0.1 = 0.001:

Integer Parts: 1110.0 - 10.0 = 1100.0:

Summary

While using strings to demonstrate addition and subtraction isn’t the most optimal solution, it is a good way to visualize the operations.  In the next post I will cover multiplication and division as well as providing a link to the code.

Tagged , , , . Bookmark the permalink.

2 Responses to Binary Floating Point Operations – part one

  1. Alezis says:

    Hi there,
    Nice article, I use it to understand floating point mathematics, but I found error here. When you convert exponent 130 to binary representation you got somehow 10000011, however 130 is 10000010. As result 12.125 representation is 01000001010000100000000000000000. You picture here http://www.eoinoflynn.com/wp-content/uploads/2015/02/binary_float.png is representation of 24.25 decimal. Please correct me if I wrong.
    Just in case: I use also this to check results http://www.h-schmidt.net/FloatConverter/IEEE754.html

    • Eóin Ó'Flynn says:

      Hi Alezis,
      Thanks for your comment and for pointing out my error. You are absolutely right and I will update the diagram and the calculations with your correction.
      Eóin

Leave a Reply

Your email address will not be published. Required fields are marked *