Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The derivative of <code>ArcTan(X, Sqrt(1-X*X))</code> with respect to X is <code>1/Sqrt(1-X*X)</code>. This goes to infinity as |X| goes to 1. Therefore, when X is near 1 or -1, any error in evaluation has a huge effect on the result. Thus, it is critical that evaluation minimize error in these cases.</p> <p>When X is near 1, evaluation of <code>1-X</code> has no error (in IEEE 754 or any good floating-point system, because the scale of the result is such that its least significant bit is at least as low as the least significant bit in 1 or X, so the exact mathematical result has no bits outside the available significand bits). Since <code>1-X</code> is exact, consider the effect of the error in <code>1+X</code> by considering the derivative of <code>ArcTan(X, Sqrt((1-X)*(1+X+e))</code> with respect to e, where e is the error introduced in the <code>1+X</code> operation. The derivative is, when X is near 1 and e is small, approximately -1/10. (Taking the derivative with Maple and substituting 1 for x yields <code>-1/(sqrt(4+2e)*(5+2e)</code>. Then substituting 0 for e yields -1/10.) Thus, the error in <code>1+X</code> is not critical.</p> <p>Therefore, evaluating the expression as <code>ArcTan(X, Sqrt((1-X)*(1+X))</code> is a good way to evaluate it.</p> <p>The situation is symmetric for X near -1. (<code>1+X</code> has no error, and <code>1-X</code> is not critical.)</p> <p>Conversely, if we consider the error in <code>X*X</code>, the derivative of <code>ArcTan(X, Sqrt(1-X*X+e))</code> with respect to e is, when X is near 1, approximately -1/(2*sqrt(e)*(1+e)), so it is large when e is small. So a small error in evaluating <code>X*X</code> will cause a large error in the result, when X is near 1.</p> <hr> <p>Ask Pascal Cuoq points out, when evaluating a function f(x), we are generally interested in minimizing the relative error in the final result. And, as I have pointed out, the errors that occur during calculation are generally relative errors in intermediate results due to floating-point rounding. I was able to ignore this in the above because I was considering the function when X is near 1, so both the intermediate values under consideration (1+X and X*X) and the final value had magnitudes near 1, so dividing the values by those magnitudes would not change anything significantly.</p> <p>However, for completeness, I examined the situation more closely. In Maple, I wrote <code>g := arctan(x, sqrt((1-x*x*(1+e0))*(1+e1))*(1+e2))</code>, thus allowing for relative errors e0, e1, and e2 in the calculations of <code>x*x</code>, <code>1-x*x</code>, and the <code>sqrt</code>, respectively, and I wrote <code>h:= arctan(x, sqrt((1-x)*(1+x)*(1+e0))*(1+e2))</code> for the alternative. Note that e0 in this case combines the three errors in <code>1-x</code>, <code>1+x</code>, and the multiplication of them; the full error term could be <code>(1+ea)*(1+eb)*(1+ec)</code>, but this is effectively <code>1+e0</code> with a larger possible range for e0.</p> <p>Then I examined the derivatives of these functions with respect to (one at a time) e0, e1, and e2 divided by abs(f(x)), where <code>f</code> was the ideal function, <code>arctan(x, sqrt(1-x*x))</code>. E.g., in Maple, I examined <code>diff(g, e0) / abs(f(x))</code>. I did not perform a full analytic evaluation of these; I examined the values for some values of x near 0 and near 1 and for values of e0, e1, and e2 at one of their limits, -2<sup>-54</sup>.</p> <p>For x near 0, the values were all of magnitude about 1 or less. That is, any relative error in calculation resulted in a similar relative error in the result, or less.</p> <p>For x near 1, the values with the derivatives of e1 and e2 were tiny, about 10<sup>-8</sup> or less. However, the values with the derivatives of e0 were hugely different for the two methods. For the <code>1-x*x</code> method, the value was about 2•10<sup>7</sup> (using x = 1-2<sup>-53</sup>). For the <code>(1-x)*(1+x)</code> method, the value was about 5•10<sup>-9</sup>.</p> <p>In summary, the two methods do not differ much near x = 0, but the latter method is significantly better near x = 1.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload