444,050 Members | 1,009 Online Need help? Post your question and get tips & solutions from a community of 444,050 IT Pros & Developers. It's quick & easy.

# problem with the 'math' module in 2.5?

 P: n/a >>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 Replies

 P: n/a 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

 P: n/a "Chris" >>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

 P: n/a sin(pi*0.5) is what I expected, but I expected to get 0 for sin(pi). Max Erickson wrote: "Chris" >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

 P: n/a Max Erickson 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

 P: n/a 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

 P: n/a 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

 P: n/a 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

 P: n/a Chris wrote: sin(pi*0.5) is what I expected, but I expected to get 0 for sin(pi). http://docs.python.org/tut/node16.html david Oct 15 '06 #9

 P: n/a "Chris"

 P: n/a Ben Finney wrote: "Chris" Oh, ok that explains it. Is that why my 16-bit calculator gives me0? 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

 P: n/a Ben Finney wrote: "Chris"

 P: n/a On Sun, 15 Oct 2006 00:18:29 -0400, Carsten Haese >.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 