473,903 Members | 6,791 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem with the 'math' module in 2.5?

>>from math import *
>>sin(0)
0.0
>>sin(pi)
1.2246063538223 773e-016
>>sin(2*pi)
-2.4492127076447 545e-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 3113

Chris wrote:
>from math import *
sin(0)
0.0
>sin(pi)
1.2246063538223 773e-016
>sin(2*pi)
-2.4492127076447 545e-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*********@gm ail.comwrote:
>>>from math import *
sin(0)
0.0
>>>sin(pi)
1.2246063538223 773e-016
>>>sin(2*pi)
-2.4492127076447 545e-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*********@gm ail.comwrote:
>>from math import *
sin(0)
0.0
>>sin(pi)
1.2246063538223 773e-016
>>sin(2*pi)
-2.4492127076447 545e-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*********@gm ail.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.2246063538223 773e-016
>sin(2*pi)
-2.4492127076447 545e-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.2246063538223 773e-016 is the scientific notation for the number
0.0000000000000 001224606353822 3773, 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.1230317691118 863e-017.

Max Erickson wrote:
Max Erickson <ma*********@gm ail.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.2246063538223 773e-016
>>sin(2*pi)
-2.4492127076447 545e-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.2246063538223 773e-016 is the scientific notation for the number
0.0000000000000 001224606353822 3773, 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*********@gm ail.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

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

Similar topics

16
2336
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 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.
11
7394
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
16403
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", line 22, in ? import BZfileRead File "/home/Larry/.blender/scripts/bzflag/BZfileRead.py", line 24, in ? import BZsceneWriter File "/home/Larry/.blender/scripts/bzflag/BZsceneWriter.py", line 25, in ? import BZcommon
8
2343
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: 18,446,744,073,709,551,616 I'm lazy... I don't want to convert it manually :)
12
15695
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 (SSH2). I've been trying to install Net::SFTP with 'sudo perl -MCPAN -e "install NET::SFTP' but I can't. It stops in the middle of the installation: Crypt-DH-0.06/t/ Crypt-DH-0.06/t/00-compile.t
4
2673
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 script. The script starts up ok, but when it imports the 'math' module, the import fails with:
17
2178
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 Before I did not know what to expect, but I certainly didn't expect
5
13563
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...
5
4403
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> OverflowError: math range error 10000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000
0
9999
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
9848
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
11284
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
10986
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
10501
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
8049
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
5894
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6094
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4308
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.