Test gmpy2_mpz_misc.c
=====================
>>> import gmpy2
>>> from gmpy2 import mpz
>>> import math
>>> a = mpz(123)
>>> b = mpz(456)
Test num_digits
---------------
>>> mpz(123456).num_digits()
6
>>> mpz(123456).num_digits(2)
17
>>> mpz(123456).num_digits(-1)
Traceback (most recent call last):
...
ValueError:
>>> mpz(123456).num_digits(9999999999999999999999999999999999)
Traceback (most recent call last):
...
OverflowError:
>>> mpz(123456).num_digits(100)
Traceback (most recent call last):
...
ValueError:
>>> gmpy2.num_digits(123456,-1,7)
Traceback (most recent call last):
...
TypeError:
>>> gmpy2.num_digits(123456,-1)
Traceback (most recent call last):
...
ValueError:
>>> gmpy2.num_digits('123456')
Traceback (most recent call last):
...
TypeError:
>>> gmpy2.num_digits(123456,100)
Traceback (most recent call last):
...
ValueError:
>>> gmpy2.num_digits(123456,'a')
Traceback (most recent call last):
...
TypeError:
>>> gmpy2.num_digits(123456)
6
>>> gmpy2.num_digits(123456,2)
17
Test round (__round__)
----------------------
>>> round(mpz(123456),'a')
Traceback (most recent call last):
...
TypeError:
>>> round(mpz(123456),'a',4)
Traceback (most recent call last):
...
TypeError:
Test bool
---------
>>> bool(mpz(100))
True
>>> bool(mpz(0))
False
>>> bool(mpz(-100))
True
Test gcd
--------
>>> gmpy2.gcd(1,2,3)
mpz(1)
>>> gmpy2.gcd(2, 4, 6)
mpz(2)
>>> gmpy2.gcd(1,'a')
Traceback (most recent call last):
...
TypeError:
>>> gmpy2.gcd(123,456)
mpz(3)
>>> gmpy2.gcd(a,b)
mpz(3)
Test lcm
--------
>>> gmpy2.lcm(1,2,3)
mpz(6)
>>> gmpy2.lcm(2, 3, 4)
mpz(12)
>>> gmpy2.lcm(1,'a')
Traceback (most recent call last):
...
TypeError:
>>> gmpy2.lcm(a,b)
mpz(18696)
>>> gmpy2.lcm(123,456)
mpz(18696)
Test gcdext
-----------
>>> gmpy2.gcdext(1,2,3)
Traceback (most recent call last):
...
TypeError:
>>> gmpy2.gcdext(1,'a')
Traceback (most recent call last):
...
TypeError:
>>> temp=gmpy2.gcdext(a,b)
>>> temp[0]==a*temp[1]+b*temp[2]
True
>>> temp=gmpy2.gcdext(123,456)
>>> temp[0]==a*temp[1]+b*temp[2]
True
>>> del temp
Test divm
---------
>>> gmpy2.divm(b,a,20)
mpz(12)
>>> gmpy2.divm(a,b,100,5)
Traceback (innermost last):
...
TypeError:
>>> gmpy2.divm(a,b,'a')
Traceback (innermost last):
...
TypeError:
>>> gmpy2.divm(a,b,100)
Traceback (innermost last):
...
ZeroDivisionError:
>>> gmpy2.divm(6,12,14)
mpz(4)
>>> gmpy2.divm(0,1,2)
mpz(0)
>>> gmpy2.divm(4,8,20)
mpz(3)
Test fac
--------
>>> gmpy2.fac(-7)
Traceback (most recent call last):
...
OverflowError: can't convert negative value to unsigned long
>>> gmpy2.fac('a')
Traceback (most recent call last):
...
TypeError:
>>> gmpy2.fac(7)
mpz(5040)
Test double_fac
---------------
>>> gmpy2.double_fac(-7)
Traceback (most recent call last):
...
OverflowError: can't convert negative value to unsigned long
>>> gmpy2.double_fac('a')
Traceback (most recent call last):
...
TypeError:
>>> gmpy2.double_fac(7)
mpz(105)
>>> gmpy2.double_fac(7) * gmpy2.double_fac(8)
mpz(40320)
>>> gmpy2.fac(8)
mpz(40320)
Test primorial
--------------
>>> gmpy2.primorial(-7)
Traceback (most recent call last):
...
OverflowError: can't convert negative value to unsigned long
>>> gmpy2.primorial('a')
Traceback (most recent call last):
...
TypeError:
>>> gmpy2.primorial(7)
mpz(210)
Test multi_fac
--------------
>>> gmpy2.multi_fac(-7)
Traceback (most recent call last):
...
TypeError:
>>> gmpy2.multi_fac(7,'a')
Traceback (most recent call last):
...
TypeError:
>>> gmpy2.multi_fac(7,-1)
Traceback (most recent call last):
...
OverflowError: can't convert negative value to unsigned long
>>> gmpy2.multi_fac(-7,1)
Traceback (most recent call last):
...
OverflowError: can't convert negative value to unsigned long
>>> gmpy2.multi_fac('a')
Traceback (most recent call last):
...
TypeError:
>>> gmpy2.multi_fac(10)
Traceback (most recent call last):
...
TypeError:
>>> gmpy2.multi_fac(10,11,12)
Traceback (most recent call last):
...
TypeError:
>>> gmpy2.multi_fac(17,4)
mpz(9945)
Test fib
--------
>>> gmpy2.fib(-2)
Traceback (most recent call last):
...
OverflowError: can't convert negative value to unsigned long
>>> gmpy2.fib(17)
mpz(1597)
Test fib2
---------
>>> gmpy2.fib2(-2)
Traceback (most recent call last):
...
OverflowError: can't convert negative value to unsigned long
>>> gmpy2.fib2(17)
(mpz(1597), mpz(987))
Test lucas
----------
>>> gmpy2.lucas(-2)
Traceback (most recent call last):
...
OverflowError: can't convert negative value to unsigned long
>>> gmpy2.lucas(17)
mpz(3571)
Test lucas2
-----------
>>> gmpy2.lucas2(-2)
Traceback (most recent call last):
...
OverflowError: can't convert negative value to unsigned long
>>> gmpy2.lucas2(17)
(mpz(3571), mpz(2207))
Test bincoef
------------
>>> gmpy2.bincoef(1)
Traceback (most recent call last):
...
TypeError:
>>> gmpy2.bincoef(1,2,3)
Traceback (most recent call last):
...
TypeError:
>>> for i in range(10):
... print(gmpy2.bincoef(10,i))
...
1
10
45
120
210
252
210
120
45
10
Test comb
---------
>>> gmpy2.comb(3,-1)
Traceback (most recent call last):
...
OverflowError: can't convert negative value to unsigned long
>>> gmpy2.comb('a',4)
Traceback (most recent call last):
...
TypeError:
>>> gmpy2.comb(8,4)
mpz(70)
Test isqrt
----------
>>> print(gmpy2.isqrt(a))
11
>>> print(gmpy2.isqrt(123))
11
>>> gmpy2.isqrt(-1)
Traceback (most recent call last):
...
ValueError:
>>> gmpy2.isqrt(mpz(-1))
Traceback (most recent call last):
...
ValueError:
>>> gmpy2.isqrt('a')
Traceback (most recent call last):
...
TypeError:
Test isqrt_rem
--------------
>>> print(gmpy2.isqrt_rem(a))
(mpz(11), mpz(2))
>>> print(gmpy2.isqrt_rem(b))
(mpz(21), mpz(15))
>>> gmpy2.isqrt_rem(-1)
Traceback (most recent call last):
...
ValueError:
>>> gmpy2.isqrt_rem(mpz(-1))
Traceback (most recent call last):
...
ValueError:
>>> gmpy2.isqrt_rem('a')
Traceback (most recent call last):
...
TypeError:
>>> gmpy2.isqrt_rem(mpz(-1))
Traceback (most recent call last):
...
ValueError:
Test remove
-----------
>>> gmpy2.remove(a,2)
(mpz(123), 0)
>>> gmpy2.remove(a,mpz(2))
(mpz(123), 0)
>>> gmpy2.remove(a,3)
(mpz(41), 1)
>>> gmpy2.remove(b,2)
(mpz(57), 3)
>>> gmpy2.remove(b,3)
(mpz(152), 1)
>>> gmpy2.remove(b,1)
Traceback (most recent call last):
...
ValueError:
>>> gmpy2.remove(b,mpz(1))
Traceback (most recent call last):
...
ValueError:
>>> gmpy2.remove(b,0)
Traceback (most recent call last):
...
ValueError:
>>> gmpy2.remove(b,789)
(mpz(456), 0)
>>> gmpy2.remove(b,-3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError:
>>> gmpy2.remove(b,float('NaN'))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError:
>>> gmpy2.remove(3,-1)
Traceback (most recent call last):
...
ValueError:
>>> gmpy2.remove(3)
Traceback (innermost last):
...
TypeError:
>>> gmpy2.remove()
Traceback (innermost last):
...
TypeError:
Test invert
-----------
>>> gmpy2.invert(a,100)
mpz(87)
>>> gmpy2.invert(a,mpz(100))
mpz(87)
>>> gmpy2.invert(b,mpz(100))
Traceback (most recent call last):
...
ZeroDivisionError:
>>> gmpy2.invert(b,mpz(0))
Traceback (most recent call last):
...
ZeroDivisionError:
>>> gmpy2.invert(3)
Traceback (innermost last):
...
TypeError:
>>> gmpy2.invert()
Traceback (innermost last):
...
TypeError:
>>> gmpy2.invert(456,0)
Traceback (most recent call last):
...
ZeroDivisionError:
>>> gmpy2.invert(456,'a')
Traceback (most recent call last):
...
TypeError:
>>> gmpy2.invert(456,100)
Traceback (most recent call last):
...
ZeroDivisionError:
>>> gmpy2.invert(123,100)
mpz(87)
Test divexact
-------------
>>> gmpy2.divexact(2)
Traceback (innermost last):
...
TypeError:
>>> gmpy2.divexact(2, 'a')
Traceback (innermost last):
...
TypeError:
>>> gmpy2.divexact(a,0)
Traceback (most recent call last):
...
ZeroDivisionError:
>>> gmpy2.divexact(a,mpz(0))
Traceback (most recent call last):
...
ZeroDivisionError:
>>> gmpy2.divexact(123,0)
Traceback (most recent call last):
...
ZeroDivisionError:
>>> aa=