summaryrefslogtreecommitdiffstatshomepage
path: root/tests/float/float2int.py
blob: 46296428742e6cabba1e5925837ad4e39c85947a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# check cases converting float to int, relying only on single precision float

print(int(14187745.))
print(int(2.**100))
print("%d" % 14187745.)
print("%d" % 2.**100)

testpass = True
for i in range(0,128):
    bitcnt = len(bin(int(2.**i))) - 3;
    if i != bitcnt:
        print('fail: 2**%u was %u bits long' % (i, bitcnt));
        testpass = False
print("power of  2 test: %s" % (testpass and 'passed' or 'failed'))

# TODO why does 10**12 fail this test for single precision float?
testpass = True
for i in range(0,12):
    digcnt = len(str(int(10.**i))) - 1;
    if i != digcnt:
        print('fail: 10**%u was %u digits long' % (i, digcnt));
        testpass = False
print("power of 10 test: %s" % (testpass and 'passed' or 'failed'))

# test inf conversion
try:
    int(float('inf'))
except OverflowError:
    print("OverflowError")

# test nan conversion
try:
    int(float('nan'))
except ValueError:
    print("ValueError")

# test numbers < 1 (this used to fail; see issue #1044)
import struct
struct.pack('I', int(1/2))