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

Zachary Pincus zpincus@stanford....
Wed Feb 21 12:26:58 CST 2007

Your results are indeed around zero.

 >>> numpy.allclose(0, 1.22460635382e-016)

It's not exactly zero because floating point math is in general not  
exact. You'll need to check out a reference about doing floating  
point operations numerically for more details, but in general you  
should not expect exact results due to the limited precision of any  
fixed-width digital representation of floats.

A corrolary: in general do not two floating-point values for equality  
-- use something like numpy.allclose. (Exception -- equality is  
expected if the exact sequence of operations to generate two numbers  
were identical.)

Zach Pincus

Program in Biomedical Informatics and Department of Biochemistry
Stanford University School of Medicine

On Feb 21, 2007, at 10:11 AM, WolfgangZillig 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?
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion@scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion

More information about the Numpy-discussion mailing list