473,769 Members | 5,871 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

math module broken?

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

cos(32) -
Python 0.83422336
Microsoft 0.84804809

tan(32) -
Python 0.66100604
Microsoft 0.62486935

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?

Thanks

Frank Millman
Jul 18 '05 #1
16 2324
fr***@chagford. com (Frank Millman) writes:
sin(32) -
Python 0.55142668
Microsoft 0.52991926
Python's number is the sin of 32 radians which is the same as the sin
of 0.584 radians. Microsoft's is the sin of 32 degrees which is 0.558
radians, so the results are coincidentally fairly close to one
another. Math libraries usually take args in radians while
calculators usually let you select between radians and degrees.
cos(32) -
Python 0.83422336
Microsoft 0.84804809

tan(32) -
Python 0.66100604
Microsoft 0.62486935


Same thing.
Jul 18 '05 #2
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
Jul 18 '05 #3
Helmut Jarausch <ja******@igpm. rwth-aachen.de> wrote in message
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 (!)
There was some discussion about this recently over in
comp.lang.fortr an, arising when someone queried sin/cos/tan etc for
very large angles (represented in floating point, eg: sin(1.0E18)).
Reduction (angle modulo 2pi) is considerably easier if you use a
*rational* unit for measuring angles, as the answer can remain exact
in terms of the particular number represented in floating point.
Without using an irrational number, exact representations of angles
can actually exist where you also have exact representations of sines
and cosines. With radians you can only represent zero exactly, and
below it seems not even that!

Can someone supply a real concrete example where there is a reason to
prefer radians when computing sin/cos/tan? (Not their derivatives!)

The latter of the three examples is most disappointing:

C:\>python
Python 2.3.3 (#51, Dec 18 2003, 20:22:39) [MSC v.1200 32 bit (Intel)]
on win32
Type "help", "copyright" , "credits" or "license" for more information.
from math import sin,asin,pi
sin(pi/6) # == 0.5 0.4999999999999 9994 degrees(asin(0. 5)) # == 30 30.000000000000 004 sin(pi) # == 0

1.2246063538223 773e-016

I understand why this fails - I just think the world is wrong.
Represent angles using some rational fraction of a circle and all this
crap goes away. Why convert to a unit which eliminates the possibility
of an exact representation of any result at all?
In the example above, Microsoft - something special as ever - seems to expect
the argument in degrees (which is quite unusual)


The microsoft calculator has a checkbox for switching between degrees
and radians and gets all of the analytical results above correct. We
live in a mad world....

Jon
Jul 18 '05 #4
On 23 Jul 2004, Jon Wright wrote:
There was some discussion about this recently over in
comp.lang.fortr an, arising when someone queried sin/cos/tan etc for
very large angles (represented in floating point, eg: sin(1.0E18)).
Reduction (angle modulo 2pi) is considerably easier if you use a
*rational* unit for measuring angles, as the answer can remain exact
in terms of the particular number represented in floating point.
Reduction... from 1.0e18?! Of course that doesn't work with radians, it
doesn't even works with degrees! 64-bit floating point numbers are only
accurate to +-64.0 at that magnitude:
(1.0e18+45)-1.0e18 0.0 (1.0e18+90)-1.0e17 128.0
Without using an irrational number, exact representations of angles
can actually exist where you also have exact representations of sines
and cosines. With radians you can only represent zero exactly, and
below it seems not even that!
So don't store your angles as radians. Mangle them, reduce them, whatever
if degrees / grads, and then convert them to radians before passing them
to the trig functions.
Can someone supply a real concrete example where there is a reason to
prefer radians when computing sin/cos/tan? (Not their derivatives!)
Because that's how the processor does it. If degrees were used in the
library, every sin() would have to be prefixed at the assembly level with
a multiply.

Derivates (and other mathematical reasons) are also perfectly valid
example. Every math / physics formula involving trigonometry uses
radians. If sin() accepted degrees, every formula I expressed in Python
would be littered with radian -> degree conversions. Quite confusing for
a researcher picking up Python for the first time. Python's done a good
job at winning over academia (mostly due to Numeric and numarray);
switching to degrees is a step in the wrong direction.

I'll skip over the obvious argument of purity here (i.e. switching trig
functions to degrees is the equivalent of switching to 1-based array
indexing).
The latter of the three examples is most disappointing:

C:\>python
Python 2.3.3 (#51, Dec 18 2003, 20:22:39) [MSC v.1200 32 bit (Intel)]
on win32
Type "help", "copyright" , "credits" or "license" for more information.
from math import sin,asin,pi
sin(pi/6) # == 0.5 0.4999999999999 9994 degrees(asin(0. 5)) # == 30 30.000000000000 004 sin(pi) # == 0
1.2246063538223 773e-016

I understand why this fails - I just think the world is wrong.
Represent angles using some rational fraction of a circle and all this
crap goes away. Why convert to a unit which eliminates the possibility
of an exact representation of any result at all?

.2 0.2000000000000 0001

The error in your calculations is of the same order of magnitude as that
of the error introduced by the representation of numbers as floating point
(not surprisingly, since the magnitude of the error of the value of pi is
caused by that same feature), and is therefore, for all practical
purposes, irrelevant.

What's more, because any method of calculating sines (including that used
by the processor) requires that its arguments be in radians, there's no
way around this error, no matter how hard you try (unless, of course, you
implement an 80- or even 128-bit floating point arithmetic library, in
which case it won't be included in Python because it will be an order of
magnitude slower than using the math coprocessor).
The microsoft calculator has a checkbox for switching between degrees
and radians and gets all of the analytical results above correct. We
live in a mad world....


