math module broken?

Helmut Jarausch jarausch at igpm.rwth-aachen.de
Fri Jul 23 03:29:54 EDT 2004


Frank Millman wrote:
> Hi all
> 
> I was helping my niece with her trigonometry homework last night. Her
> calculator's batteries were flat, so I thought I would use Python's
> math module to calculate sin, cos, and tan.
> 
> I tried the example in the text book first, to ensure that I was
> getting the correct result, but it did not agree. Then my wife had the
> idea of using the Microsoft calculator in scientific mode, and that
> one did give the correct result.
> 
> Here are some examples -
> 
> sin(32) -
>     Python    0.55142668
>     Microsoft 0.52991926
> 
> 
> Version is Python 2.3.3. I get the same results on Linux and on
> Windows 2000. I also get the same results using the cmath module.
> 
> Can someone please explain these discrepancies?
> 

Both are "correct" if you know what you have been asking for.
In standard mathematics the argument to a trigonometric function like
sin, cos, tan, ...  is in radians (!)
This is even the case with most pocket calculators but there you can switch
modes.
In the example above, Microsoft - something special as ever - seems to expect
the argument in degrees (which is quite unusual)

Since an argument in radians normally is between 0 and 2*pi  or  between -pi and pi,
an argument of 32 is unusual for a radians argument though perfectly legal.

If your input is in degrees, define

def mysin(x)
     return math.sin(x/pi*180) # 1 degree = 180/pi radians (don's use  x/180*pi)

now
print mysin(32)  gives 0.529919264233


-- 
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany



More information about the Python-list mailing list