0

My Code :

import math
def CalPI(precision):
    answer = round((math.pi),precision)
    return answer

precision=raw_input('Enter number of digits you want after decimal:')

try:
    roundTo=int(precision)
    print CalPI(roundTo)

except:
    print 'Error'

When I run this code I get the output max only upto 11 decimal places. However I want to generate the output according to the input given by user.

This is my output snip:

Can anyone tell me where I am going wrong?

Thank you in advance!

Mureinik
  • 297,002
  • 52
  • 306
  • 350
Chirag Shah
  • 87
  • 1
  • 3
  • 13

2 Answers2

2

If you use repr in your print you'll have 15 digits: 3.141592653589793.

If you want more digits (until 50) use

nb_digits = 40
print(format(math.pi, '.%dg' % nb_digits))

(thanks Stefan for the precision :) but as he stated again: don't trust digits after digit 15 so the 1000 digit program is the best).

For even more digits, compute pi yourself just like here:

1000 digits of pi in python

Community
  • 1
  • 1
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
0
"""
Math provides fast square rooting
Decimal gives the Decimal data type which is much better than Float
sys is needed to set the depth for recursion.
"""
from __future__ import print_function
import math, sys
from decimal import *
getcontext().rounding = ROUND_FLOOR
sys.setrecursionlimit(100000)

python2 = sys.version_info[0] == 2
if python2:
    input = raw_input()

def factorial(n):
    """
    Return the Factorial of a number using recursion
    Parameters:
    n -- Number to get factorial of
    """
    if not n:
        return 1
    return n*factorial(n-1)


def getIteratedValue(k):
    """
    Return the Iterations as given in the Chudnovsky Algorithm.
    k iterations give k-1 decimal places. Since we need k decimal places
    make iterations equal to k+1

    Parameters:
    k  -- Number of Decimal Digits to get
    """
    k = k+1
    getcontext().prec = k
    sum=0
    for k in range(k):
        first = factorial(6*k)*(13591409+545140134*k)
        down = factorial(3*k)*(factorial(k))**3*(640320**(3*k))
        sum += first/down 
    return Decimal(sum) 

def getValueOfPi(k):
    """
    Returns the calculated value of Pi using the iterated value of the loop
    and some division as given in the Chudnovsky Algorithm
    Parameters:
    k -- Number of Decimal Digits upto which the value of Pi should be calculated
    """
    iter = getIteratedValue(k)
    up = 426880*math.sqrt(10005)
    pi = Decimal(up)/iter 

    return pi

def shell():
    """
    Console Function to create the interactive Shell.
    Runs only when __name__ == __main__ that is when the script is being called directly
    No return value and Parameters
    """
    print ("Welcome to Pi Calculator. In the shell below Enter the number of digits upto which the value of Pi should be calculated or enter quit to exit")

    while True:
        print (">>> ", end='')
        entry = input()
        if entry == "quit":
            break
        if not entry.isdigit():
            print ("You did not enter a number. Try again")
        else:
            print (getValueOfPi(int(entry)))

if __name__=='__main__':
    shell()
Jeff Learman
  • 2,914
  • 1
  • 22
  • 31
  • 1
    Welcome to StackOverflow! Please edit your post. First, include some text to indicate what you're trying to show (it looks like you're only posting code, which is discouraged.) Second, paste your code in, select it, and click the `{}` widget to get it to render as code. That will shift it right four spaces, causing it to be rendered as code. Avoid tabs, unless they're set to 4 spaces. – Jeff Learman Jun 14 '18 at 15:56