我的代码:
def calc_pi(acc):
pos = False
sum = 4.0
for i in range(2, acc):
if not pos:
sum -= 4.0/(2*i-1)
pos = True
else:
sum += 4.0/(2*i-1)
pos = False
return float(sum)
print(calc_pi(5000))
当然,我正在尝试计算一个带有10个小数点后数字的圆周率。但是Python似乎会四舍五入到10位。有没有简单的方法可以防止这种情况发生?例如一百万位小数点后?
谢谢!
decimal
可以让你赚到更多。 (https://docs.python.org/2/library/decimal.html) - NightShadeQueen