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

David Goldsmith David.L.Goldsmith at noaa.gov
Wed Feb 21 14:24:19 EST 2007


As far as a computer is concerned, those numbers are "around" zero - 
"growing-up" w/ Matlab, e.g., one quickly learns to recognize these 
numbers for what they are.  One way to return zero for numbers like 
these is

if numpy.allclose(x, 0): return 0 (or 0*x to assure that 0 is the same 
type as x),

but caveat emptor: sometimes, of course, 1e-16 really is supposed to be 
1e-16, not just the best the algorithm can do to get to zero.  Also, 
(help me out here guys) I thought there was something like 
zeroifclose(x) which does the above, or does numpy only have realifclose 
to return a real when an imaginary part is close to zero?

DG

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 at scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>   




More information about the NumPy-Discussion mailing list