2

I was making a calculator and I wanted to use pi however python just shortens pi to 12 d.p. and I wanted the whole of pi.

I have a simple code in a file called getpi.py:

def getpisend():
   pi=*pi to a million places*
return pi

and I receive it with this code:

import getpi
print getpi.getpisend()

However it only prints this:

3.14159265359

How do I make it print all one million digits without making it a string. Because then I can't use it in a calculator. I have tried changing it into a string and then back to an float but get this error:

Traceback (most recent call last):
File "use-pi.py", line 2, in <module>
print (int(pifunc.getpisend()))
ValueError: invalid literal for int() with base 10:3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381

` EDIT: It is probably stupid to do this because it isn't actually usable because you would have to write/print loads of data but this has helped me understand python much more ! Special thanks to @qxz for showing the theory!

jimbob88
  • 697
  • 5
  • 20
  • 3
    Floating-point numbers have a limited amount of precision in memory. If you want to store numbers with more, look into an arbitrary-precision arithmetic library. (In other words, you can't with just a normal `number`) – qxz Dec 03 '16 at 18:12
  • @qxz okay. I will try this now – jimbob88 Dec 03 '16 at 18:13
  • int can only hold an integer -_- – Paul Stelian Dec 03 '16 at 18:15
  • @Paul Stelian I tried that afterwards and it failed :( – jimbob88 Dec 03 '16 at 18:17
  • 1
    This might be your answer. [How can I show an irrational number to 100 decimal places in python?](http://stackoverflow.com/questions/4733173/how-can-i-show-an-irrational-number-to-100-decimal-places-in-python) – mcmxl Dec 03 '16 at 18:18
  • @qxz This is really hard to understand as I am quite inexperienced! – jimbob88 Dec 20 '16 at 18:13
  • @wowcha Give this a read: [Floating Point Arithmetic: Issues and Limitations](https://docs.python.org/3/tutorial/floatingpoint.html) – qxz Dec 20 '16 at 21:13
  • @qxz Gave it a quick read and yes that is interesting and have written it down as a note! Who new there was so much more to a fraction!!!! – jimbob88 Dec 20 '16 at 21:20

1 Answers1

6

use decimal.Decimal:

In [57]: f = decimal.Decimal('3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359
    ...: 408128481117450284102701938521105559644622948954930381')

In [58]: f
Out[58]: Decimal('3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381')

In [59]: f.to_eng_string()
Out[59]: '3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381'
sirfz
  • 4,097
  • 23
  • 37