PageRenderTime 22ms CodeModel.GetById 15ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/test3/test_large.py

http://gmpy.googlecode.com/
Python | 31 lines | 22 code | 5 blank | 4 comment | 4 complexity | 477c608e1ded1fca6995cdd3e70e5344 MD5 | raw file
 1from gmpy2 import *
 2from math import log
 3from time import time
 4
 5# This test is designed to detect issues when allocating memory for large
 6# numbers. If it crashes and you need to work with very large numbers,
 7# you will need to compile GMP from scratch and try a different memory
 8# allocation option.
 9
10
11
12def pi(N):
13    print("Computing pi to %s decimal places." % N)
14    start = time()
15    N = int(round(log(10,2)*N))
16    sq2 = fsqrt(mpf(2, N))
17    a = mpz(6) - 4*sq2
18    y = sq2-1
19    for k in range(0, 10000):
20        xx = fsqrt(fsqrt(1-y**4))
21        y = (1-xx)/(1+xx)
22        anew = a*(1+y)**4 - 2**(2*k+3)*y*(1+y+y**2)
23        if anew == a:
24            break
25        a = anew
26    print("Computation took %5.2f seconds." % (time() - start))
27    return 1/a
28
29if __name__ == '__main__':
30    print("Testing operations with large numbers.")
31    pi(1000000)