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

WolfgangZillig wollez at gmx.net
Wed Feb 21 13:36:16 EST 2007


Anne Archibald schrieb:
> On 21/02/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?
> 
> Well, nothing. It is around zero. Try (without numpy)
>>>> (1.+1e-16)-1.
> 0.0
> That is, for floating-point numbers, 1e-16 is about the fractional
> error you can expect from any calculation, and since pi is of order
> unity, you should expect its representation to have about that big an
> error on it; feed that through sin and you get an error of about the
> same size.
> 
> Or, to see more clearly, try taking (on a pocket calculator, say)
> sin(3.14) (or even sin(pi)). Roundoff error is a basic fact of life in
> numerical computations.
> 
> Anne


Thanks for your answers. I was already aware that it won't be exactly 0 
but it wasn't clear to me that the rounding precision is around 1e-16.

Thanks for your help!
Wolfgang




More information about the NumPy-Discussion mailing list