-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_vectors.py
executable file
·83 lines (61 loc) · 1.84 KB
/
add_vectors.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
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
78
79
80
81
82
83
#!/usr/bin/env python
from create_posit import create_vector
ES = 3
def add_numbers(seed1, exp1, frac1, seed2, exp2, frac2):
cexp1 = seed1 * (2 ** ES) + exp1
cexp2 = seed2 * (2 ** ES) + exp2
if (cexp1 >= cexp2):
bigger_cexp = cexp1
smaller_cexp = cexp2
bigger_frac = frac1
smaller_frac = frac2
bigger_seed = seed1
else:
bigger_cexp = cexp2
smaller_cexp = cexp1
bigger_frac = frac2
smaller_frac = frac1
bigger_seed = seed2
exp_diff = bigger_cexp - smaller_cexp
smaller_frac = smaller_frac * (2 ** (-exp_diff))
frac = smaller_frac + bigger_frac
exp = bigger_cexp - (bigger_seed * 2**ES)
seed = bigger_seed
if frac >= 2:
frac = frac / 2
exp = exp +1
if exp > 2 ** ES:
exp = exp - 2**ES
seed + 1
print("sum output:")
print("\tseed: {}".format(seed))
print("\texponent: {}".format(exp))
print("\tfraction: {}".format(frac))
return create_vector(seed, exp, frac)
def multiply_numbers(seed1, exp1, frac1, seed2, exp2, frac2):
seed = seed1 + seed2
exp = exp1 + exp2
frac = frac1 * frac2
if (frac >= 2):
exp = exp +1
frac = frac / 2
if (exp > 2 ** ES):
seed = seed + 1
exp = exp - 2 ** ES
print("product output:")
print("\tseed: {}".format(seed))
print("\texponent: {}".format(exp))
print("\tfraction: {}".format(frac))
return create_vector(seed, exp, frac)
seed1 = 5
exp1 = 3
frac1 = 2 + 1/2 + 1/8 + 1/16
posit1 = create_vector(seed1, exp1, frac1)
seed2 = 5
exp2 = 2
frac2 = 1 + 1/4 + 1/8 + 1/32
posit2 = create_vector(seed2, exp2, frac2)
print("Posit1: %s" % posit1)
print("Posit2: %s" % posit2)
product_posit = add_numbers(seed1, exp1, frac1, seed2, exp2, frac2)
print("\tposit: {}".format(product_posit))