/test2/test_large.py

http://gmpy.googlecode.com/ · Python · 31 lines · 22 code · 5 blank · 4 comment · 3 complexity · 01d733e295065de12a6100b2d8b70464 MD5 · raw file

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