[Numpy-discussion] what goes wrong with cos(), sin()

Charles R Harris charlesr.harris at gmail.com
Wed Feb 21 13:25:50 EST 2007


On 2/21/07, WolfgangZillig <wollez at gmx.net> wrote:
>
> Hi,
>
> I'm quite new to numpy/scipy so please excuse if my problem is too
> obvious.
>
> example code:
>
> import numpy as n
> print n.sin(n.pi)
> print n.cos(n.pi/2.0)
>
> results in:
> 1.22460635382e-016
> 6.12303176911e-017
>
> I've expected something around 0. Can anybody explain what I am doing
> wrong here?


They *are* around zero. You are seeing rounding error, probably both in the
value of pi and the approximations used to evaluate the sin and cos, most
likely some rational min/max fit. I recall teaching PDEs to students who
used Mathematica and they had the same sort of problem because terms
involving sin(pi) didn't go exactly to zero. Made their fourier series not
quite match the text answers. If you want these sort of terms to be exact
you will have to use some sort of symbolic program.

Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20070221/06e9173f/attachment.html>


More information about the NumPy-Discussion mailing list