-3

I'm trying to make a Pi calculator in python but I need more decimal places.

it would help a lot if someone edited my code and carefully explained what they did.

this is the code I'm using.

import math
d = 0
ans = 0
display = 0
while True:
    display += 1
    d += 1
    ans += 1/d**2
    if display == 1000000:
        print(math.sqrt(ans*6))
        display = 0
    # displays value calculated every 1m iterations

output after ~85m iterations: (3.14159264498239)

I need more than 15 decimal places (3.14159264498239........)

  • 1
    More compared to what? What is your actual requirement? Also, instead of `while loop == loop`, you would typically use `while True` for an infinite loop. – jarmod Nov 04 '21 at 15:27
  • i need more decimal places in the output because im calculating a value with seemingly an infinite number of them. – Not Gartificial Nov 04 '21 at 15:34
  • 1
    You can't expect Stack Overflow users to download and run your code (in the general case). You should clearly indicate what your actual and desired results are. – jarmod Nov 04 '21 at 15:38
  • Take a look at the `decimal` package – JonSG Nov 04 '21 at 15:39
  • Find a better approximation series. https://en.wikipedia.org/wiki/Approximations_of_%CF%80#Efficient_methods – General Grievance Nov 04 '21 at 15:43
  • @Calculuswhiz how does that help with the problem of lack of float precision? – JonSG Nov 04 '21 at 15:44
  • 1
    @JonSG That's only part of the problem. Even with python's normal float precision, the series is extremely inefficient. At 12 million iterations, the series is only accurate to 6 digits, which python can represent without issue. – General Grievance Nov 04 '21 at 15:54
  • @Calculuswhiz While the approach is not optimal, the problem they have is clearly stated as "*I need more decimal places*" – JonSG Nov 04 '21 at 15:58
  • Review https://stackoverflow.com/questions/45416626/print-pi-to-a-number-of-decimal-places – June7 Nov 06 '21 at 06:26

2 Answers2

1

You’re using a very slowly converging series for π²∕6, so you are not going to get a very precise value this way. Floating point limitations prevent further progress after 3.14159264498239, but you’re not going to get much further in any reasonable amount of time, anyway. You can get around these issues by some combination of

  • micro-optimising your code,

  • storing a list of values, reversing it and using math.fsum,

  • using decimal.Decimal,

  • using a better series (like this one),

  • using a method that converges to the value of π quickly, instead of a series (like this one),

  • using PyPy, or a faster language than Python,

  • from math import pi.

LeopardShark
  • 3,820
  • 2
  • 19
  • 33
0

you could try with a generator:

def oddnumbers():
    n = 1
    while True:
        yield n
        n += 2


def pi_series():
    odds = oddnumbers()
    approximation = 0
    while True:
        approximation += (4 / next(odds))
        yield approximation
        approximation -= (4 / next(odds))
        yield approximation


approx_pi = pi_series()

for x in range(10000000):
    print(next(approx_pi))