Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
allendowney
GitHub Repository: allendowney/cpython
Path: blob/main/Modules/_decimal/tests/bignum.py
12 views
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
7
import sys
8
from decimal import *
9
from gmpy import mpz
10
11
12
_PyHASH_MODULUS = sys.hash_info.modulus
13
# hash values to use for positive and negative infinities, and nans
14
_PyHASH_INF = sys.hash_info.inf
15
_PyHASH_NAN = sys.hash_info.nan
16
17
# _PyHASH_10INV is the inverse of 10 modulo the prime _PyHASH_MODULUS
18
_PyHASH_10INV = pow(10, _PyHASH_MODULUS - 2, _PyHASH_MODULUS)
19
20
def xhash(coeff, exp):
21
sign = 1
22
if coeff < 0:
23
sign = -1
24
coeff = -coeff
25
if exp >= 0:
26
exp_hash = pow(10, exp, _PyHASH_MODULUS)
27
else:
28
exp_hash = pow(_PyHASH_10INV, -exp, _PyHASH_MODULUS)
29
hash_ = coeff * exp_hash % _PyHASH_MODULUS
30
ans = hash_ if sign == 1 else -hash_
31
return -2 if ans == -1 else ans
32
33
34
x = mpz(10) ** 425000000 - 1
35
coeff = int(x)
36
37
d = Decimal('9' * 425000000 + 'e-849999999')
38
39
h1 = xhash(coeff, -849999999)
40
h2 = hash(d)
41
42
assert h2 == h1
43
44