Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The most precise way with floats is likely to compute both sin and cos using a single x87 instruction, <strong><a href="http://www.rz.uni-karlsruhe.de/rz/docs/VTune/reference/vc116.htm" rel="nofollow">fsincos</a></strong>. </p> <p><strike>However, if you need to do the computation manually, it's best to group arguments with similar magnitudes. This means the second option is more precise, especially when <code>cos_theta</code> is close to 0, where precision matters the most.</strike></p> <p>As the article <a href="http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html" rel="nofollow">What Every Computer Scientist Should Know About Floating-Point Arithmetic</a> notes:</p> <blockquote> <p>The expression x<sup>2</sup> - y<sup>2</sup> is another formula that exhibits catastrophic cancellation. It is <strong>more accurate</strong> to evaluate it as (x - y)(x + y).</p> </blockquote> <p>Edit: it's more complicated than this. Although the above is generally true, (x - y)(x + y) is <strong>slightly less accurate</strong> when x and y are of very different magnitudes, as the footnote to the statement explains: </p> <blockquote> <p>In this case, (x - y)(x + y) has three rounding errors, but x<sup>2</sup> - y<sup>2</sup> has only two since the rounding error committed when computing the smaller of x<sup>2</sup> and y<sup>2</sup> does not affect the final subtraction.</p> </blockquote> <p>In other words, taking x - y, x + y, and the product (x - y)(x + y) each introduce rounding errors (3 steps of rounding error). x<sup>2</sup>, y<sup>2</sup>, and the subtraction x<sup>2</sup> - y<sup>2</sup> also each introduce rounding errors, but the rounding error obtained by squaring a relatively small number (the smaller of x and y) is so negligible that there are effectively only two steps of rounding error, making the difference of squares more precise.</p> <p>So option 1 is actually going to be more precise. This is confirmed by dev.brutus's Java test.</p>
    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