Python – arctan(x) function in python

functionpythontrigonometry

The Write a function that will return an approximate value for pi by evaluating the equation pi = arctan(1/5) – arctan(1/239), where arctan is the tangent function from the math library . Im using python 3.4.1

The function I used was:

def wallis(pairs):
    acc = 1
    num = 2
    for apair in range(pairs):
        leftterm = math.atan(0.2)
        rightterm = math.atan(0.0041)

        acc = leftterm - rightterm

        return pi

but it didn't work. It's confusing.

Best Solution

I don't know where you got that formula;

it looks like you were trying to do Wallis' formula, pi = (2/1) * (2/3) * (4/3) * (4/5) * ... and switched mid-stream to Machin's formula, pi == 16 * atan(1/5) - 4 * atan(1/239).

from math import atan

def approx_pi():
    return 16 * atan(1/5) - 4 * atan(1/239)

then

>>> approx_pi()
3.1415926535897936