473,395 Members | 1,679 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

problem with the 'math' module in 2.5?

>>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?

Oct 15 '06 #1
15 3075

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?
What answer do you suppose you get in version 2.4?

Oct 15 '06 #2
"Chris" <ch*********@gmail.comwrote:
>>>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?
>From help(math) in an interactive window:

DESCRIPTION
This module is always available. It provides access to the
mathematical functions defined by the C standard.

So what you are seeing is the behavior of the C library being exposed.
Try sin(pi*0.5) to see similar behavior to cos(pi) or cos(pi*2).

Oct 15 '06 #3
sin(pi*0.5) is what I expected, but I expected to get 0 for sin(pi).

Max Erickson wrote:
"Chris" <ch*********@gmail.comwrote:
>>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?
From help(math) in an interactive window:


DESCRIPTION
This module is always available. It provides access to the
mathematical functions defined by the C standard.

So what you are seeing is the behavior of the C library being exposed.
Try sin(pi*0.5) to see similar behavior to cos(pi) or cos(pi*2).
Oct 15 '06 #4
Max Erickson <ma*********@gmail.comwrote:
>
Try sin(pi*0.5) to see similar behavior to cos(pi) or cos(pi*2).
Uhh, switch that, cos(pi*0.5)...

Oct 15 '06 #5
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

Oct 15 '06 #6
I don't understand what that number came from. My calculator gives me
cos(pi*.5) = 0, and my interpreter gives me cos(pi*0.5) =
6.1230317691118863e-017.

Max Erickson wrote:
Max Erickson <ma*********@gmail.comwrote:

Try sin(pi*0.5) to see similar behavior to cos(pi) or cos(pi*2).

Uhh, switch that, cos(pi*0.5)...
Oct 15 '06 #7
Oh, ok that explains it. Is that why my 16-bit calculator gives me 0?
Carsten Haese wrote:
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
Oct 15 '06 #8
Chris wrote:
sin(pi*0.5) is what I expected, but I expected to get 0 for sin(pi).
<snip>

http://docs.python.org/tut/node16.html

david

Oct 15 '06 #9
"Chris" <ch*********@gmail.comwrites:
Oh, ok that explains it. Is that why my 16-bit calculator gives me
0?
Your calculator is probably doing rounding without you asking for it.

Python refuses to guess what you want, and gives you the information
available.

--
\ "Earth gets its price for what Earth gives us." -- James |
`\ Russell Lowell |
_o__) |
Ben Finney

Oct 15 '06 #10
Ben Finney wrote:
"Chris" <ch*********@gmail.comwrites:

>Oh, ok that explains it. Is that why my 16-bit calculator gives me
0?

Your calculator is probably doing rounding without you asking for it.
Yes. Almost all calculators have 1 or 2 guard digits. These are extra
digits beyond what is shown on the display. All calculations are done at
that higher precision and the result are rounded to the precision of the
display for the user's benefit.

This satisfies users who know nothing about the numerical imprecision of
finite digit arithmetic. Python makes the opposite assumption that we
are are adults here and can handle the full knowledge, slight
imprecision and all.

Dr. Gary Herron
Python refuses to guess what you want, and gives you the information
available.

Oct 15 '06 #11

Ben Finney wrote:
"Chris" <ch*********@gmail.comwrites:
Oh, ok that explains it. Is that why my 16-bit calculator gives me
0?

Your calculator is probably doing rounding without you asking for it.

Python refuses to guess what you want, and gives you the information
available.
Hi Ben,
I don't think Python should take too much credit here. Floating point
calcuations are subject to rounding. Sometimes it shows.

- Pad.

Oct 15 '06 #12
On Sun, 15 Oct 2006 00:18:29 -0400, Carsten Haese <ca*****@uniqsys.comwrote:
....
You're not getting *exactly* zero because you're not passing in *exactly* pi
but a close approximation of pi.
That, plus the fact that floating-point math never is (in some sense)
precise. I am surprised noone brought up this one:
>>.2
0.20000000000000001

The original poster should read more on the subject. The Wikipedia article
seems like a good place to start:

http://en.wikipedia.org/wiki/Floating-point

/Jorgen

--
// Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
\X/ snipabacken.dyndns.org R'lyeh wgah'nagl fhtagn!
Oct 15 '06 #13
"Paddy" <pa*******@netscape.netwrites:
Ben Finney wrote:
Your calculator is probably doing rounding without you asking for
it.

Python refuses to guess what you want, and gives you the
information available.

I don't think Python should take too much credit here.
I don't understand what you could mean by this. Credit for "giv[ing]
you the information available"? That's exactly what it's doing.
Floating point calcuations are subject to rounding. Sometimes it
shows.
And Python is showing it, rather than hiding it. It certainly isn't
doing any rounding unless asked to do so.

--
\ Legionnaire: "We have their leader captive!" C泡r: "Is he |
`\ bound?" Legionnaire: "Of his health I know not, sir." -- The |
_o__) Goon Show, _The Histories Of Pliny The Elder_ |
Ben Finney

