I made a simple test to see how the round off error can be amplified in the computation. This code below
int main(int argc, char *argv[])
{
double a = 1.1237194475545454312;
double b = 123124.65464646545467897;
double c = 1.23212412412546464318794e22;
double A = (a + b) * c;
double B = a*c + b*c;
double diff = A - B;
std::cout << "diff: " << std::setprecision(10) << std::scientific << diff << std::endl;
return 0;
}
gives result:
diff: 2.7487790694e+11
However, when change c
to double c = 1.23212412412546464318794e23;
, the result is
diff: 0.0000000000e+00
What could possibly go wrong with the first evaluation?
The compiler is g++ 11.3.0