title: c++ Floating point subtraction error and absolute values
body: <p>The way I understand it is: when subtracting two <code>double</code> numbers with double precision in c++ they are first transformed to a significand starting with one times 2 to the power of the exponent. Then one can get an error if the subtracted numbers have the same exponent and many of the same digits in the significand, leading to loss of precision. To test this for my code I wrote the following safe addition function:</p>
  
  <pre><code>double Sadd(double d1, double d2, int&amp; report, double prec) {
      int exp1, exp2;
      double man1=frexp(d1, &amp;exp1), man2=frexp(d2, &amp;exp2);
      if(d1*d2&lt;0) {
          if(exp1==exp2) {
              if(abs(man1+man2)&lt;prec) {
                  cout &lt;&lt; "Floating point error" &lt;&lt; endl;
                  report=0;
              }
          }
      }
      return d1+d2;
  }
  </code></pre>
  
  <p>However, testing this I notice something strange: it seems that the actual error (not whether the function reports error but the actual one resulting from the computation) seems to depend on the absolute values of the subtracted numbers and not just the number of equal digits in the significand...</p>
  
  <p>For examples, using <code>1e-11</code> as the precision <code>prec</code> and subtracting the following numbers:</p>
  
  <p>1) 9.8989898989898-9.8989898989897: The function reports error and I get the highly incorrect value 9.9475983006414e-14 </p>
  
  <p>2) 98989898989898-98989898989897: The function reports error but I get the correct value 1</p>
  
  <p>Obviously I have misunderstood something. Any ideas?</p>