prog2_bity_w_double

1954 days ago by macieksk

import struct, numpy as np def write_bin(number, fname='data/plik.txt'): f1 = open(fname,'w') f1.write(struct.pack("d",number)) #f1.write("".join(reversed(struct.pack("d",number)))) # big endian f1.close() write_bin(0.1) 
       
import os os.system('hexdump -b data/plik.txt') 
       
0000000 232 231 231 231 231 231 271 077                             

0000008
0
write_bin(0.1) a=np.fromfile('data/plik.txt',dtype=np.uint64) a 
       
array([4591870180066957722], dtype=uint64)
def to_bits(num): bits = list(reversed([(num>>i&1) for i in range(64)])) return {'sign':str(bits[0]), 'wykladnik':"".join(map(str,bits[1:12])), 'mantysa':"".join(map(str,bits[12:]))} write_bin(0.1) a=np.fromfile('data/plik.txt',dtype=np.uint64) to_bits(a[0]) 
       
{'wykladnik': '01111111011', 'mantysa':
'1001100110011001100110011001100110011001100110011010', 'sign': '0'}
write_bin(-0.1) a=np.fromfile('data/plik.txt',dtype=np.uint64) to_bits(a[0]) # widac roznice na bicie znaku 
       
{'wykladnik': '01111111011', 'mantysa':
'1001100110011001100110011001100110011001100110011010', 'sign': '1'}
write_bin(1) a=np.fromfile('data/plik.txt',dtype=np.uint64) to_bits(a[0]) 
       
{'wykladnik': '01111111111', 'mantysa':
'0000000000000000000000000000000000000000000000000000', 'sign': '0'}
write_bin(0) a=np.fromfile('data/plik.txt',dtype=np.uint64) to_bits(a[0]) 
       
{'wykladnik': '00000000000', 'mantysa':
'0000000000000000000000000000000000000000000000000000', 'sign': '0'}
write_bin(2) a=np.fromfile('data/plik.txt',dtype=np.uint64) to_bits(a[0]) 
       
{'wykladnik': '10000000000', 'mantysa':
'0000000000000000000000000000000000000000000000000000', 'sign': '0'}
write_bin(4) a=np.fromfile('data/plik.txt',dtype=np.uint64) to_bits(a[0]) 
       
{'wykladnik': '10000000001', 'mantysa':
'0000000000000000000000000000000000000000000000000000', 'sign': '0'}
write_bin(8) a=np.fromfile('data/plik.txt',dtype=np.uint64) to_bits(a[0]) 
       
{'wykladnik': '10000000010', 'mantysa':
'0000000000000000000000000000000000000000000000000000', 'sign': '0'}
write_bin(1./2) a=np.fromfile('data/plik.txt',dtype=np.uint64) to_bits(a[0]) 
       
{'wykladnik': '01111111110', 'mantysa':
'0000000000000000000000000000000000000000000000000000', 'sign': '0'}
write_bin(1./8) a=np.fromfile('data/plik.txt',dtype=np.uint64) to_bits(a[0]) 
       
{'wykladnik': '01111111100', 'mantysa':
'0000000000000000000000000000000000000000000000000000', 'sign': '0'}