Skip to content

Commit 50e0a7b

Browse files
committed
test/float2int_fp30: Variant of float2int for 30-bit stuffed float.
1 parent fbb3c19 commit 50e0a7b

1 file changed

Lines changed: 89 additions & 0 deletions

File tree

tests/float/float2int_fp30.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# check cases converting float to int, relying only on single precision float
2+
3+
try:
4+
import ustruct as struct
5+
except:
6+
import struct
7+
8+
# work out configuration values
9+
is_64bit = struct.calcsize("P") == 8
10+
# 0 = none, 1 = long long, 2 = mpz
11+
try:
12+
dummy = 0x7fffffffffffffff
13+
try:
14+
if (0xffffffffffffffff + 1) > 0:
15+
ll_type = 2
16+
else:
17+
ll_type = 1
18+
except:
19+
# in case the sum in the if statement above changes to raising an exception on overflow
20+
ll_type = 1
21+
except:
22+
ll_type = 0
23+
24+
# basic conversion
25+
print(int(14187744.))
26+
print("%d" % 14187744.)
27+
if ll_type == 2:
28+
print(int(2.**100))
29+
print("%d" % 2.**100)
30+
31+
testpass = True
32+
p2_rng = ((30,63,127),(62,63,127))[is_64bit][ll_type]
33+
for i in range(0,p2_rng):
34+
bitcnt = len(bin(int(2.**i))) - 3;
35+
if i != bitcnt:
36+
print('fail: 2.**%u was %u bits long' % (i, bitcnt));
37+
testpass = False
38+
print("power of 2 test: %s" % (testpass and 'passed' or 'failed'))
39+
40+
# TODO why does 10**12 fail this test for single precision float?
41+
testpass = True
42+
p10_rng = 9
43+
for i in range(0,p10_rng):
44+
digcnt = len(str(int(10.**i))) - 1;
45+
if i != digcnt:
46+
print('fail: 10.**%u was %u digits long' % (i, digcnt));
47+
testpass = False
48+
print("power of 10 test: %s" % (testpass and 'passed' or 'failed'))
49+
50+
def fp2int_test(num, name, should_fail):
51+
try:
52+
x = int(num)
53+
passed = ~should_fail
54+
except:
55+
passed = should_fail
56+
print('%s: %s' % (name, passed and 'passed' or 'failed'))
57+
58+
if ll_type != 2:
59+
if ll_type == 0:
60+
if is_64bit:
61+
neg_bad_fp = -1.00000005*2.**62.
62+
pos_bad_fp = 2.**62.
63+
neg_good_fp = -2.**62.
64+
pos_good_fp = 0.99999993*2.**62.
65+
else:
66+
neg_bad_fp = -1.00000005*2.**30.
67+
pos_bad_fp = 2.**30.
68+
neg_good_fp = -2.**30.
69+
pos_good_fp = 0.9999999499*2.**30.
70+
else:
71+
neg_bad_fp = -0.51*2.**64.
72+
pos_bad_fp = 2.**63.
73+
neg_good_fp = -2.**63.
74+
pos_good_fp = 1.9999998*2.**62.
75+
76+
fp2int_test(neg_bad_fp, 'neg bad', True)
77+
fp2int_test(pos_bad_fp, 'pos bad', True)
78+
fp2int_test(neg_good_fp, 'neg good', False)
79+
fp2int_test(pos_good_fp, 'pos good', False)
80+
else:
81+
fp2int_test(-1.999999879*2.**126., 'large neg', False)
82+
fp2int_test(1.999999879*2.**126., 'large pos', False)
83+
84+
fp2int_test(float('inf'), 'inf test', True)
85+
fp2int_test(float('nan'), 'NaN test', True)
86+
87+
# test numbers < 1 (this used to fail; see issue #1044)
88+
fp2int_test(0.0001, 'small num', False)
89+
struct.pack('I', int(1/2))

0 commit comments

Comments
 (0)