-
Notifications
You must be signed in to change notification settings - Fork 207
Expand file tree
/
Copy pathbackend.py
More file actions
46 lines (39 loc) · 1.62 KB
/
backend.py
File metadata and controls
46 lines (39 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import fractions
import os
#----------------------------------------------------------------------------#
# Support GMPY for high-speed large integer arithmetic. #
# #
# To allow an external module to handle arithmetic, we need to make sure #
# that all high-precision variables are declared of the correct type. MPZ #
# is the constructor for the high-precision type. It defaults to Python's #
# long type but can be assinged another type, typically gmpy.mpz. #
# #
# MPZ must be used for the mantissa component of an mpf and must be used #
# for internal fixed-point operations. #
# #
# Side-effects: #
# * "is" cannot be used to test for special values. Must use "==". #
#----------------------------------------------------------------------------#
gmpy = None
BACKEND = 'python'
MPZ = int
MPQ = fractions.Fraction
if 'MPMATH_NOGMPY' not in os.environ:
try:
import gmpy2 as gmpy
BACKEND = 'gmpy'
MPQ = gmpy.mpq
except ImportError:
try:
import gmp as gmpy
BACKEND = 'gmp'
except ImportError:
pass
if gmpy:
MPZ = gmpy.mpz
MPZ_ZERO = MPZ(0)
MPZ_ONE = MPZ(1)
MPZ_TWO = MPZ(2)
MPZ_THREE = MPZ(3)
MPZ_FIVE = MPZ(5)
int_types = (int,) if BACKEND == 'python' else (int, MPZ)