problem with the 'math' module in 2.5?

Carsten Haese carsten at uniqsys.com
Sun Oct 15 06:18:29 CEST 2006


On 14 Oct 2006 20:33:13 -0700, Chris wrote
> >>> from math import *
> >>> sin(0)
> 0.0
> >>> sin(pi)
> 1.2246063538223773e-016
> >>> sin(2*pi)
> -2.4492127076447545e-016
> >>> cos(0)
> 1.0
> >>> cos(pi)
> -1.0
> >>> cos(2*pi)
> 1.0
> 
> The cosine function works fine, but I'm getting weird answers for sine.
> Is this a bug? Am I doing something wrong?

You're apparently not correctly reading python's answer to sin(pi).
1.2246063538223773e-016 is the scientific notation for the number
0.00000000000000012246063538223773, which is pretty darn close to zero, the
result you probably expected.

You're not getting *exactly* zero because you're not passing in *exactly* pi
but a close approximation of pi.

I'll leave it as an exercise for the reader to explain why cosine seems to
work fine. Hint: Look at cos(pi/2) and sin(pi/2).

-Carsten




More information about the Python-list mailing list