Why is numpy's sine function so inaccurate at some points?
<p>I just checked <code>numpy</code>'s <code>sine</code> function. Apparently, it produce highly inaccurate results around pi. </p>

<pre><code>In [26]: import numpy as np

In [27]: np.sin(np.pi)
Out[27]: 1.2246467991473532e-16
</code></pre>

<p>The expected result is 0.  Why is <code>numpy</code> so inaccurate there?</p>

<p>To some extend, I feel uncertain whether it is acceptable to regard the calculated result as inaccurate: Its absolute error comes within one machine epsilon (for binary64), whereas the relative error is <code>+inf</code> -- reason why I feel somewhat confused. Any idea?</p>

<p>[Edit] I fully understand that floating-point calculation can be inaccurate. But most of the floating-point libraries can manage to deliver results within a small range of error. Here, the relative error is +inf, which seems unacceptable. Just imagine that we want to calculate </p>

<pre><code>1/(1e-16 + sin(pi)) 
</code></pre>

<p>The results would be disastrously wrong if we use numpy's implementation. </p>

<p>The value is dependent upon the algorithm used to compute it.  A typical implementation will use some quickly-converging infinite series, carried out until it converges within one machine epsilon.  Many modern chips (starting with the Intel 960, I think) had such functions in the instruction set.</p>

<p>To get 0 returned for this, we would need either a notably more accurate algorithm, one that ran extra-precision arithmetic to guarantee the closest-match result, or something that recognizes special cases: detect a multiple of PI and return the exact value.</p>

python numpy floating-point