Oct 15 '06 #14
Chris wrote:
sin(pi*0.5) is what I expected, but I expected to get 0 for sin(pi).
Computers in general, and Python too, usually use floating point
arithmetic in which all numbers are approximated by rational numbers of
a particular form (see http://en.wikipedia.org/wiki/Floating_point for
details).

1) pi is an irrational number, so it *cannot* be represented exactly in
floating point. Therefore the value of pi in your call to the sin
function is definitely, proveably, *not* exactly equal to the true
value of pi.

2) So, even if the function "sin" could be evaluated exactly, the fact
that you are not evaluating it exactly at the true value of pi, but
instead at a good but imperfect approximation to this value, means that
the sine function *should not* give the result = 0 for your request!

3) The function sin is also evaluated to only a finite degree of
precision - just like everything else in floating point arithmetic.
Therefore you should not expect absolutely precise results. Instead,
you need to understand the limitations of floating point arithmetic,
understand the precision you *can* expect, and work within these
bounds. It's a good system, but you do need to understand its
limitations. The links other people have posted are good resources for
this.

Best wishes,
andy

Oct 15 '06 #15
On Sun, 15 Oct 2006 20:03:18 +1000, Ben Finney wrote:
"Paddy" <pa*******@netscape.netwrites:
>Ben Finney wrote:
Your calculator is probably doing rounding without you asking for
it.

Python refuses to guess what you want, and gives you the
information available.

I don't think Python should take too much credit here.

I don't understand what you could mean by this. Credit for "giv[ing]
you the information available"? That's exactly what it's doing.
>Floating point calcuations are subject to rounding. Sometimes it
shows.

And Python is showing it, rather than hiding it. It certainly isn't
doing any rounding unless asked to do so.
Python simply exposes whatever the C maths library does. The C maths
library is almost certainly doing some rounding: floats have only a finite
precision, which generally means the designer of the math library has two
choices: just truncate (chop) the calculation, or carry extra guard digits
(or bits) and round down. Most systems these days use guard digits, as
that is more accurate than truncating to a fixed precision.

If you mean that Python isn't doing *extra* rounding, above and beyond
what the C library is doing, you're correct. But rounding is happening.

This is a useful resource:

"What every computer scientist should know about floating-point
arithmetic"
http://docs.sun.com/source/806-3568/ncg_goldberg.html

To go back to the Original Poster's problem, he pointed out that his "16
bit calculator" gave a more accurate (but less precise) answer for
sin(pi), namely 0, instead of the more precise (but less accurate)
1.2246063538223773e-016 that Python reported. That just goes to show that,
sometimes, extra precision in floating point maths is a bad thing -- a
less precise library would actually have given a more correct answer.

This isn't strictly a Python question, but if there is anybody out there
who knows what the C library is doing, I'd appreciate an answer: since
sine is periodic, doesn't it make sense to reduce the argument modulo pi
before calculating the sine? Something like this:

def _sin(x):
x = x % math.pi
return math.sin(x)

Yes, you lose precision for large values of x because of the modulo, and
because math.pi isn't precisely pi, but that's got to be better than
losing precision for moderate values of x, surely? Have I missed something?
--
Steve.

Oct 15 '06 #16

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

Similar topics

16
by: Frank Millman | last post by:
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...
11
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
by: WIdgeteye | last post by:
I have been trying to run a python program and I get the following error: Traceback (most recent call last): File "<string>", line 39, in ? File "/home/Larry/.blender/scripts/bzflag/__init__.py",...
8
by: brad | last post by:
How does one make the math module spit out actual values without using engineer or scientific notation? I get this from <code>print math.pow(2,64)</code>: 1.84467440737e+19 I want this:...
12
by: jcor | last post by:
Hi, I'm using Ubuntu 7.04. I'm writing a sript that sends files via ftp for several destinations. So far I used Net::FTP and it worked fine. My problem is that I need to send files via SFTP...
4
by: DavidM | last post by:
Hi all, I'm embedding python in a C prog which is built as a linux shared lib. The prog is linked against libpython, and on startup, it calls Py_Initialize(). The prog imports a pure-python...
17
by: Albert Hopkins | last post by:
This issue may have been referred to in news:<mailman.1864.1196703799.13605.python-list@python.orgbut I didn't entirely understand the explanation. Basically I have this: 6.0 nan 6.0 nan ...
5
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...
5
by: Tzury Bar Yochay | last post by:
What is the reason math.pow yields OverflowError while python itself can calculate these large numbers. e.g: 1e+308 Traceback (most recent call last): File "<stdin>", line 1, in <module>...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...

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.