Floating-point Operations Tutorial
Posted: Thu Sep 24, 2009 11:12 pm
This article discusses about the arithmetic operation likes addition, subtraction, multiplication and division of floating point numbers.
Floating point addition:
How floating addition works? Here are the steps involve in floating point addition.
Assume both the operands are in IEEE 754 floating point format. Performing floating point addition between ‘A’ and ‘B’
i.e. A = (-1) ^S x 2^Ae x 1.Am
S = sign bit,
Ae = Exponent of operand A i.e. E – 127 (assuming Single precision),
Am = Mantissa. Similarly for operand B (S, Be, Bm).
Therefore A + B = (Am x 2^Ae + Bm x 2^Be).
Step by step procedure explaining floating point addition:
Floating point subtraction:
How its works? Here are the steps involve in floating point subtraction.
Assume both the operands are in IEEE 754 floating point format. Performing floating point subtraction between ‘A’ and ‘B’
Single Precision, floating point representation. A = (-1)^ S x 2^Ae x Am
S = sign bit,
Ae = Exponent of operand A i.e. E – 127 (assuming Single precision),
Am = Mantissa. Similarly for operand B (S, Be, Bm).
Therefore A - B = (Am x 2^Ae - Bm x 2^Be).
Step by step procedure explaining floating point subtraction:
How it works? Here are the steps involve in floating point multiplication.
Assume both the operands are in IEEE 754 floating point format. Performing floating point multiplication between ‘A’ and ‘B’
Single Precision, floating point representation. A = (-1)^ (AS) x 2^Ae x Am
Step by step procedure explaining floating point subtraction
Floating point addition:
How floating addition works? Here are the steps involve in floating point addition.
Assume both the operands are in IEEE 754 floating point format. Performing floating point addition between ‘A’ and ‘B’
i.e. A = (-1) ^S x 2^Ae x 1.Am
S = sign bit,
Ae = Exponent of operand A i.e. E – 127 (assuming Single precision),
Am = Mantissa. Similarly for operand B (S, Be, Bm).
Therefore A + B = (Am x 2^Ae + Bm x 2^Be).
Step by step procedure explaining floating point addition:
- Aligning binary points of A and B
- Compare Ae and Be. Take the lager and compute exponent
- If Ae > Be right shift Bm that many positions to form Bm x 2^(Be – Ae).
If Be > Ae right shift Am that many positions to form Am x 2^(Ae – Be). - Compute the sum of aligned mantissa i.e.
Bm x 2^(Be – Ae) + Am (or) Am x 2^(Be – Ae) + Bm - If normalization of result is need, steps to perform
- If result looks like (0.001001…) then reduce the exponent by
left shifting the result. - If result looks like (101.01001…) then increase the exponent by
right shifting the result.
Continue above steps (a or b) until MSB (hidden bit in IEEE 754 standard) is 1.
- If result looks like (0.001001…) then reduce the exponent by
- Check result exponent.
- If larger than allowed exponent allowed return exponent overflow.
- If smaller than allowed exponent allowed return exponent underflow.
- If mantissa is equal to zero set exponent to zero.
Floating point subtraction:
How its works? Here are the steps involve in floating point subtraction.
Assume both the operands are in IEEE 754 floating point format. Performing floating point subtraction between ‘A’ and ‘B’
Single Precision, floating point representation. A = (-1)^ S x 2^Ae x Am
S = sign bit,
Ae = Exponent of operand A i.e. E – 127 (assuming Single precision),
Am = Mantissa. Similarly for operand B (S, Be, Bm).
Therefore A - B = (Am x 2^Ae - Bm x 2^Be).
Step by step procedure explaining floating point subtraction:
- Aligning binary points of A and B
- Compare Ae and Be. Take the lager and compute exponent difference Be – Ae (Be > Ae).
- If Ae > Be right shift Bm that many positions to form Bm x 2^(Be – Ae).(or) If Be > Ae right shift Am that many positions to form Am x 2^(Ae – Be).
- Compute the sum of aligned mantissa i.e.
Bm x 2^(Be – Ae) - Am (or) Am x 2^(Be – Ae) - Bm - If normalization of result is need, steps to perform
- If result looks like (0.001001…) then reduce the exponent by left shifting the result.
- If result looks like (101.01001…) then increase the exponent by right shifting the result.
Continue above steps (a or b) until MSB (hidden bit in IEEE 754 standard) is 1.
- Check result exponent.
- If larger than allowed exponent allowed return exponent overflow.
- If smaller than allowed exponent allowed return exponent underflow.
- If mantissa is equal to zero set exponent to zero.
How it works? Here are the steps involve in floating point multiplication.
Assume both the operands are in IEEE 754 floating point format. Performing floating point multiplication between ‘A’ and ‘B’
Single Precision, floating point representation. A = (-1)^ (AS) x 2^Ae x Am
Step by step procedure explaining floating point subtraction
- If any of the operands are equal to zero, return result as zero.
- Compute the sign: AS XOR BS
- Multiply the mantissa’s : Am x Bm, and round it to allowed number of mantissa bits.
- Compute the exponent of the result.
- Result exponent = biased exponent A + biased exponent B – bias;
- Normalize the result shift the mantissa, increment result exponent if needed
- Check the result exponent:
- If larger than maximum exponent allowed then return overflow.
- If smaller than maximum exponent allowed then return underflow.