Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

Is my code giving a bad pi result because of a mistake on the code or because of float error?

$
0
0

I have just started learning python and to test if I can do loops correctly I tried to calculate pi through the wallis product.

The code I wrote was like this:

pi=[1]i=0while i<100:    i=i+1    pi=pi+[4*i**2/(4*i**2-1)]print(pi)x=1for y in pi:    x=x*y    print(x*2)

For some reason this is giving a very very bad approximation of pi and I don't know why. With 100 iterations it should definitely not give 3.13And even if i increase the number of iterations the improvement is minimal. I know it's a very basic code but I'm just learning this language and I don't see if the mistake is on the floating point error or in my code itself.


Viewing all articles
Browse latest Browse all 23131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>