2015-01-07 07:10:47 -05:00
|
|
|
# check cases converting float to int, relying only on single precision float
|
2014-12-29 17:22:10 -05:00
|
|
|
|
2022-08-18 02:57:45 -04:00
|
|
|
import struct
|
|
|
|
import sys
|
2017-03-06 10:23:09 -05:00
|
|
|
|
|
|
|
maxsize_bits = 0
|
|
|
|
maxsize = sys.maxsize
|
|
|
|
while maxsize:
|
|
|
|
maxsize >>= 1
|
|
|
|
maxsize_bits += 1
|
|
|
|
|
2015-01-23 12:52:22 -05:00
|
|
|
# work out configuration values
|
2017-03-06 10:23:09 -05:00
|
|
|
is_64bit = maxsize_bits > 32
|
2015-01-23 12:52:22 -05:00
|
|
|
# 0 = none, 1 = long long, 2 = mpz
|
2017-03-06 10:23:09 -05:00
|
|
|
ll_type = None
|
|
|
|
if is_64bit:
|
|
|
|
if maxsize_bits < 63:
|
|
|
|
ll_type = 0
|
|
|
|
else:
|
|
|
|
if maxsize_bits < 31:
|
|
|
|
ll_type = 0
|
|
|
|
if ll_type is None:
|
|
|
|
one = 1
|
|
|
|
if one << 65 < one << 62:
|
2015-01-23 12:52:22 -05:00
|
|
|
ll_type = 1
|
2017-03-06 10:23:09 -05:00
|
|
|
else:
|
|
|
|
ll_type = 2
|
|
|
|
|
2015-01-23 12:52:22 -05:00
|
|
|
|
|
|
|
# basic conversion
|
2020-03-22 22:26:08 -04:00
|
|
|
# fmt: off
|
2015-01-07 07:10:47 -05:00
|
|
|
print(int(14187745.))
|
|
|
|
print("%d" % 14187745.)
|
2020-03-22 22:26:08 -04:00
|
|
|
# fmt: on
|
2015-01-23 12:52:22 -05:00
|
|
|
if ll_type == 2:
|
2022-02-02 00:11:25 -05:00
|
|
|
print(int(2.0**100))
|
|
|
|
print("%d" % 2.0**100)
|
2015-01-02 07:39:22 -05:00
|
|
|
|
|
|
|
testpass = True
|
2020-03-22 22:26:08 -04:00
|
|
|
p2_rng = ((30, 63, 127), (62, 63, 127))[is_64bit][ll_type]
|
|
|
|
for i in range(0, p2_rng):
|
2022-02-02 00:11:25 -05:00
|
|
|
bitcnt = len(bin(int(2.0**i))) - 3
|
2015-01-02 07:39:22 -05:00
|
|
|
if i != bitcnt:
|
2020-03-22 22:26:08 -04:00
|
|
|
print("fail: 2.**%u was %u bits long" % (i, bitcnt))
|
2015-01-02 07:39:22 -05:00
|
|
|
testpass = False
|
2020-03-22 22:26:08 -04:00
|
|
|
print("power of 2 test: %s" % (testpass and "passed" or "failed"))
|
2015-01-02 07:39:22 -05:00
|
|
|
|
2015-01-07 07:10:47 -05:00
|
|
|
# TODO why does 10**12 fail this test for single precision float?
|
2015-01-02 07:39:22 -05:00
|
|
|
testpass = True
|
2015-01-23 12:52:22 -05:00
|
|
|
p10_rng = 9 if (ll_type == 0 and ~is_64bit) else 11
|
2020-03-22 22:26:08 -04:00
|
|
|
for i in range(0, p10_rng):
|
2022-02-02 00:11:25 -05:00
|
|
|
digcnt = len(str(int(10.0**i))) - 1
|
2015-01-02 07:39:22 -05:00
|
|
|
if i != digcnt:
|
2020-03-22 22:26:08 -04:00
|
|
|
print("fail: 10.**%u was %u digits long" % (i, digcnt))
|
2015-01-02 07:39:22 -05:00
|
|
|
testpass = False
|
2020-03-22 22:26:08 -04:00
|
|
|
print("power of 10 test: %s" % (testpass and "passed" or "failed"))
|
|
|
|
|
2015-01-02 18:04:09 -05:00
|
|
|
|
2015-01-23 12:52:22 -05:00
|
|
|
def fp2int_test(num, name, should_fail):
|
|
|
|
try:
|
|
|
|
x = int(num)
|
|
|
|
passed = ~should_fail
|
|
|
|
except:
|
|
|
|
passed = should_fail
|
2020-03-22 22:26:08 -04:00
|
|
|
print("%s: %s" % (name, passed and "passed" or "failed"))
|
|
|
|
|
2015-01-02 18:04:09 -05:00
|
|
|
|
2015-01-23 12:52:22 -05:00
|
|
|
if ll_type != 2:
|
|
|
|
if ll_type == 0:
|
|
|
|
if is_64bit:
|
2022-02-02 00:11:25 -05:00
|
|
|
neg_bad_fp = -1.00000005 * 2.0**62.0
|
|
|
|
pos_bad_fp = 2.0**62.0
|
|
|
|
neg_good_fp = -(2.0**62.0)
|
|
|
|
pos_good_fp = 0.99999993 * 2.0**62.0
|
2015-01-23 12:52:22 -05:00
|
|
|
else:
|
2022-02-02 00:11:25 -05:00
|
|
|
neg_bad_fp = -1.00000005 * 2.0**30.0
|
|
|
|
pos_bad_fp = 2.0**30.0
|
|
|
|
neg_good_fp = -(2.0**30.0)
|
|
|
|
pos_good_fp = 0.9999999499 * 2.0**30.0
|
2015-01-23 12:52:22 -05:00
|
|
|
else:
|
2022-02-02 00:11:25 -05:00
|
|
|
neg_bad_fp = -0.51 * 2.0**64.0
|
|
|
|
pos_bad_fp = 2.0**63.0
|
|
|
|
neg_good_fp = -(2.0**63.0)
|
|
|
|
pos_good_fp = 1.9999998 * 2.0**62.0
|
2015-01-23 12:52:22 -05:00
|
|
|
|
2020-03-22 22:26:08 -04:00
|
|
|
fp2int_test(neg_bad_fp, "neg bad", True)
|
|
|
|
fp2int_test(pos_bad_fp, "pos bad", True)
|
|
|
|
fp2int_test(neg_good_fp, "neg good", False)
|
|
|
|
fp2int_test(pos_good_fp, "pos good", False)
|
2015-01-23 12:52:22 -05:00
|
|
|
else:
|
2022-02-02 00:11:25 -05:00
|
|
|
fp2int_test(-1.999999879 * 2.0**127.0, "large neg", False)
|
|
|
|
fp2int_test(1.999999879 * 2.0**127.0, "large pos", False)
|
2015-01-23 12:52:22 -05:00
|
|
|
|
2020-03-22 22:26:08 -04:00
|
|
|
fp2int_test(float("inf"), "inf test", True)
|
|
|
|
fp2int_test(float("nan"), "NaN test", True)
|
2015-01-07 07:10:47 -05:00
|
|
|
|
|
|
|
# test numbers < 1 (this used to fail; see issue #1044)
|
2020-03-22 22:26:08 -04:00
|
|
|
fp2int_test(0.0001, "small num", False)
|
|
|
|
struct.pack("I", int(1 / 2))
|