bignum.py 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #
  2. # These tests require gmpy and test the limits of the 32-bit build. The
  3. # limits of the 64-bit build are so large that they cannot be tested
  4. # on accessible hardware.
  5. #
  6. import sys
  7. from decimal import *
  8. from gmpy import mpz
  9. _PyHASH_MODULUS = sys.hash_info.modulus
  10. # hash values to use for positive and negative infinities, and nans
  11. _PyHASH_INF = sys.hash_info.inf
  12. _PyHASH_NAN = sys.hash_info.nan
  13. # _PyHASH_10INV is the inverse of 10 modulo the prime _PyHASH_MODULUS
  14. _PyHASH_10INV = pow(10, _PyHASH_MODULUS - 2, _PyHASH_MODULUS)
  15. def xhash(coeff, exp):
  16. sign = 1
  17. if coeff < 0:
  18. sign = -1
  19. coeff = -coeff
  20. if exp >= 0:
  21. exp_hash = pow(10, exp, _PyHASH_MODULUS)
  22. else:
  23. exp_hash = pow(_PyHASH_10INV, -exp, _PyHASH_MODULUS)
  24. hash_ = coeff * exp_hash % _PyHASH_MODULUS
  25. ans = hash_ if sign == 1 else -hash_
  26. return -2 if ans == -1 else ans
  27. x = mpz(10) ** 425000000 - 1
  28. coeff = int(x)
  29. d = Decimal('9' * 425000000 + 'e-849999999')
  30. h1 = xhash(coeff, -849999999)
  31. h2 = hash(d)
  32. assert h2 == h1