-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmods.py
More file actions
148 lines (123 loc) · 3.73 KB
/
mods.py
File metadata and controls
148 lines (123 loc) · 3.73 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#Author FuzzLightyer aka Fuzzifier aka Medusa
#File to store some of the functions defined elsewhere
def getPhi(): #input(None), calculates phi. Returns phi
n = 20
print(n)
phi = 1.00
current = 1.00
last = 1.00
tmp = 1.00
for i in range(n):
print("current:",current,"; last:",last,";")
tmp = current
current = current + last
last = tmp
phi = (phi+(current/last))/2
return phi
def revArray(x): #input(list), makes new list with elements in reverse order, returns reversed array
#set reverse arrays
revx = list()
bc = len(x)-1
for rx in range(len(x)):
revx.append(x[bc])
bc = bc - 1
return revx
def numToBinary(x):
tmpx = x
counter = 0
binNum = []
while 2**counter <= x: #If less than 8 for example, must only need 3 digits
counter += 1
for i in range(counter):
if tmpx - 2**(counter-i-1) >= 0: #if can subtract by next highest power of 2, do it and append 1 to binNum.
tmpx -= 2**(counter-i-1) #Set tmpx as remainder of the subtraction
binNum.append(int(1)) #mark in binNum that this operation was done successfully
else:
binNum.append(int(0))
return binNum
def binaryToInt(_binNum):
tmpsum = 0
for i in range(len(_binNum)):
if int(_binNum[len(_binNum)-i-1]) == 1:
tmpsum += 2**i
return tmpsum
def binaryAdd(x, y): #takes lists ([0,0,1,0],[1,0,1])
print("Adding Binary Numbers: ", x , " (", binaryToInt(x), ") and ", y, " (", binaryToInt(y), ")")
rx = revArray(x)
ry = revArray(y)
if len(x) != len(ry):
if min(len(x),len(y)) == len(x):
s = rx
else:
s = ry
while len(rx) != len(ry):
s.append(0)
carry = 0
sum = []
for i in range(len(rx)):
if rx[i] and ry[i] and carry:
sum.append(1)
carry =1
elif (rx[i] and ry[i] and not carry) or (rx[i] and carry and not ry[i]) or (ry[i] and carry and not rx[i]):
sum.append(0)
carry = 1
elif (rx[i] and not carry and not ry[i]) or (carry and not rx[i] and not ry[i]) or (ry[i] and not carry and not rx[i]):
sum.append(1)
carry = 0
else: #all 0s
sum.append(0)
carry=0
if carry:
sum.append(1)
return(revArray(sum))
l = []
o = []
l.append(int(1))
l.append(int(1))
l.append(int(1))
o.append(int(1))
o.append(int(1))
o.append(int(1))
o.append(int(1))
print("Proceeding with binary number addition of X and Y to get sum Z")
print("X, as seen directly below in binary and numerical form")
print(l)
print(binaryToInt(l))
print("+")
print("Y, as seen directly below in binary and numerical form")
print(o)
print(binaryToInt(o))
h = binaryAdd(l,o)
print(h)
print("=")
print("Z, as seen directly below in binary and numerical form")
print(binaryToInt(h))
def binMult(x, y):
rsum = 0
print(binaryToInt(y))
for i in range(binaryToInt(y)):
tmp = binaryAdd(numToBinary(rsum), x)
rsum = binaryToInt(tmp)
return tmp #return rsum to return int
def mult(x, y):
#Proceeding with multiplication.
#Converted x and y to binary forms.
#Transitioning to binary multiplication operation.
binProduct = binMult(numToBinary(x), numToBinary(y))
return(binaryToInt(binProduct))
def add(x, y):
#Proceeding with addition.
#Converted x and y to binary forms.
#Transitioning to binary addition operation.
binSum = binaryAdd(numToBinary(x), numToBinary(y))
return(binaryToInt(binSum))
# x = []
# y = []
# x.append(int(1))
# x.append(int(1)) #x=3
# y.append(int(1)) #y = 7
# y.append(int(1))
# y.append(int(1))
# print(binMult(x,y))
print(mult(5, 7))
print(add(3,10))