Sign of result of addition in floating point arithmetic

49 Views Asked by At

How can I determine the sign of the result of an addition in floating-point arithmetic?

I'm not sure if I've seen any info about it in standard IEEE-754, but my guess is: implement addition and subtraction of non-negative numbers and then change sign of result according to needs. Is it right?

1

There are 1 best solutions below

0
Eric Postpischil On

If the mathematical sum of two numbers is non-zero, the floating-point result has the same sign as the mathematical sum, even if the floating-point result is zero. The latter is due to IEEE 754-2019 4.3, first paragraph: “Inexact numeric floating-point results always have the same sign as the unrounded result.”

If the mathematical sum is zero, then, per IEEE 754-2019 6.3, third paragraph:

  • If the sum is +0 + +0, the sign is +, and, if the sum is −0 + −0, the sign is −.
  • Otherwise, if the rounding method is toward-negative, the sign is −.
  • Otherwise, the sign is +.