-2

i need to write a script that get input from client of an number and i need to print back the PI number until client number for example: client number is 52 --> 3.14159265358979323846264338327950288419716939937510

so far i write this:

sum = 0

for i in range(1, 1001):
    sum += ((-1)**(i+1))*4 / ((i + i)*(i + i + 1)*(i + i + 2))

print(sum)

the issue is that python showing to me only the 17 decimals digits, and i expect to see the 1000 decimal digits. there is a way to showing all the decimal digits based on the inputed range? it's for school task, so i need to write is as simple as it can be.

dor
  • 29
  • 5
  • Double precision floating point only offers ~14 significant figures of precision. If you want an arbitrary number of digits you will need a more sophisticated algorithm. – meowgoesthedog Mar 08 '19 at 10:47
  • 1
    You can use the [`decimal`](https://docs.python.org/3/library/decimal.html) module for arbitrary precision floating point arithmetic. – jdehesa Mar 08 '19 at 11:00
  • In addition to the [decimal](https://docs.python.org/3/library/decimal.html) module, which is in the Python standard library, you could also use [mpmath](http://mpmath.org/), which is part of the scipy stack and comes with the Anaconda distribution. The home page for mpmath shows three lines that calculate pi to 50 places. You could easily modify that to whatever number of digits you want. (Change the `50` in the second line to your desired number of digits.) – Rory Daulton Mar 08 '19 at 11:11
  • 2
    Possible duplicate of [1000 digits of pi in python](https://stackoverflow.com/questions/9004789/1000-digits-of-pi-in-python) – Chris Mar 08 '19 at 11:13

2 Answers2

0

I do not think it is possible to get a a float value with a 1000point precision.

Check https://stackoverflow.com/a/54967370/11152011 to calculate pi up to n-precision

You can then use the "decimal" library to use the string as a number.

The calculation of pi was taken from the above link.

import decimal

DIGITS = 1000

def pi_digits(x):
    k,a,b,a1,b1 = 2,4,1,12,4
    while x > 0:
        p,q,k = k * k, 2 * k + 1, k + 1
        a,b,a1,b1 = a1, b1, p*a + q*a1, p*b + q*b1
        d,d1 = a/b, a1/b1
        while d == d1 and x > 0:
            yield int(d)
            x -= 1
            a,a1 = 10*(a % b), 10*(a1 % b1)
            d,d1 = a/b, a1/b1

digits = [str(n) for n in list(pi_digits(DIGITS))]
str_pi='{}.{}'.format(digits.pop(0), "".join(digits)

context = decimal.Context(prec=100)
decimal.setcontext(context)
pi = decimal.Decimal(str_pi)
print(pi+5)
print(pi*20)
Sridhar Murali
  • 380
  • 1
  • 11
-3

I don't know how accurate is this but for a small class assignment i guess do the following:

num=52 #input
pi=22/7;
print('{:.{}f}'.format(pi, num))
Vinit Pillai
  • 518
  • 6
  • 17
  • 2
    I believe OP is looking for the **precise** value of PI up to some specified precision, not an ancient Mesopotamian approximation. – meowgoesthedog Mar 08 '19 at 11:09
  • Thanks for the answer, but the longest decimal digits is 51 digits. can you please also explain about the format use? – dor Mar 08 '19 at 11:21
  • @dor its called precision handling. refer this link: https://www.geeksforgeeks.org/precision-handling-python/ – Vinit Pillai Mar 08 '19 at 11:25