I'm certain that's because it limits (reasonably) the accuracy of its
output, just like Python can do:
print sin(pi/6) # == 0.5 0.5 print degrees(asin(0. 5)) # == 30 30.0 print sin(pi) # == 0 1.2246063538223 773e-016

Oops, that last one didn't work. How about:
'%.15f'%sin(pi)

'0.000000000000 000'

Personally, I'd like to see an application where any accuracy greater than
that is needed... and anyways, just how often do you expect angles of 30,
45, 60, and 90 degrees to come up in regular calculations? The chance of
one of them occuring randomly is infinitesmally small (except for in
high-school geometry homework, of course), and when they do come up
deterministical ly in a formula, they are almost always optimized out of
the equation.

Jul 18 '05 #5
[Christopher T King]
...
Reduction... from 1.0e18?! Of course that doesn't work with radians, it
doesn't even works with degrees!


Actually, it can. If you view a floating-point number as being
exactly what it says it is, then of course there's an exact reduction
of that number modulo pi. Very high-quality math libraries compute
that too. I know because I wrote one <wink>. The popular fdlibm
(from Netlib) does "as-if infinite precision" trig argument reduction
too. The details can be excruciating. You (of course) need to know
pi to greater than machine precision, but not to as much greater as
you may think: across all possible 754 doubles, you can find the one
closest to being an exact multiple of pi, and you only need to use
enough extra precision to get the right answer (to machine precision)
in that worst case. At least 3 groups have discovered that
independently (I was one of them, discovered when writing a libm for
Kendall Square Research in the early 90's, in collaboration with Peter
Tang).

IIRC, Mary Payne at DEC was the first to implement "infinite
precision" trig argument reduction in a commercial math library, and
she wrote a very readable paper about it I'm unable to find now. She
wasn't able to put an a priori bound on the amount of precision
needed, so it was less efficient than later attempts.

All in all, and despite having pushed the state of the art there
myself, it's a silly thng to bother with <wink>.
Jul 18 '05 #6
On Fri, 23 Jul 2004, Tim Peters wrote:
If you view a floating-point number as being exactly what it says it is,


then I have a proof that 1==2 to show you... ;)

Jul 18 '05 #7
Helmut Jarausch <ja******@igpm. rwth-aachen.de> wrote in message news:<41******* *******@igpm.rw th-aachen.de>...
....
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


x/180*pi _is_ correct. Your formula gives mysin(32) = -0.9408618465702 292.

Btw, another way to write this is:

def mysin(x):
return math.sin(math.r adians(x))
Jul 18 '05 #8
da*****@yahoo.c om (Dan Bishop) wrote in message news:<ad******* *************** ****@posting.go ogle.com>...
Helmut Jarausch <ja******@igpm. rwth-aachen.de> wrote in message news:<41******* *******@igpm.rw th-aachen.de>...
...
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


x/180*pi _is_ correct. Your formula gives mysin(32) = -0.9408618465702 292.

Btw, another way to write this is:

def mysin(x):
return math.sin(math.r adians(x))


Even better:

def mysin(x):
return math.sin(math.r adians(x % 360))
Jul 18 '05 #9
da*****@yahoo.c om (Dan Bishop) wrote in message news:<ad******* *************** ****@posting.go ogle.com>...
Helmut Jarausch <ja******@igpm. rwth-aachen.de> wrote in message news:<41******* *******@igpm.rw th-aachen.de>...
...
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


x/180*pi _is_ correct. Your formula gives mysin(32) = -0.9408618465702 292.

Btw, another way to write this is:

def mysin(x):
return math.sin(math.r adians(x))


Even better:

def mysin(x):
return math.sin(math.r adians(x % 360))
Jul 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

89
5144
by: Radioactive Man | last post by:
In python 2.3 (IDLE 1.0.3) running under windows 95, I get the following types of errors whenever I do simple arithmetic: 1st example: >>> 12.10 + 8.30 20.399999999999999 >>> 1.1 - 0.2 0.90000000000000013
11
7370
by: Sambo | last post by:
I have the following module: ------------------------------- import math def ac_add_a_ph( amp1, ph1, amp2, ph2 ): amp3 = 0.0 ph3 = 0.0 ac1 = ( 0, 0j ) ac2 = ( 0, 0j )
5
13546
by: aguirre.adolfo | last post by:
Hi, I am a very newbie who would very much appreciate some hints. Python 2.52. on Windows XP for now. Soon on Ubuntu 8 I am teaching myself Python following free tutorials. I can solve problems using arithmetic, but when I try to upgrade the programs using math libraries nothing seems to work. I downloaded a 2002 tutorial from Zelle "An Introduction to Computer Science" where he uses a "import math" statement to calculate a square...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10211
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9994
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8872
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7409
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6673
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3959
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3562
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.