python中的arctan(x)函数

2024-10-06 07:51:12 发布

您现在位置:Python中文网/ 问答频道 /正文

编写一个函数,通过计算公式pi=arctan(1/5)-arctan(1/239),返回pi的近似值,其中arctan是数学库中的切线函数。Im使用Python3.4.1

我使用的功能是:

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

但没用。令人困惑。


Tags: 函数功能defpi数学mathaccim
1条回答
网友
1楼 · 发布于 2024-10-06 07:51:12

我不知道你从哪里得到的公式

看起来你在尝试做Wallis公式,pi = (2/1) * (2/3) * (4/3) * (4/5) * ...,然后把中流转换成Machin公式,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)

那么

>>> approx_pi()
3.1415926535897936

相关问题 更多 >