Diff of /python/maths.py [000000] .. [a32498]

Switch to unified view

a b/python/maths.py
1
import struct
2
3
value = struct.pack('>hh', 125, 148)
4
print([value, ])
5
print([struct.unpack('f', value)])
6
7
8
def bits(bytes):
9
    print("+++++++++++++++++++")
10
    """
11
    Helper function to get a tuple of the bits in a byte.
12
    :param byte: The byte from which to get the bits.
13
    :return: Tuple of bits in the byte param.
14
    """
15
    value = 0
16
    bits_list = []
17
    packed = []
18
    for byte in bytes:
19
        level = 0
20
        print(byte)
21
        bit_list = []
22
        for i in range(8):
23
            level <<= 1
24
            bit_list.append(byte >> i & 1)
25
            level |= byte >> i & 1
26
            print(level)
27
        print(bit_list)
28
        value += level
29
        print(value)
30
    packed.append(struct.unpack('f', struct.pack('>l', value)))
31
32
    print(packed)
33
    print("math")
34
    # print(packed[0][0] + packed[1][0])
35
    # print(packed[0][1] / packed[0][0] * 16)
36
    # print(packed[0][0] * packed[0][1] * packed[1][0] * packed[1][1] * 16)
37
    # print(packed[0] / packed[1])
38
    # print((packed[0] / packed[1]) / 128)
39
    # print((packed[0] / packed[1]) / 256)
40
    # print(packed[0] * packed[1])
41
    # print("flopped")
42
    # print(packed[1] + packed[0])
43
    # print((packed[1] + packed[0]) / 128)
44
    # print(packed[1] / packed[0])
45
    # print((packed[1] / packed[0]) / 128)
46
    # print((packed[1] / packed[0]) / 256)
47
    # print(packed[1] * packed[0])
48
    # print("mangled")
49
    # mange = packed[1] + packed[0]
50
51
    # added_mod = struct.pack('l', mange)
52
    # print([added_mod, ])
53
    # print(struct.unpack('d', added_mod))
54
55
    print("------------------")
56
    return value
57
58
59
b = bits([227, 125, ])
60
b = bits([6, 126, ])
61
print(b)
62
'4080.00389105'
63
'4080.99610901'
64
'4335.0000152'
65
'4335.003891'
66
'4335.99609375'
67
68
69
def bits_to_float(b):
70
    print('bits to float')
71
    print('b: {}'.format(b))
72
    print('bj: {}'.format("".join(b)))
73
    b = "".join(b)
74
    print('a: {}'.format(b))
75
    # s = struct.pack('L', b)
76
    # print("s: {}".format(s))
77
    return struct.unpack('>d', b)[0]