IEEE 754 Floating Point Arithmetic: Algorithms and Examples
Advertisement
IEEE 754 is a standard for floating-point arithmetic used in computers. It defines how to represent floating-point numbers and perform operations like multiplication, addition, and division with them. The standard ensures consistency across different computing systems. This tutorial covers algorithms and examples for each of the operations and describes decimal to floating-point conversion and vice versa.
IEEE 754 Standard Floating Point Numbers
This tutorial attempts to provide a brief overview of the IEEE Floating-point Numbers format with the help of simple examples, without going too much into mathematical detail and notations. By the end of this tutorial, you should be able to understand what floating-point numbers are and their basic arithmetic operations such as addition, multiplication & division.
An IEEE 754 standard floating-point binary word consists of a sign bit, exponent, and a mantissa as shown in the figure below. IEEE 754 single precision floating point number consists of 32 bits of which:
- 1 bit = sign bit(s)
- 8 = Biased exponent bits (e)
- 23 = mantissa (m)
The decimal value of a normalized floating-point number in IEEE 754 standard is represented as:
which can also be expressed as:
Note: “1” is hidden in the representation of IEEE 754 floating-point word, since it takes up an extra bit location and it can be avoided. It is understood that we need to append the “1” to the mantissa of a floating-point word for conversions and calculations.
For example, in Figure 1, the mantissa represented is 0101_0000_0000_0000_0000_000
. In actual fact it is (1.mantissa) = 1.0101_0000_0000_0000_0000_000
.
To make equation 1 clearer, let’s consider the example in Figure 1. Let’s try and represent the floating-point binary word in the form of equation and convert it to an equivalent decimal value.
Floating point binary word X1 =
- Sign bit (S1) = 0
- Biased Exponent (E1) = 1000_0001 (2) = 129(10)
- Mantissa (M1) = 0101_0000_0000_0000_0000_000
This can be expressed as:
IEEE 754 Standard Floating Point Conversions
Let’s look into an example for decimal to IEEE 754 floating-point number and IEEE 754 floating-point number to decimal conversion. This will make the concept and notations of floating-point numbers much clearer.
Decimal to IEEE 754 Standard Floating Point
Let’s take a decimal number, say 286.75, and represent it in IEEE floating-point format (Single precision, 32 bit). We need to find the Sign, exponent, and mantissa bits.
-
Represent the Decimal number 286.75 (10) into Binary format:
-
The binary number is not normalized. Normalize the binary number by shifting the decimal point such that we get a 1 at the very end (i.e., 1.m form).
We had to shift the binary point left 8 times to normalize it; the exponent value (8) should be added with bias. We now have the value of mantissa.
Note: In Floating-point numbers, the mantissa is treated as a fractional fixed-point binary number. Normalization is the process in which mantissa bits are either shifted right or to the left (add or subtract the exponent accordingly) such that the most significant bit is “1”.
-
, In our case e=8 (IEEE 754 format single precision).
(This is the bias value for single precision IEEE floating point format).
-
The biased exponent e is represented as:
. Convert this to binary and we have our exponent value:
-
We now have our floating-point number equivalent to 286.75:
Now with the above example of decimal to floating-point conversion, it should be clear as to what is mantissa, exponent & the bias.
IEEE 754 Standard Floating Point Standard to Decimal Point Conversion
Let’s reverse the above process and convert back the floating-point word obtained above to decimal. We have already done this in section 1 but for a different value.
IEEE 754 Standard Floating Point Arithmetic
Let us look at Multiplication, Addition, subtraction & inversion algorithms performed on IEEE 754 floating-point standard. Let us consider the IEEE 754 floating-point format numbers X1 & X2 for our calculations.
IEEE 754 Standard Floating Point Multiplication Algorithm
A brief overview of the floating-point multiplication algorithm is explained below:
and . Result
Where:
- => Sign bits of number & .
- : =>Exponent bits of number & .
- =>Mantissa bits of Number & .
Steps:
- Check if one/both operands = 0 or infinity. Set the result to 0 or inf. i.e. exponents = all “0” or all “1”.
- , the signed bit of the multiplicand is XOR’d with the multiplier signed bit of . The result is put into the resultant sign bit.
- The mantissa of the Multiplier () and multiplicand () are multiplied and the result is placed in the resultant field of the mantissa (truncate/round the result for 24 bits).
- The exponents of the Multiplier () and the multiplicand () bits are added and the base value is subtracted from the added result. The subtracted result is put in the exponential field of the result block.
- Normalize the sum, either shifting right and incrementing the exponent or shifting left and decrementing the exponent.
- Check for underflow/overflow. If Overflow set the output to infinity & for underflow set to zero.
- If () >= to then set the product to infinity.
- If is lesser than/equal to then set product to zero.
Floating Point Multiplication Example
Floating Point Multiplication is simpler when compared to floating point addition. Let’s try to understand the Multiplication algorithm with the help of an example. Let’s consider two decimal numbers:
Equivalent floating-point binary words are:
Steps:
-
Find the sign bit by xor-ing sign bit of A and B i.e. Sign bit => (0 xor 0) => 0
-
Multiply the mantissa values including the “hidden one”. The Resultant product of the 24 bits mantissas ( and ) is 48 bits (2 bits are to the left of the binary point).
If then left shift the binary point and add “1” to the exponent else don’t add anything. This normalizes the mantissa. Truncate the result to 24 bits. Add the exponent “1” to the final exponent value.
-
Find exponent of the result:
. Add the exponent value after normalization to the biased exponent obtained in step 2.
i.e., => exponent value.
Note: The normalization of the product is simpler as the range of and is between 1 - 1.9999999 and the range of the product is between (1 - 3.9999999). Therefore, a 1-bit shift is required with the adjust of exponent. So we have found mantissa, sign, and exponent bits.
-
We have our final result, i.e.,
If we convert this to decimal we get:
Floating Point Multiplication is simpler when compared to floating point addition we will discuss the basic floating point multiplication algorithm.
IEEE 754 Standard Floating Point Addition Algorithm
Floating-point addition is more complex than multiplication. A brief overview of the floating-point addition algorithm is explained below:
Steps:
- and can only be added if the exponents are the same i.e .
- We assume that has the larger absolute value of the 2 numbers. Absolute value of should be greater than absolute value of , else swap the values such that .
- The initial value of the exponent should be the larger of the 2 numbers, since we know exponent of will be bigger, hence Initial exponent result .
- Calculate the exponent’s difference i.e. .
- Left shift the decimal point of mantissa () by the exponent difference. Now the exponents of both and are the same.
- Compute the sum/difference of the mantissas depending on the sign bit and .
- If signs of and are equal () then add the mantissas
- If signs of and are not equal () then subtract the mantissas
- Normalize the resultant mantissa () if needed (1.m3 format) and the initial exponent result needs to be adjusted according to the normalization of mantissa.
- If any of the operands is infinity or if (), overflow has occurred, the output should be set to infinity. If () then it’s an underflow and the output should be set to zero.
- NaN’s are not supported.
Floating Point Addition Example
Equivalent floating-point binary words are:
Steps:
-
? Yes.
-
Result of Initial exponent
-
-
Shift the mantissa by so that the exponents are the same for both numbers.
-
Sign bits of both are equal? Yes. Add the mantissa’s
-
Normalization needed? No, (if Normalization was required for then the initial exponent result should be adjusted accordingly)
-
Result
in decimal = 10.3125.
-
If we had to perform subtraction, just change the sign bit of to “1”, Then we would have subtracted the mantissas, since sign bits are not equal.
NOTE: For floating point Subtraction, invert the sign bit of the number to be subtracted and apply it to floating-point Adder.
IEEE 754 Standard Floating Point Division Algorithm
Division of IEEE 754 Floating-point numbers ( & ) is done by dividing the mantissas and subtracting the exponents.
Steps:
- If divisor = zero then set the result to “infinity”, if both and are zero’s set it to “NAN”
- Sign bit .
- Find mantissa by dividing
- Exponent
- Normalize if required, i.e by left shifting the mantissa and decrementing the resultant exponent.
- Check for overflow/underflow
- If return overflow i.e. “infinity”
- If return underflow i.e. zero
Floating Point Division Example
Equivalent floating-point binary words are:
Steps:
-
-
-
Divide the mantissas
-
Result
in decimal = 7.5
Conclusion
The IEEE 754 standard for floating-point arithmetic provides a reliable framework for representing and manipulating real numbers in computer systems. The algorithms for multiplication, addition, and division are designed to maintain precision and handle a wide range of values. By adhering to the IEEE 754 standard, we can achieve accurate and efficient numerical calculations in a variety of scientific, engineering and financial applications. These concepts help developers and engineers working with numerical computations to optimize performance and prevent common pitfalls such as rounding errors and loss of significance.