In floating-point arithmetic, if two numbers have the same binary representation, then the result of any operation performed on these numbers should be the same, and equality comparisons using ==
should work as expected.
For example, if a and b are computed as 1.0/3.0
, they will indeed have the same binary representation in a standard floating-point system. Therefore, x
and y
calculated as follows shall be identical and the assertion shall hold.
double a = 1.0/3.0;
double b = 1.0/3.0;
double x = a*a/Math::Pi;
double y = b*b/Math::Pi;
assert(x==y);
My question is, will the sign of the number affect the accuracy of the results? Will the following be always true?
double a = 1.0/3.0;
double b = -1.0/3.0;
double x = a*a/Math::Pi;
double y = -(-b*b/Math::Pi);
assert(x==y);
How about this? Will the assertion hold?
double a = 1.0/3.0;
double b = 1.0/7.0;
double x = a-b;
double y = -(b-a);
assert(x==y);
I mainly work on x86/x64 machines. I thought C/C++/ASM shall have the same behaviour, so I tagged both C and C++.
3