1

We recently delve into infinite series in calculus and that being said, I'm having so much fun with it. I derived my own inverse tan infinte series in python and set to 1 to get pi/4*4 to get pi. I know it's not the fastest algorithm, so please let's not discuss about my algorithm. What I would like to discuss is how do I represent very very small numbers in python. What I notice is as my programs iterate the series, it stops somewhere at the 20 decimal places (give or take). I tried using decimal module and that only pushed to about 509. I want an infinite (almost) representation.

Is there a way to do such thing? I reckon no data type will be able to handle such immensity, but if you can show me a way around that, I would appreciate that very much.

Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
  • possible duplicate of [Python floating point arbitrary precision available?](http://stackoverflow.com/questions/11522933/python-floating-point-arbitrary-precision-available) – Ken Wayne VanderLinde May 18 '13 at 19:54
  • If it stopped after 509 decimal places then that indicates a flaw in the algorithm, not the datatype. – Ignacio Vazquez-Abrams May 18 '13 at 19:57
  • @KenWayneVanderLinde nope, they just recommended to use the decimal module on that one either. – Joey Arnold Andres May 18 '13 at 19:59
  • @JoeyArnoldAndres: How are you sing the `decimal` module? Are you sure your algorithm isn't just slowing down to a point where it looks like it stopped? – Blender May 18 '13 at 20:02
  • @IgnacioVazquez-Abrams can't be, (btw 509 is a guess, it is actually +/- 400). I gave it a try in the interpreter typing Decimal ( 10 ** -100) to Decimal( 10 ** -300) and they both gave the expected ammount of decimal. But when I gave (10 ** -400) it just gives zero, indicating the rough limit of the decimal module. – Joey Arnold Andres May 18 '13 at 20:04
  • 2
    That's because you're converting from a `float`, and those only go to about 1e-308 or so. `>>> Decimal('1e-400')` `Decimal('1E-400')` – Ignacio Vazquez-Abrams May 18 '13 at 20:07
  • @Blender Huhhhhh, that's why I said let's not discuss the algorithm. I know that the inverse tan algorithm slows down after a couple of iteration below a hundred, but to be honest I'm using the spigot method. I simply used inverse tan for simplicity sake. NOpe, it still seem rolling but with the same value after the point when it rans out of precission. – Joey Arnold Andres May 18 '13 at 20:07

1 Answers1

1

Python's decimal module requires that you specify the "context," which affects how precise the representation will be.

I might recommend gmpy2 for this type of thing - you can do the calculation on rational numbers (arbitrary precision) and convert to decimal at the last step.

Here's an example - substitute your own algorithm as needed:

import gmpy2
# See https://gmpy2.readthedocs.org/en/latest/mpfr.html
gmpy2.get_context().precision = 10000
pi = 0
for n in range(1000000):
    # Formula from http://en.wikipedia.org/wiki/Calculating_pi#Arctangent
    numer = pow(2, n + 1)
    denom = gmpy2.bincoef(n + n, n) * (n + n + 1)
    frac = gmpy2.mpq(numer, denom)
    pi += frac
    # Print every 1000 iterations
    if n % 1000 == 0:
        print(gmpy2.mpfr(pi))
bbayles
  • 4,389
  • 1
  • 26
  • 34