Here is my project on a small scale. The purpose is to approximate pi. I know it isn't neat, but the algorithm is correct.
adds = 0
subtracts = 0
for x in range(0, 10):
adds += 1/(1 + 4*x)
subtracts += 1/(3 + 4*x)
print(adds) #DEBUGGING
print(subtracts) #DEBUGGING
pi = float(4*(adds + subtracts)
print(pi)
Seems like it should work, right? On python 3, the same exact code gives me an accurate answer. However, in 2.7.10, this happens in the shell:
===================== RESTART: C:/Python27/Scripts/pi.py
=====================
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
4.0
>>>
Can someone explain to me why this happens and how I can correct this? I have tried conventional methods like converting to strings, formatting, rounding, etc. but none of them work. It is like my variables are being saved as 1's and 0's despite their values constantly changing. Once again, the same code works fine on Python 3.