Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Arc tan
Index -> Programming, Python -> Python Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
andrew.




PostPosted: Sat Apr 05, 2008 8:55 pm   Post subject: Arc tan

Hi guys,

I've moved on from Turing and now I'm learning Python. I'm very new to this so this may sound like a stupid question to you but, how do I get atan (arctangent) to work in Python?

I was trying to make it calculate pi and the equation I'm using is: pi=16 * atan(1/5) - 4 * atan(1/239)

The problem is when I run this, it gets this error:

Traceback (most recent call last):
File "/home/andrew/Documents/Pi calculator.py", line 1, in <module>
pi=16 * atan(1/5) - 4 * atan(1/239)
NameError: name 'atan' is not defined

I've looked on the internet and it says stuff about it not working in Windows, so I booted up my Linux system and it's still not working. How can I make arctangent work and also can you help me make asin, acos, etc. work too just for if I need it in the future?

Thanks,
Andrew
Sponsor
Sponsor
Sponsor
sponsor
OneOffDriveByPoster




PostPosted: Sat Apr 05, 2008 10:04 pm   Post subject: Re: Arc tan

Python:
#!/usr/bin/python
import math
print math.atan2(0, -1)
andrew.




PostPosted: Sun Apr 06, 2008 6:03 pm   Post subject: Re: Arc tan

Will this work in Windows as well?

EDIT: Nevermind, I checked and it works. How did you get it to calculate pi though?
shaon




PostPosted: Sun Apr 06, 2008 6:19 pm   Post subject: Re: Arc tan

import math

math.pi # this gives back the value for pi

the math package also has many other functions as well
use the manual that comes with python to see how to use them all.
You shouldn't have to go and force your way into getting a number for pi.
OneOffDriveByPoster




PostPosted: Sun Apr 06, 2008 10:11 pm   Post subject: Re: Arc tan

I agree that math.pi would be more appropriate for getting pi. As for why math.atan2(0, -1) gets you pi, atan2(y, x) basically tells you
the angle between the positive x-axis and the argument vector. Similar to atan(y / x), but atan2() can handle extra cases.

atan2() is usually the only inverse trig function you need.
andrew.




PostPosted: Mon Apr 07, 2008 10:06 am   Post subject: Re: Arc tan

So how many decimal values will I get if I use math.pi? I want to try to calculate pi to about 1000 decimals.
OneOffDriveByPoster




PostPosted: Mon Apr 07, 2008 5:59 pm   Post subject: Re: Arc tan

Not enough. I am fairly certain that math.pi is just an IEEE double. If you want so many digits, you should look into specialized algorithms.
shaon




PostPosted: Mon Apr 07, 2008 9:08 pm   Post subject: Re: Arc tan

sorry to side track your question Andrew, but I'm curious as to what is meant by atan2 being able to handle extra cases? what are the extra cases?
Sponsor
Sponsor
Sponsor
sponsor
OneOffDriveByPoster




PostPosted: Mon Apr 07, 2008 11:51 pm   Post subject: Re: Arc tan

shaon @ Mon Apr 07, 2008 9:08 pm wrote:
sorry to side track your question Andrew, but I'm curious as to what is meant by atan2 being able to handle extra cases? what are the extra cases?
atan2 has special handling to give you an answer
*in the correct quadrant* and even with x == 0 or y == 0.

(If both x and y is zero, the result is not well defined; I think
python returns zero.)

S A
T C

With atan() a +ve (y / x) is assumed to be in quadrant A
and a -ve (y / x) is assumed to be in quadrant C.

Consider atan(y / x):

What if x is zero?

If (y == 0),
do we use the value of x effectively?

If (y / x < 0),
which was negative? x or y?

If (y / x > 0),
was x and y both negative?
TheZsterBunny




PostPosted: Sat Sep 27, 2008 12:17 pm   Post subject: RE:Arc tan

I am back from the dead.

Is it not better style to import specific functions/constants from libraries in python?

i.e.
Python:

from math import atan, pi


i don't have access to python from this location, but the general syntax was my goal.

-Z
Display posts from previous:   
   Index -> Programming, Python -> Python Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 10 Posts ]
Jump to:   


Style:  
Search: