Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Off the top of my head, here are two approaches:</p> <ul> <li>Use <a href="http://en.wikipedia.org/wiki/Bisection_method" rel="noreferrer">interval bisection</a>; you know that the error is no more than the current interval width, so stop iterating once the interval is smaller than required precision. This is very simple, but doesn't converge as quickly as other methods.</li> <li>Use an iterative method such as <a href="http://en.wikipedia.org/wiki/Newton%27s_method" rel="noreferrer">Newton's method</a> (also known as the <a href="http://en.wikipedia.org/wiki/Babylonian_method#Babylonian_method" rel="noreferrer">Babylonian method</a> when used to calculate a square root), and estimate the error after each iteration.</li> </ul> <p>To estimate the error, suppose we're trying to find <code>x0 = sqrt(y)</code>, so that <code>x0*x0 = y</code>. After each iteration, we have a candidate <code>x = x0 + d</code>, and we want to estimate the error <code>d</code>. If we square <code>x</code>, then we get</p> <pre><code>x*x = x0*x0 + 2*x0*d + d*d = y + 2*(x-d)*d + d*d ~= y + 2*x*d </code></pre> <p>discarding the <code>d*d</code> terms, which gets very small as <code>d</code> gets small. So we can estimate the error as</p> <pre><code>d ~= (x*x - y) / (2*x) = (x - y/x) / 2 </code></pre> <p>and stop iterating once this is smaller than the required precision.</p> <p>If you are using the Babylonian method, then this adds very little work to the iterative calculation, <code>x = (x + y/x) / 2</code>, so the result is something like</p> <pre><code>double sqrt(double y, double precision) { double x = y; // or find a better initial estimate for (;;) { double z = y/x; if (std::abs(x-z) &lt; 2*precision) return x; x = (x+z)/2; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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