Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is floating-point multiplication commutative? [duplicate]

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";
like image 724
user1899020 Avatar asked Mar 14 '23 00:03

user1899020


1 Answers

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).

like image 122
Kevin Avatar answered Mar 25 '23 08:03

Kevin