Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Just to get a feel for it, for your range y = 50, x = 0 gives r = 50 and y = 50, x = +/- 30 gives r ~= 58.3. You want an approximation good for +/- 0.1%, or +/- 0.05 absolute. That's a lot lower accuracy than most library sqrts do.</p> <p>Two approximate approaches - you calculate r based on interpolating from the previous value, or use a few terms of a suitable series. </p> <h2>Interpolating from previous r</h2> <p>r = ( x<sup>2</sup> + y<sup>2</sup> ) <sup>1/2</sup></p> <p>dr/dx = 1/2 . 2x . ( x<sup>2</sup> + y<sup>2</sup> ) <sup>-1/2</sup> = x/r</p> <pre><code> double r = 50; for ( int x = 0; x &lt;= 30; ++x ) { double r_true = Math.sqrt ( 50*50 + x*x ); System.out.printf ( "x: %d r_true: %f r_approx: %f error: %f%%\n", x, r, r_true, 100 * Math.abs ( r_true - r ) / r ); r = r + ( x + 0.5 ) / r; } </code></pre> <p>Gives:</p> <pre><code>x: 0 r_true: 50.000000 r_approx: 50.000000 error: 0.000000% x: 1 r_true: 50.010000 r_approx: 50.009999 error: 0.000002% .... x: 29 r_true: 57.825065 r_approx: 57.801384 error: 0.040953% x: 30 r_true: 58.335225 r_approx: 58.309519 error: 0.044065% </code></pre> <p>which seems to meet the requirement of 0.1% error, so I didn't bother coding the next one, as it would require quite a bit more calculation steps.</p> <h1>Truncated Series</h1> <p>The taylor series for sqrt ( 1 + x ) for x near zero is </p> <p>sqrt ( 1 + x ) = 1 + 1/2 x - 1/8 x<sup>2</sup> ... + ( - 1 / 2 )<sup>n+1</sup> x<sup>n</sup></p> <p>Using r = y sqrt ( 1 + (x/y)<sup>2</sup> ) then you're looking for a term t = ( - 1 / 2 )<sup>n+1</sup> 0.36<sup>n</sup> with magnitude less that a 0.001, log ( 0.002 ) > n log ( 0.18 ) or n > 3.6, so taking terms to x^4 should be Ok.</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