Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old October 15th, 2006, 04:15 AM
Chris
Guest
 
Posts: n/a
Default problem with the 'math' module in 2.5?

>>from math import *
Quote:
Quote:
Quote:
>>sin(0)
0.0
Quote:
Quote:
Quote:
>>sin(pi)
1.2246063538223773e-016
Quote:
Quote:
Quote:
>>sin(2*pi)
-2.4492127076447545e-016
Quote:
Quote:
Quote:
>>cos(0)
1.0
Quote:
Quote:
Quote:
>>cos(pi)
-1.0
Quote:
Quote:
Quote:
>>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?

  #2  
Old October 15th, 2006, 04:35 AM
mensanator@aol.com
Guest
 
Posts: n/a
Default Re: problem with the 'math' module in 2.5?


Chris wrote:
Quote:
Quote:
Quote:
>from math import *
>sin(0)
0.0
Quote:
Quote:
>sin(pi)
1.2246063538223773e-016
Quote:
Quote:
>sin(2*pi)
-2.4492127076447545e-016
Quote:
Quote:
>cos(0)
1.0
Quote:
Quote:
>cos(pi)
-1.0
Quote:
Quote:
>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?

  #3  
Old October 15th, 2006, 04:45 AM
Max Erickson
Guest
 
Posts: n/a
Default Re: problem with the 'math' module in 2.5?

"Chris" <chrispatton@gmail.comwrote:
Quote:
Quote:
Quote:
>>>from math import *
>>>sin(0)
0.0
Quote:
Quote:
>>>sin(pi)
1.2246063538223773e-016
Quote:
Quote:
>>>sin(2*pi)
-2.4492127076447545e-016
Quote:
Quote:
>>>cos(0)
1.0
Quote:
Quote:
>>>cos(pi)
-1.0
Quote:
Quote:
>>>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?
>
Quote:
>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).

  #4  
Old October 15th, 2006, 04:55 AM
Chris
Guest
 
Posts: n/a
Default Re: problem with the 'math' module in 2.5?

sin(pi*0.5) is what I expected, but I expected to get 0 for sin(pi).

Max Erickson wrote:
Quote:
"Chris" <chrispatton@gmail.comwrote:
>
Quote:
Quote:
>>from math import *
>>sin(0)
0.0
Quote:
>>sin(pi)
1.2246063538223773e-016
Quote:
>>sin(2*pi)
-2.4492127076447545e-016
Quote:
>>cos(0)
1.0
Quote:
>>cos(pi)
-1.0
Quote:
>>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?
>
Quote:
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).
  #5  
Old October 15th, 2006, 04:55 AM
Max Erickson
Guest
 
Posts: n/a
Default Re: problem with the 'math' module in 2.5?

Max Erickson <maxerickson@gmail.comwrote:
Quote:
>
Try sin(pi*0.5) to see similar behavior to cos(pi) or cos(pi*2).
>
Uhh, switch that, cos(pi*0.5)...

  #6  
Old October 15th, 2006, 05:05 AM
Carsten Haese
Guest
 
Posts: n/a
Default Re: problem with the 'math' module in 2.5?

On 14 Oct 2006 20:33:13 -0700, Chris wrote
Quote:
Quote:
Quote:
>from math import *
>sin(0)
0.0
Quote:
Quote:
>sin(pi)
1.2246063538223773e-016
Quote:
Quote:
>sin(2*pi)
-2.4492127076447545e-016
Quote:
Quote:
>cos(0)
1.0
Quote:
Quote:
>cos(pi)
-1.0
Quote:
Quote:
>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

  #7  
Old October 15th, 2006, 05:05 AM
Chris
Guest
 
Posts: n/a
Default Re: problem with the 'math' module in 2.5?

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:
Quote:
Max Erickson <maxerickson@gmail.comwrote:
Quote:

Try sin(pi*0.5) to see similar behavior to cos(pi) or cos(pi*2).
>
Uhh, switch that, cos(pi*0.5)...
  #8  
Old October 15th, 2006, 05:05 AM
Chris
Guest
 
Posts: n/a
Default Re: problem with the 'math' module in 2.5?

Oh, ok that explains it. Is that why my 16-bit calculator gives me 0?
Carsten Haese wrote:
Quote:
On 14 Oct 2006 20:33:13 -0700, Chris wrote
Quote:
Quote:
>>from math import *
>>sin(0)
0.0
Quote:
>>sin(pi)
1.2246063538223773e-016
Quote:
>>sin(2*pi)
-2.4492127076447545e-016
Quote:
>>cos(0)
1.0
Quote:
>>cos(pi)
-1.0
Quote:
>>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
  #9  
Old October 15th, 2006, 05:15 AM
David Lees
Guest
 
Posts: n/a
Default Re: problem with the 'math' module in 2.5?

Chris wrote:
Quote:
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

  #10  
Old October 15th, 2006, 06:45 AM
Ben Finney
Guest
 
Posts: n/a
Default Re: problem with the 'math' module in 2.5?

"Chris" <chrispatton@gmail.comwrites:
Quote:
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

  #11  
Old October 15th, 2006, 06:55 AM
Gary Herron
Guest
 
Posts: n/a
Default Re: problem with the 'math' module in 2.5?

Ben Finney wrote:
Quote:
"Chris" <chrispatton@gmail.comwrites:
>
>
Quote:
>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
Quote:
Python refuses to guess what you want, and gives you the information
available.
>
>
  #12  
Old October 15th, 2006, 09:55 AM
Paddy
Guest
 
Posts: n/a
Default Re: problem with the 'math' module in 2.5?


Ben Finney wrote:
Quote:
"Chris" <chrispatton@gmail.comwrites:
>
Quote:
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.

  #13  
Old October 15th, 2006, 10:25 AM
Jorgen Grahn
Guest
 
Posts: n/a
Default Re: problem with the 'math' module in 2.5?

On Sun, 15 Oct 2006 00:18:29 -0400, Carsten Haese <carsten@uniqsys.comwrote:
....
Quote:
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:
Quote:
Quote:
Quote:
>>.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!
  #14  
Old October 15th, 2006, 10:45 AM
Ben Finney
Guest
 
Posts: n/a
Default Re: problem with the 'math' module in 2.5?

"Paddy" <paddy3118@netscape.netwrites:
Quote:
Ben Finney wrote:
Quote:
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.
Quote:
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

  #15  
Old October 15th, 2006, 09:55 PM
andy2O@hotmail.com
Guest
 
Posts: n/a
Default Re: problem with the 'math' module in 2.5?

Chris wrote:
Quote:
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

  #16  
Old October 15th, 2006, 11:55 PM
Steven D'Aprano
Guest
 
Posts: n/a
Default Re: problem with the 'math' module in 2.5?

On Sun, 15 Oct 2006 20:03:18 +1000, Ben Finney wrote:
Quote:
"Paddy" <paddy3118@netscape.netwrites:
>
Quote:
>Ben Finney wrote:
Quote:
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.
>
Quote:
>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.

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles