我有一个令人费解的问题,我怀疑这与科学计数法和十进制精度有关。这是我的代码的一部分:
def atan(x):
# Calculate arctan(1/x)
x = Decimal(x)
current_value = Decimal(0)
divisor = 1
x_squared = x * x
current_term = 1 / x
while True:
current_value += current_term
divisor += 2
current_term = (-current_term / x_squared) / divisor
print(current_term)
# The issue
if current_term == Decimal(0):
break
return current_value
print(atan(5))
这是基于公式 atan(1/x) = 1/x - 1/(3x^3) + 1/(5x^5) - ...
但是,我发现,current_term
每次循环迭代都会变小,其值将变为4E-80000。由于我将小数精度设置getcontext().prec
为20,因此当前项甚至不应该支持这些值。我认为某种程度上current_term
不是十进制类型,而是科学计数法/浮点类型,但是python告诉我它仍然是十进制类型。
arctan(1/5)的正确值约为0.1973955。我得到的值为0.1973545,这从第5位数字开始是错误的。即使我手动中断了循环,由于某种原因,该值仍然是错误的。感谢您提供任何解决此问题的帮助。
您的代码与公式不匹配。从下一个推断一个术语有点麻烦;-)1/(5x^5)
术语不是术语的倍数1/(3x^3)
。
以下是直接对公式建模的代码:
from decimal import Decimal
def atan_recip(x):
# Calculate arctan(1/x)
x = Decimal(x)
total = Decimal(0)
sign = 1
for i in range(1, 35, 2):
total += sign / (i * x ** i)
sign = -sign
print(total)
atan_recip(5)
输出是您期望的:
0.2
0.1973333333333333333333333333
0.1973973333333333333333333333
0.1973955047619047619047619047
0.1973955616507936507936507936
0.1973955597889754689754689754
0.1973955598519908535908535908
0.1973955598498063202575202575
0.1973955598498834214339908457
0.1973955598498806620234645299
0.1973955598498807618878454823
0.1973955598498807582406246127
0.1973955598498807583748423407
0.1973955598498807583698713137
0.1973955598498807583700564416
0.1973955598498807583700495142
0.1973955598498807583700497745
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句