Reputation: 3965
I am a little confused about rounding to nearest in floating point arithmetic. Let a, b, and c be normalized double precision floating point numbers. Is it true that a+b=b+a where + is correctly rounded to nearest floating point addition? My initial guess is yes this is always true, but I don't completely understand rounding to nearest. Could someone give an example of when a+b != b+a using floating point addition with rounding to nearest?
Upvotes: 2
Views: 1327
Reputation: 2611
As noted above, addition is commutative but not associative. The difference in rounding modes can be seen by running the following (MS Visual Studio) C++ code:
#include <iostream>
#include <float.h>
#pragma fenv_access(on)
using namespace std;
int main(int argc, char* argv[])
{
float a = 1.75f, b = 1e-6f;
cout.setf(ios::fixed,ios::floatfield);
cout.precision(7);
cout << "a = " << a << ", b = " << b << endl;
_controlfp_s(NULL, _RC_DOWN,_MCW_RC);
cout << "Result of a + b rounded down: " << a+b << endl;
cout << "Result of a - b rounded down: " << a-b << endl;
_controlfp_s(NULL, _RC_UP,_MCW_RC);
cout << "Result of a + b rounded up: " << a+b << endl;
cout << "Result of a - b rounded up: " << a-b << endl;
_controlfp_s(NULL, _RC_NEAR,_MCW_RC);
cout << "Result of a + b rounded to nearest: " << a+b << endl;
cout << "Result of a - b rounded to nearest: " << a-b << endl;
return 0;
}
Output:
a = 1.7500000, b = 0.0000010
Result of a + b rounded down: 1.7500010
Result of a - b rounded down: 1.7499989
Result of a + b rounded up: 1.7500011
Result of a - b rounded up: 1.7499990
Result of a + b rounded to nearest: 1.7500010
Result of a - b rounded to nearest: 1.7499990
Upvotes: 0
Reputation: 224596
Properly implemented IEEE-754 floating-point addition is commutative (a+b equals b+a) regardless of rounding mode.
The rounding mode affects how the exact mathematical result is rounded to fit into the destination format. Since the exact mathematical results of a+b and b+a are identical, they are rounded identically.
Upvotes: 3