diff --git a/.idea/Numerical.iml b/.idea/Numerical.iml new file mode 100644 index 0000000..4f21c2d --- /dev/null +++ b/.idea/Numerical.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..cd9d35b --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..99ab671 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..6564d52 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..a39b29c --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,43 @@ + + + + + + + + + + + + + + + + + + + + + + + + 1434792298659 + + + + + + + + + + \ No newline at end of file diff --git a/chap1/1_3_infiniteseries.py b/chap1/1_3_infiniteseries.py new file mode 100644 index 0000000..4c4ed0b --- /dev/null +++ b/chap1/1_3_infiniteseries.py @@ -0,0 +1,48 @@ +__author__ = 'chunk' + +import struct + + +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): + # 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('!d', num)] + print bits + return ''.join(bits) + + +def infiniteseries(): + s = 0 + tmp = -1 + i = 0 + while float(s) != float(tmp): + i += 1 + tmp = s + s += float(1.0 / i) + print tmp,s + + print i, s, tmp + + +if __name__ == '__main__': + print float2bin(1.0) + infiniteseries() + pass diff --git a/chap1/__init__.py b/chap1/__init__.py new file mode 100644 index 0000000..a1459cf --- /dev/null +++ b/chap1/__init__.py @@ -0,0 +1 @@ +__author__ = 'chunk' diff --git a/main.py b/main.py new file mode 100644 index 0000000..a1459cf --- /dev/null +++ b/main.py @@ -0,0 +1 @@ +__author__ = 'chunk' -- libgit2 0.21.2