1

When reading about numerical methods, then subtraction cancellation is often a topic.

Simple example of cancellation:

a = 1;
b = 1e-16;
a-(a+b) = 0

hence we loss all information of b.

However, i rarely read about how to check for this problem. Could we not use a simple function to subtract number, which also warns us of problems? Example:

function c = subtract(a,b)
    c = a-b;
    if abs(c) < 1e-14*a
       disp('Warning: Low precision on subtraction');
    end
end

There are probably some deficiencies of this simple function - it was just my first idea. Anyway, would something like this work? And why is it not done? (i.e. i have never seen/heard about such checks before).

jsjq
  • 51
  • 4
  • 3
    How would that help? It doesn’t make the math more precise. What do the users of your program do with such a warning? It is your responsibility to use numerical methods that are stable. Any loss of precision then is acceptable and a normal part of floating-point computations. – Cris Luengo Oct 17 '18 at 13:54
  • "_hence we loss all information of `b`_". No you didn't, not as long as you keep `b` in scope. Other comments: (1) Not just floating-point computations loose precision, there are a huge number of physical/mathematical formulas which we learned at school and take for granted which are in fact the simplified _linearized_ version of a much more complex formulation. We are fine with it because if you can keep 99% accuracy for only 10% of the complexity it is a no brainer and the minor loss of information is accepted. (2) What would you change to your computation if your test raise a warning? – Hoki Oct 17 '18 at 14:04
  • Great comments from both of you. You are correct that this warning will not make the math more precise. However, could it be useful as a debugging tool, to find potential pitfalls? Getting this warning, maybe it would be possible to reformulate the given equation. – jsjq Oct 18 '18 at 08:50
  • @jsjq, probably the following tool would work for you: http://gappa.gforge.inria.fr/. It is capable of providing a maximum difference between result computed in IEEE single (or double) precision and a result computed with infinite precision for a known interval of input values. – VirtualVDX Oct 22 '18 at 14:24

0 Answers0