6

Floating point numbers are not exact due to its limited precision. My question is: is multiplication commutative in floating number computer calculation?

For example

double a = ..;
double b = ...;
double c = a * b;
double d = b * a;
if (c == d)
   cout << "Yes, great floating";
rici
  • 234,347
  • 28
  • 237
  • 341
user1899020
  • 13,167
  • 21
  • 79
  • 154
  • 2
    Do you mean "is floating point multiplication commutative?"? If so, this may be relevant: [Is multiplication always commutative in inexact floating point arithmetic?](http://stackoverflow.com/q/5007400/953482) – Kevin Dec 01 '15 at 18:35
  • 2
    If both `a` and `b` are NaN, you wouldn't necessarily get the same NaN if you switch the operands (but that's a pretty weird thing to rely on) – harold Dec 01 '15 at 18:47
  • 1
    @harold And anyway, if the result is `NaN` then `a * b` and `b * a` will compare unequal even if the two results have the same representation. – Pascal Cuoq Dec 01 '15 at 19:18

1 Answers1

10

According to Wikipedia, yes, float multiplication is commutative.

While floating-point addition and multiplication are both commutative (a + b = b + a and a×b = b×a), they are not necessarily associative. That is, (a + b) + c is not necessarily equal to a + (b + c).

Kevin
  • 74,910
  • 12
  • 133
  • 166