-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtesting.py
47 lines (31 loc) · 1001 Bytes
/
testing.py
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
import math
def binary_list(num):
arr = []
int_part = int(num)
fractional_part = num - int_part
fractional_binary = []
while fractional_part:
fractional_part *= 2
bit = int(fractional_part)
fractional_binary.append(bit)
fractional_part -= bit
if len(fractional_binary) > 56: # Limit the length to avoid infinite loop
break
if int_part != 0:
int_part = bin(int_part).replace("0b", "")
for i in (int_part):
arr.append(int(i))
full_num = arr + fractional_binary # Correctly concatenate the integer and fractional parts
exponent = -len(arr)
while full_num[0] == 0:
full_num.pop(0)
exponent += 1
return full_num, exponent
num = 2.3
full_num, exponent = binary_list(num)
print(full_num)
print(exponent)
x_minus = full_num[:53]
x_minus_int = int(''.join(str(bit) for bit in x_minus), 2)
x_plus, x_plus_ex = binary_list(x_minus_int)
print(x_plus)