Although this will work for 20 and 2, it is definitely not going to work in general: it will break for double values when the addition forces an increment of the exponent portion of the representation: the result will lose some precision.
Here is a simple example (in Java) illustrating the point:
double a = 1.0 / 3;
double b = 0.5;
double c = a + b;
double d = c - b;
System.out.println(a);
System.out.println(d);
System.out.println(a == d);
When you run it, this example prints the following:
0.3333333333333333
0.33333333333333326
false
The results become even more pronounced when the exponents of the values being added are different. For example, if you change b
's value from 0.5
to 100000
, the difference would be a lot bigger:
0.3333333333333333
0.3333333333284827
false
Changing b
to 1E15
gives
0.3333333333333333
0.375
false
Changing b
to 1E20
makes even the traces of the original a
disappear:
0.3333333333333333
0.0
false