[a32498]: / python / maths.py

Download this file

78 lines (67 with data), 2.0 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
72
73
74
75
76
77
import struct
value = struct.pack('>hh', 125, 148)
print([value, ])
print([struct.unpack('f', value)])
def bits(bytes):
print("+++++++++++++++++++")
"""
Helper function to get a tuple of the bits in a byte.
:param byte: The byte from which to get the bits.
:return: Tuple of bits in the byte param.
"""
value = 0
bits_list = []
packed = []
for byte in bytes:
level = 0
print(byte)
bit_list = []
for i in range(8):
level <<= 1
bit_list.append(byte >> i & 1)
level |= byte >> i & 1
print(level)
print(bit_list)
value += level
print(value)
packed.append(struct.unpack('f', struct.pack('>l', value)))
print(packed)
print("math")
# print(packed[0][0] + packed[1][0])
# print(packed[0][1] / packed[0][0] * 16)
# print(packed[0][0] * packed[0][1] * packed[1][0] * packed[1][1] * 16)
# print(packed[0] / packed[1])
# print((packed[0] / packed[1]) / 128)
# print((packed[0] / packed[1]) / 256)
# print(packed[0] * packed[1])
# print("flopped")
# print(packed[1] + packed[0])
# print((packed[1] + packed[0]) / 128)
# print(packed[1] / packed[0])
# print((packed[1] / packed[0]) / 128)
# print((packed[1] / packed[0]) / 256)
# print(packed[1] * packed[0])
# print("mangled")
# mange = packed[1] + packed[0]
# added_mod = struct.pack('l', mange)
# print([added_mod, ])
# print(struct.unpack('d', added_mod))
print("------------------")
return value
b = bits([227, 125, ])
b = bits([6, 126, ])
print(b)
'4080.00389105'
'4080.99610901'
'4335.0000152'
'4335.003891'
'4335.99609375'
def bits_to_float(b):
print('bits to float')
print('b: {}'.format(b))
print('bj: {}'.format("".join(b)))
b = "".join(b)
print('a: {}'.format(b))
# s = struct.pack('L', b)
# print("s: {}".format(s))
return struct.unpack('>d', b)[0]