
-----------------------------------
andrew.
Sat Apr 05, 2008 8:55 pm

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 
    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

-----------------------------------
OneOffDriveByPoster
Sat Apr 05, 2008 10:04 pm

Re: Arc tan
-----------------------------------
#!/usr/bin/python
import math
print math.atan2(0, -1)

-----------------------------------
andrew.
Sun Apr 06, 2008 6:03 pm

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
Sun Apr 06, 2008 6:19 pm

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
Sun Apr 06, 2008 10:11 pm

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.
Mon Apr 07, 2008 10:06 am

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
Mon Apr 07, 2008 5:59 pm

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
Mon Apr 07, 2008 9:08 pm

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?

-----------------------------------
OneOffDriveByPoster
Mon Apr 07, 2008 11:51 pm

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?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
Sat Sep 27, 2008 12:17 pm

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.

from math import atan, pi


i don't have access to python from this location, but the general syntax was my goal.

-Z
