2

According to Is Floating point addition and multiplication associative?, I know A+B+C would have different results with A+C+B because the result of (A+B)+C and (A+C)+B is different.

But how about if there are 2 numbers only? Does A+B have the same result as B+A?

ocomfd
  • 3,858
  • 2
  • 7
  • 17

1 Answers1

3

First, the question if A+B gives the same result as B+A is not about associativity, but commutativity. To answer your question, we need to know if the order of the operands for floating point addition and multiplication influences the result (as opposed to different groupings for the associativity question).

Now, IEEE-754 floating point addition and multiplication are commutative (but not associative, as you correctly pointed out in your question).

I should probably put an asterisk here, as different NaN's (actually, their so-called payloads) which are a part of IEEE-754 can violate commutativity, but I don't think you are interested in this particular case.

There is also another question on StackOverflow that actually answers the question of commutativity for floating-point operations – but I think that the confusion between the commutativity and associativity might be addressed for this question better.

Anton Menshov
  • 2,031
  • 8
  • 22
  • 37