我製作了一個Python 3程序來計算學校項目的pi值,但它總是停在小數點後16位。 python中的數字長度是否有限制?如果有,我可以使用的語言會讓我繼續嗎?python有數字限制嗎?
accuracy = int(input("accuracy: "))
current = 2
opperation = "+"
number = 3
count = 1
for i in range (accuracy):
if opperation == "-":
number = number - (4/(current*(current+1)*(current+2)))
opperation = "+"
elif opperation == "+":
number = number + (4/(current*(current+1)*(current+2)))
opperation = "-"
current += 2
print(str(count).zfill(8)) + ": " + str(number)
count += 1
使用'decimal'模塊可以避免長度限制,對於您的情況重要的是避免浮點不精確。 –
使用給定大小的浮點數可以獲得精度限制。但是Python會讓你顯示更多的數字:'從數學導入pi;打印(格式(pi,'.32f'))'。 – jonrsharpe
如果你想確切知道你係統上的浮點數是多少,'import sys; print(sys.float_info)' –