Print Pi To A Number Of Decimal Places
Solution 1:
The proposed solutions using np.pi
, math.pi
, etc only only work to double precision (~14 digits), to get higher precision you need to use multi-precision, for example the mpmath package
>>>from mpmath import mp>>>mp.dps = 20# set number of digits>>>print(mp.pi)
3.1415926535897932385
Using np.pi
gives the wrong result
>>>format(np.pi, '.20f')
3.14159265358979311600
Compare to the true value:
3.14159265358979323846264338327...
Solution 2:
Why not just format
using number_of_places
:
''.format(pi)
>>> format(pi, '.4f')
'3.1416'
>>> format(pi, '.14f')
'3.14159265358979'
And more generally:
>>>number_of_places = 6>>>'{:.{}f}'.format(pi, number_of_places)
'3.141593'
In your original approach, I guess you're trying to pick a number of digits using number_of_places
as the control variable of the loop, which is quite hacky but does not work in your case because the initial number_of_digits
entered by the user is never used. It is instead being replaced by the iteratee values from the pi
string.
Solution 3:
For example the mpmath
package
from mpmath import mp
defa(n):
mp.dps=n+1return(mp.pi)
Solution 4:
Great answers! there are so many ways to achieve this. Check out this method I used below, it works any number of decimal places till infinity:
#import multp-precision modulefrom mpmath import mp
#define PI functiondefpi_func():
whileTrue:
#request input from usertry:
entry = input("Please enter an number of decimal places to which the value of PI should be calculated\nEnter 'quit' to cancel: ")
#condition for quitif entry == 'quit':
break#modify input for computation
mp.dps = int(entry) +1#condition for input errorexcept:
print("Looks like you did not enter an integer!")
continue#execute and print resultelse:
print(mp.pi)
continue
Good luck Pal!
Solution 5:
Your solution appears to be looping over the wrong thing:
fornumber_of_placesin fraser:
For 9 places, this turns out be something like:
for"9"in"3.141592653589793":
Which loops three times, one for each "9" found in the string. We can fix your code:
from math import pi
fraser = str(pi)
length_of_pi = []
number_of_places = int(raw_input("Enter the number of decimal places you want: "))
for places inrange(number_of_places + 1): # +1 for decimal point
length_of_pi.append(str(fraser[places]))
print"".join(length_of_pi)
But this still limits n
to be less than the len(str(math.pi))
, less than 15 in Python 2. Given a serious n
, it breaks:
> python test.py
Enter the number ofdecimal places you want to see: 100
Traceback (most recent calllast):
File "test.py", line 10, in<module>
length_of_pi.append(str(fraser[places]))
IndexError: string index outofrange>
To do better, we have to calculate PI ourselves -- using a series evaluation is one approach:
# Rewrite of Henrik Johansson's (Henrik.Johansson@Nexus.Comm.SE)# pi.c example from his bignum package for Python 3## Terms based on Gauss' refinement of Machin's formula:## arctan(x) = x - (x^3)/3 + (x^5)/5 - (x^7)/7 + ...from decimal import Decimal, getcontext
TERMS = [(12, 18), (8, 57), (-5, 239)] # ala Gaussdefarctan(talj, kvot):
"""Compute arctangent using a series approximation"""
summation = 0
talj *= product
qfactor = 1while talj:
talj //= kvot
summation += (talj // qfactor)
qfactor += 2return summation
number_of_places = int(input("Enter the number of decimal places you want: "))
getcontext().prec = number_of_places
product = 10 ** number_of_places
result = 0for multiplier, denominator in TERMS:
denominator = Decimal(denominator)
result += arctan(- denominator * multiplier, - (denominator ** 2))
result *= 4# pi == atan(1) * 4
string = str(result)
# 3.14159265358979E+15 => 3.14159265358979print(string[0:string.index("E")])
Now we can take on a large value of n
:
> python3 test2.py
Enter the number of decimal places you want: 100
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067
>
Post a Comment for "Print Pi To A Number Of Decimal Places"