MPZ related input/output ======================== >>> import gmpy2 as G >>> from gmpy2 import mpz, mpq, mpfr, mpc >>> from decimal import Decimal as D >>> from fractions import Fraction as F >>> a = mpz(123) >>> b = mpz(456) Test mpz construction --------------------- >>> mpz(3.14) mpz(3) >>> mpz(mpq(17,3)) mpz(5) >>> mpz(23) mpz(23) >>> mpz(-23) mpz(-23) >>> x=1000*1000*1000*1000*1000*1000*1000 >>> print(mpz(x)) 1000000000000000000000 >>> mpz(0.0) mpz(0) >>> mpz(0.0) mpz(0) >>> mpz(-0.0) mpz(0) >>> mpz(float("nan")) Traceback (most recent call last): File "", line 1, in ValueError: 'mpz' does not support NaN >>> mpz(float("inf")) Traceback (most recent call last): File "", line 1, in OverflowError: 'mpz' does not support Infinity >>> mpz(float("-inf")) Traceback (most recent call last): File "", line 1, in OverflowError: 'mpz' does not support Infinity >>> mpz("0") mpz(0) >>> mpz("-0") mpz(0) >>> mpz("hi") Traceback (most recent call last): File "", line 1, in ValueError: invalid digits >>> mpz("123456", 7) mpz(22875) >>> mpz("123456", base=3) Traceback (most recent call last): File "", line 1, in ValueError: invalid digits >>> mpz() mpz(0) >>> mpz(F(1,2)) mpz(0) >>> mpz(F(-3,2)) mpz(-1) >>> mpz(F(3,2)) mpz(1) >>> mpz(D("0")) mpz(0) >>> mpz(D("-0")) mpz(0) >>> mpz(D("1")) mpz(1) >>> mpz(D("1e10")) == mpz(10000000000) True >>> mpz(D("nan")) Traceback (most recent call last): File "", line 1, in File "/opt/local/lib/python3.2/decimal.py", line 1564, in __int__ raise ValueError("Cannot convert NaN to integer") ValueError: Cannot convert NaN to integer >>> mpz(D("inf")) Traceback (most recent call last): File "", line 1, in File "/opt/local/lib/python3.2/decimal.py", line 1566, in __int__ raise OverflowError("Cannot convert infinity to integer") OverflowError: Cannot convert infinity to integer >>> mpz(D("-inf")) Traceback (most recent call last): File "", line 1, in File "/opt/local/lib/python3.2/decimal.py", line 1566, in __int__ raise OverflowError("Cannot convert infinity to integer") OverflowError: Cannot convert infinity to integer >>> G.mpz('043') mpz(43) >>> G.mpz('43',0) mpz(43) >>> G.mpz('0o43') mpz(35) >>> G.mpz('0x43') mpz(67) >>> G.mpz('0x43',10) Traceback (innermost last): ... ValueError: invalid digits >>> G.mpz('43') mpz(43) Test format ----------- >>> str(a) '123' >>> repr(a) 'mpz(123)' >>> hex(a) '0x7b' >>> oct(a) == ('0o173' if sys.version_info[0] == 3 else '0173') True >>> G.mpz('123') mpz(123) >>> G.mpz('1001001011',2) mpz(587) >>> bin(G.mpz('1001001011',2)) '0b1001001011' >>> '1001001011' == G.mpz('1001001011',2).digits(2) True >>> for i in range(2,63): ... print(a.digits(i)) ... 1111011 11120 1323 443 323 234 173 146 123 102 a3 96 8b 83 7b 74 6f 69 63 5i 5d 58 53 4n 4j 4f 4b 47 43 3u 3r 3o 3l 3i 3f 3C 39 36 33 30 2d 2b 2Z 2X 2V 2T 2R 2P 2N 2L 2J 2H 2F 2D 2B 29 27 25 23 21 1z >>> print(a.digits(63)) Traceback (innermost last): ... ValueError: base must be either 0 or in the interval 2 ... 62 >>> a.__format__() Traceback (most recent call last): File "", line 1, in TypeError: function takes exactly 1 argument (0 given) >>> a.__format__('') '123' >>> a.__format__('d') '123' >>> a.__format__('b') '1111011' >>> a.__format__('o') '173' >>> a.__format__('x') '7b' >>> a.__format__('#x') '0x7b' >>> a.__format__('#X') '0X7B' >>> a.__format__('#o') '0o173' >>> a.__format__('#15o') ' 0o173' >>> a.__format__('<#15o') '0o173 ' >>> a.__format__('^#15o') ' 0o173 ' >>> a.__format__('>#15o') ' 0o173' >>> a.__format__('^ #15o') ' 0o173 ' >>> a.__format__('^#15o') ' 0o173 ' >>> a.__format__('^ #16o') ' 0o173 ' >>> a.__format__('#^16o') Traceback (most recent call last): File "", line 1, in ValueError: Invalid conversion specification >>> a.__format__('^#16o') ' 0o173 '