2023-03-23 09:24:11 -04:00
|
|
|
# to test arbitrary precision integers
|
2014-03-01 14:50:50 -05:00
|
|
|
|
|
|
|
x = 1000000000000000000000000000000
|
2015-11-22 15:11:01 -05:00
|
|
|
xn = -1000000000000000000000000000000
|
2014-03-01 14:50:50 -05:00
|
|
|
y = 2000000000000000000000000000000
|
|
|
|
|
|
|
|
# printing
|
|
|
|
print(x)
|
|
|
|
print(y)
|
2016-12-18 21:13:10 -05:00
|
|
|
print('%#X' % (x - x)) # print prefix
|
|
|
|
print('{:#,}'.format(x)) # print with commas
|
2014-03-01 14:50:50 -05:00
|
|
|
|
|
|
|
# addition
|
|
|
|
print(x + 1)
|
|
|
|
print(x + y)
|
2015-11-22 15:11:01 -05:00
|
|
|
print(x + xn == 0)
|
|
|
|
print(bool(x + xn))
|
2014-03-01 14:50:50 -05:00
|
|
|
|
|
|
|
# subtraction
|
|
|
|
print(x - 1)
|
|
|
|
print(x - y)
|
|
|
|
print(y - x)
|
2015-11-22 15:11:01 -05:00
|
|
|
print(x - x == 0)
|
|
|
|
print(bool(x - x))
|
2014-03-01 14:50:50 -05:00
|
|
|
|
|
|
|
# multiplication
|
|
|
|
print(x * 2)
|
|
|
|
print(x * y)
|
|
|
|
|
|
|
|
# integer division
|
|
|
|
print(x // 2)
|
|
|
|
print(y // x)
|
|
|
|
|
|
|
|
# bit inversion
|
|
|
|
print(~x)
|
|
|
|
print(~(-x))
|
|
|
|
|
|
|
|
# left shift
|
|
|
|
x = 0x10000000000000000000000
|
|
|
|
for i in range(32):
|
|
|
|
x = x << 1
|
|
|
|
print(x)
|
|
|
|
|
|
|
|
# right shift
|
|
|
|
x = 0x10000000000000000000000
|
|
|
|
for i in range(32):
|
|
|
|
x = x >> 1
|
|
|
|
print(x)
|
|
|
|
|
|
|
|
# left shift of a negative number
|
|
|
|
for i in range(8):
|
|
|
|
print(-10000000000000000000000000 << i)
|
|
|
|
print(-10000000000000000000000001 << i)
|
|
|
|
print(-10000000000000000000000002 << i)
|
|
|
|
print(-10000000000000000000000003 << i)
|
|
|
|
print(-10000000000000000000000004 << i)
|
|
|
|
|
|
|
|
# right shift of a negative number
|
|
|
|
for i in range(8):
|
|
|
|
print(-10000000000000000000000000 >> i)
|
|
|
|
print(-10000000000000000000000001 >> i)
|
|
|
|
print(-10000000000000000000000002 >> i)
|
|
|
|
print(-10000000000000000000000003 >> i)
|
|
|
|
print(-10000000000000000000000004 >> i)
|
2015-02-07 20:57:40 -05:00
|
|
|
|
2015-03-14 17:20:58 -04:00
|
|
|
# conversion from string
|
|
|
|
print(int("123456789012345678901234567890"))
|
|
|
|
print(int("-123456789012345678901234567890"))
|
|
|
|
print(int("123456789012345678901234567890abcdef", 16))
|
|
|
|
print(int("123456789012345678901234567890ABCDEF", 16))
|
2016-12-27 20:08:46 -05:00
|
|
|
print(int("1234567890abcdefghijklmnopqrstuvwxyz", 36))
|
2015-03-14 17:20:58 -04:00
|
|
|
|
2016-12-18 21:13:10 -05:00
|
|
|
# invalid characters in string
|
|
|
|
try:
|
|
|
|
print(int("123456789012345678901234567890abcdef"))
|
|
|
|
except ValueError:
|
|
|
|
print('ValueError');
|
2018-04-03 23:56:00 -04:00
|
|
|
try:
|
|
|
|
print(int("123456789012345678901234567890\x01"))
|
|
|
|
except ValueError:
|
|
|
|
print('ValueError');
|
2016-12-18 21:13:10 -05:00
|
|
|
|
2015-02-07 20:57:40 -05:00
|
|
|
# test constant integer with more than 255 chars
|
|
|
|
x = 0x84ce72aa8699df436059f052ac51b6398d2511e49631bcb7e71f89c499b9ee425dfbc13a5f6d408471b054f2655617cbbaf7937b7c80cd8865cf02c8487d30d2b0fbd8b2c4e102e16d828374bbc47b93852f212d5043c3ea720f086178ff798cc4f63f787b9c2e419efa033e7644ea7936f54462dc21a6c4580725f7f0e7d1aaaaaaa
|
|
|
|
print(x)
|
2015-04-22 11:52:03 -04:00
|
|
|
|
|
|
|
# test parsing ints just on threshold of small to big
|
|
|
|
# for 32 bit archs
|
|
|
|
x = 1073741823 # small
|
|
|
|
x = -1073741823 # small
|
|
|
|
x = 1073741824 # big
|
|
|
|
x = -1073741824 # big
|
2018-04-04 10:59:49 -04:00
|
|
|
# for nan-boxing with 47-bit small ints
|
|
|
|
print(int('0x3fffffffffff', 16)) # small
|
|
|
|
print(int('-0x3fffffffffff', 16)) # small
|
|
|
|
print(int('0x400000000000', 16)) # big
|
|
|
|
print(int('-0x400000000000', 16)) # big
|
2015-04-22 11:52:03 -04:00
|
|
|
# for 64 bit archs
|
|
|
|
x = 4611686018427387903 # small
|
|
|
|
x = -4611686018427387903 # small
|
|
|
|
x = 4611686018427387904 # big
|
|
|
|
x = -4611686018427387904 # big
|
2015-09-15 11:15:57 -04:00
|
|
|
|
|
|
|
# sys.maxsize is a constant mpz, so test it's compatible with dynamic ones
|
|
|
|
import sys
|
|
|
|
print(sys.maxsize + 1 - 1 == sys.maxsize)
|
2018-04-04 01:23:32 -04:00
|
|
|
|
|
|
|
# test extraction of big int value via mp_obj_get_int_maybe
|
|
|
|
x = 1 << 70
|
|
|
|
print('a' * (x + 4 - x))
|