这个问题的范围很广,但这里有一些简单的想法(和代码!)这可能是计算arctan的起点。首先,好的老泰勒级数。为了简单起见,我们使用固定数量的术语;实际上,您可能需要根据x的大小来决定要动态使用的术语数量,或者引入某种收敛准则。在固定数量的条件下,我们可以使用类似于霍纳方案的方法有效地进行评估。在def arctan_taylor(x, terms=9):
"""
Compute arctan for small x via Taylor polynomials.
Uses a fixed number of terms. The default of 9 should give good results for
abs(x) < 0.1. Results will become poorer as abs(x) increases, becoming
unusable as abs(x) approaches 1.0 (the radius of convergence of the
series).
"""
# Uses Horner's method for evaluation.
t = 0.0
for n in range(2*terms-1, 0, -2):
t = 1.0/n - x*x*t
return x * t
上面的代码对于较小的x(比如说在绝对值中小于0.1),但是随着x变大,精度下降;对于abs(x) > 1.0,无论我们向它扔多少项(或多高的精度),序列永远不会收敛。所以我们需要一种更好的方法来计算更大的x。一种解决方案是通过标识arctan(x) = 2 * ar