1_3_infinite.py
1.53 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
__author__ = 'chunk'
import struct
import numpy as np
from common import *
def float2bits(f, fmt='bin'):
if fmt == 'hex':
return hex(struct.unpack('!l', struct.pack('!f', f))[0])
return bin(struct.unpack('!l', struct.pack('!f', f))[0])
def double2bits(d, fmt='bin'):
if fmt == 'hex':
return hex(struct.unpack('!q', struct.pack('!d', d))[0])
return bin(struct.unpack('!q', struct.pack('!d', d))[0])
def float2bin(num):
# http://stackoverflow.com/questions/16444726/binary-representation-of-float-in-python-bits-not-hex
bits = [bin(ord(c)).replace('0b', '').rjust(8, '0') for c in struct.pack('!f', num)]
print bits
return ''.join(bits)
def double2bin(num):
bits = [bin(ord(c)).replace('0b', '').rjust(8, '0') for c in struct.pack('!d', num)]
print bits
return ''.join(bits)
def infinite_float():
s = np.float32(0)
tmp = np.float32(-1)
i = 0
while s != tmp:
i += 1
tmp = s
s += np.float32(1.0 / i)
print i, s, tmp, float2bin(s)
def infinite_double(n):
s = np.float64(0)
for i in range(1, n + 1):
s += np.float64(1.0 / i)
return s
def testn0():
for i in range(100):
print i, np.log(i)
if np.log(i) >= 8:
print i
if __name__ == '__main__':
# testn0()
# infinite_float()
# i = 1
# while infinite_double(i) < 16:
# i += 1
# print i, infinite_double(i)
timer = Timer()
timer.mark()
infinite_double(10000000) # 1125899906842624
timer.report()
pass