0

I am using Microsoft Visual Studio Version 16.11.7, and have trouble understanding where this difference of values comes from, since the equations are mathematically the same. The values of a_res and Z are calculated through a complicated routine and are defined as doubles:

double test;
test = a_res - 1.0 + Z - (a_res - 1.0 + Z); // test is exactly 0.0
        
test = a_res - 1.0 + Z - a_res + 1.0 - Z; // test has the value of -1.1102230246251565e-16

It is important for my calculations do solve this Problem.

I am thanksful for any help

Uldoq
  • 1
  • https://stackoverflow.com/questions/588004/is-floating-point-math-broken – Hans Passant Nov 24 '21 at 09:12
  • Floating-point computations are inexact = prone to rounding errors. These errors generally depend on the order of performed arithmetic operations. If you change this order, you effectively change the resulting rounding errors, which is what you observe. – Daniel Langr Nov 24 '21 at 09:22
  • Without knowing what the range of values (or magnitudes) of `a_res` and `Z` are, it's virtually impossible to explain the observed difference - since the relative magnitude of each addition/subtraction performed in sub-expressions affects the order of operations needed to prevent propagation/growth of rounding errors. Explaining it in general (i.e. allowing for `a_res` and `Z` to have any value) would require writing a large part of a textbook on numerical analysis - and, unless someone has years to spend writing an answer, won't happen in the Q&A format here. – Peter Nov 24 '21 at 10:09

0 Answers0