0

I want to make a program which will return a nth number of digits after decimal in PI number. It works good as long as I don't put n higher than 50. Can I fix it or Python don't allow that?

Here's my code:

pi = 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270938521105559644622948954930381964428


def nth_pi_digit(n=input("How many digits after decimal you would like to see?(PI number):  ")):
    try:
        n = int(n)
        if n <= 204:
            return print(format(pi, f".{n}f"))
        else:
            print("Sorry, we have pi with 204 digits after decimal, so we'll print that")
            return print(format(pi, ".204f"))
    except:
        print("You need to put a integer digit")


nth_pi_digit()
MrLok3rs
  • 13
  • 5
  • 2
    Using side effect in default parameter is horrible trick. And it only works on function definition time, so it's a bug in your code – Elazar Feb 20 '20 at 15:08
  • 1
    Also, `return print(...)` returns `None`. It's a very strange shorthand for `print(...); return` – Elazar Feb 20 '20 at 15:12
  • Related: [Limiting floats to two decimal points](https://stackoverflow.com/q/455612/4518341) – wjandrea Feb 20 '20 at 15:14
  • Does this answer your question? [1000 digits of pi in Python](https://stackoverflow.com/questions/9004789/1000-digits-of-pi-in-python) – Georgy Feb 20 '20 at 15:26

3 Answers3

4

The float datatype has a limit on how precise you can get. 50 digits is around that.

I recommend using a Decimal instead to represent numbers with such high precision:

>>> from decimal import Decimal
>>> d = Decimal('3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270938521105559644622948954930381964428')
>>> format(d, ".50f")
'3.14159265358979323846264338327950288419716939937511'
>>> format(d, ".204f")
'3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102709385211055596446229489549303819644280'

This way you can do math on it without losing precision, which "treating it as a string" doesn't let you do.

Green Cloak Guy
  • 23,793
  • 4
  • 33
  • 53
1

You can simply use text:

pi = '3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270938521105559644622948954930381964428'

Though using decimal is better.

Elazar
  • 20,415
  • 4
  • 46
  • 67
0

Instantiate PI as a string object:

pi = '3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270938521105559644622948954930381964428'

And then you can perform slicing to show as many decimals you want:

n = 10    # 10 decimals
print(pi[:2+n])
adam
  • 94
  • 4