Binary Floating Point Operations – part two Previously we saw how to convert a float to a binary value as well as how to extract the relevant integer and fractional parts from that binary value.  We also saw the rules and logic for carrying out addition and subtraction.  Now we will look at multiplication and division of these binary values.

Multiplication

There is a quick check that you can do when multiplying before attempting any calculations.  If either of the values is 0.0f, the result is always 0.0f.  After checking for 0.0f, we extract the integer and fractional parts of each value as before.  Multiplying integer parts is a case of shifting the bits left (equivalent to the << operator).  Multiplying fractional parts is a case of shifting the bits right (equivalent to the >> operator).  For strings that essentially means appending a 0 for integers and pre-pending a 0 for fractions and then a simple addition.  As we are calculating the integer and fractional parts separately we need to multiply each fractional part by each integer part and add those results to the total.  In order to multiply an integer part by a fractional part we need to ensure that the length of the strings match by padding the integer with 0s at the beginning.  That way we don’t adjust the value. For example  Fractional Parts: :

Integer Parts: :

Combined parts: :

Division

Firstly a couple of checks are required.  A denominator of 0.0f is not allowed as the result is undefined.  Also a numerator of 0.0f results in 0.0f irrespective of the value of the denominator (as long as it isn’t 0.0f).  There is a little trick that can be used in some cases for division.  If the denominator consists purely of a fractional part, which is also a rational number, it can be converted from decimal format to fractional format (i.e. ).  Then the division becomes a simple case of multiplication: However that doesn’t work for irrational floating point values.  A better solution involves converting the denominator to a whole integer.  Then the numerator should be scaled by the same amount as the denominator.  For example  Once the denominator has been converted to a whole integer the logic then is:

Summary

As we can see there is a bit more work involved with multiplication and division of binary floating point numbers compared to addition and subtraction.  However the binary operations are more straightforward compared to their decimal counter parts.  With multiplication you are either shifting digits left or right.  With division you are simply comparing the size of the denominator to a sub-string from the numerator that is at least as long as the denominator.  The operation boils down to a few ‘>’ comparisons.  Exploring Binary has a great example of a ‘pencil and paper’ version of binary division for further reading.

Source Code

The source code can be found on Bitbucket

Tagged , , , . Bookmark the permalink.