473,782 Members | 2,507 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
16 2325
On 23 Jul 2004 23:00:18 -0700,
da*****@yahoo.c om (Dan Bishop) wrote:
da*****@yahoo.c om (Dan Bishop) wrote in message news:<ad******* *************** ****@posting.go ogle.com>...
def mysin(x):
return math.sin(math.r adians(x)) Even better: def mysin(x):
return math.sin(math.r adians(x % 360))


Why?
for x in range( 20 ):

print '%.12f %.12f' % (math.sin( math.radians( x ) ), math.sin( math.radians( x % 360 ) ))

0.000000000000 0.000000000000
0.017452406437 0.017452406437
0.034899496703 0.034899496703
0.052335956243 0.052335956243
0.069756473744 0.069756473744
0.087155742748 0.087155742748
0.104528463268 0.104528463268
0.121869343405 0.121869343405
0.139173100960 0.139173100960
0.156434465040 0.156434465040
0.173648177667 0.173648177667
0.190808995377 0.190808995377
0.207911690818 0.207911690818
0.224951054344 0.224951054344
0.241921895600 0.241921895600
0.258819045103 0.258819045103
0.275637355817 0.275637355817
0.292371704723 0.292371704723
0.309016994375 0.309016994375
0.325568154457 0.325568154457

Regards,
Dan

--
Dan Sommers
<http://www.tombstoneze ro.net/dan/>
Never play leapfrog with a unicorn.
Jul 18 '05 #11
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.
Can someone please explain these discrepancies?


Thank you so much for the explanations, everyone. I appreciate your
patience.

I agree with Timothy's suggestion about adding a note to the
documentation. Obviously the math module is designed for those that
know what they are doing. However, you will always get the odd guy
like me that has some vague recollection of trigonometry from school
days, but does not really understand it, so it should help to avoid
similar confusion in the future.

Frank
Jul 18 '05 #12
Dan Sommers wrote:
On 23 Jul 2004 23:00:18 -0700,
da*****@yahoo.c om (Dan Bishop) wrote:

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


Why?
for x in range( 20 ):
print '%.12f %.12f' % (math.sin( math.radians( x ) ), math.sin( math.radians( x % 360 ) ))

0.000000000000 0.000000000000
0.017452406437 0.017452406437
0.034899496703 0.034899496703
0.052335956243 0.052335956243
0.069756473744 0.069756473744
0.087155742748 0.087155742748
0.104528463268 0.104528463268
0.121869343405 0.121869343405
0.139173100960 0.139173100960
0.156434465040 0.156434465040
0.173648177667 0.173648177667
0.190808995377 0.190808995377
0.207911690818 0.207911690818
0.224951054344 0.224951054344
0.241921895600 0.241921895600
0.258819045103 0.258819045103
0.275637355817 0.275637355817
0.292371704723 0.292371704723
0.309016994375 0.309016994375
0.325568154457 0.325568154457

for x in range(10000000, 10000020): print '%.12f %.12f' %

(math.sin( math.radians( x ) ), math.sin( math.radians( x % 360 ) ))

-0.984807753010 -0.984807753012
-0.981627183449 -0.981627183448
-0.978147600733 -0.978147600734
-0.974370064789 -0.974370064785
-0.970295726278 -0.970295726276
-0.965925826288 -0.965925826289
-0.961261695942 -0.961261695938
-0.956304755964 -0.956304755963
-0.951056516293 -0.951056516295
-0.945518575604 -0.945518575599
-0.939692620787 -0.939692620786
-0.933580426495 -0.933580426497
-0.927183854571 -0.927183854567
-0.920504853453 -0.920504853452
-0.913545457639 -0.913545457643
-0.906307787041 -0.906307787037
-0.898794046299 -0.898794046299
-0.891006524183 -0.891006524188
-0.882947592863 -0.882947592859
-0.874619707138 -0.874619707139

When the numbers get larger, floats start losing precision. If you
restrict the numbers to the range [0, 360[ before conversion to radians
(and hence to degrees), that problem doesn't exist.

--
"Codito ergo sum"
Roel Schroeven
Jul 18 '05 #13
Roel Schroeven wrote:
When the numbers get larger, floats start losing precision. If you
restrict the numbers to the range [0, 360[ before conversion to radians
(and hence to degrees), that problem doesn't exist.

(360**100 +0.0) % 360

184.0

Rule of thumb: floats are always inaccurate. % may only help when you are
dealing with large integers denoting an angle in degrees - an unlikely
scenario methinks.

Peter

Jul 18 '05 #14
Peter Otten wrote:
Roel Schroeven wrote:

When the numbers get larger, floats start losing precision. If you
restrict the numbers to the range [0, 360[ before conversion to radians
(and hence to degrees), that problem doesn't exist.


(360**100 +0.0) % 360


184.0

Rule of thumb: floats are always inaccurate. % may only help when you are
dealing with large integers denoting an angle in degrees - an unlikely
scenario methinks.


Ok, you're right, I only considered the case where the input is
specified in degrees.

Anyway, when dealing with angles it's in most cases very well possible
to keep the values small by doing % 360 or % (2*pi) in all relevant
operations on the angles.

--
"Codito ergo sum"
Roel Schroeven
Jul 18 '05 #15
Dan Sommers <me@privacy.net > wrote in message news:<m2******* *****@unique.fu lly.qualified.d omain.name.yeah .right>...
On 23 Jul 2004 23:00:18 -0700,
da*****@yahoo.c om (Dan Bishop) wrote:
da*****@yahoo.c om (Dan Bishop) wrote in message news:<ad******* *************** ****@posting.go ogle.com>...

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

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


Why?
for x in range( 20 ): print '%.12f %.12f' % (math.sin( math.radians( x ) ), math.sin( math.radians( x % 360 ) ))
[2 identical columns]


It's not suprising that they're identical: When 0 <= x < 360, then x % 360 == x.

However,
x = 360000000000L # a billion revolutions
math.sin(math.r adians(x)) -6.6394736764063 769e-08 math.sin(math.r adians(x % 360))

0.0
Jul 18 '05 #16
On 24 Jul 2004 12:44:30 -0700,
da*****@yahoo.c om (Dan Bishop) wrote:
Dan Sommers <me@privacy.net > wrote in message news:<m2******* *****@unique.fu lly.qualified.d omain.name.yeah .right>...
On 23 Jul 2004 23:00:18 -0700,
da*****@yahoo.c om (Dan Bishop) wrote:
> da*****@yahoo.c om (Dan Bishop) wrote in message news:<ad******* *************** ****@posting.go ogle.com>...
>> def mysin(x):
>> return math.sin(math.r adians(x))

> Even better:

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


Why?
>>> for x in range( 20 ):

print '%.12f %.12f' % (math.sin( math.radians( x ) ), math.sin( math.radians( x % 360 ) ))
[2 identical columns]

It's not suprising that they're identical: When 0 <= x < 360, then x % 360 == x.


Yes, obviously, no surprise there. Duh! (smacks self on forehead)

I do, hoever, agree with Peter Otten about very large integers denoting
angles in degreen being unlikely. Usually, the 'mod 2pi' ends up in the
calculation of the angle rather than in the call to math.<whatever> .

Regards,
Dan

--
Dan Sommers
<http://www.tombstoneze ro.net/dan/>
Never play leapfrog with a unicorn.
Jul 18 '05 #17

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

Similar topics

89
5149
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
7372
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
9641
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10146
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10080
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
8968
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
7494
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
6735
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();...
1
4044
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
3643
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2875
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.