#this works in python 3
def pi_sum(n):
total, k = 0,1
while k <= n:
total, k = total +8 /(k *(k+2)), k + 4
return total
#this is how i tried to fix it for python 2
def pi_sum2
我有一種情況,我可能可能需要應用乘數到一個值,以獲得正確的結果。這涉及使用浮點除法來計算值。 我想這是一個好主意,在我對它們執行浮點邏輯以節省處理器時間之前檢查這些值,但是我不確定它在運行時的效率如何。 我假設if檢查是1或2條指令(自從彙編類以來已經有一段時間了),並且浮點運算將比這更多。 //Check
if (a != 10) { //1 or 2 instructions?