-
Notifications
You must be signed in to change notification settings - Fork 1
/
libbaltcalc.py
executable file
·358 lines (331 loc) · 8.19 KB
/
libbaltcalc.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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#!/usr/bin/env python
import math
#HELPTEXT = ('''commands:
#BTTODEC: convert a number into balancet
#''')
# syntax info:
#trit order is least signifigant digit on right
#1=+
#0=0
#T=-
def numflip(numtoflip):
return(numtoflip[::-1])
#converts balanced ternary integers to decimal.
#this is a core function to the library.
def BTTODEC(NUMTOCONV1):
FLIPPEDSTR1=(numflip(NUMTOCONV1))
EXTRAP1=0
SUMDEC1=0
for btnumlst1 in FLIPPEDSTR1:
EXTPOLL1 = (3**EXTRAP1)
if btnumlst1==("+"):
SUMDEC1 += EXTPOLL1
if btnumlst1==("-"):
SUMDEC1 -= EXTPOLL1
EXTRAP1 += 1
return (SUMDEC1)
#converts decimal integers to balanced ternary.
#this is a core function to the library.
def DECTOBT(NUMTOCONV1):
digbat=""
while NUMTOCONV1 != 0:
if NUMTOCONV1 % 3 == 0:
#note_digit(0)
digbat=("0" + digbat)
elif NUMTOCONV1 % 3 == 1:
#note_digit(1)
digbat=("+" + digbat)
elif NUMTOCONV1 % 3 == 2:
#note_digit(-1)
digbat=("-" + digbat)
NUMTOCONV1 = (NUMTOCONV1 + 1) // 3
#print NUMTOCONV1
#zero exception
if (str(digbat)==""):
digbat="0"
return(digbat)
def btmul(numA, numB):
numAcon=BTTODEC(numA)
numBcon=BTTODEC(numB)
decRes=(numAcon * numBcon)
btRes=(DECTOBT(decRes))
return(btRes)
def btadd(numA, numB):
numAcon=BTTODEC(numA)
numBcon=BTTODEC(numB)
decRes=(numAcon + numBcon)
btRes=(DECTOBT(decRes))
return(btRes)
def btsub(numA, numB):
numAcon=BTTODEC(numA)
numBcon=BTTODEC(numB)
decRes=(numAcon - numBcon)
btRes=(DECTOBT(decRes))
return(btRes)
#note that values may not be exact. this is due to that the libbaltcalc currently handles integers only.
def btdev(numA, numB):
numAcon=BTTODEC(numA)
numBcon=BTTODEC(numB)
decRes=int(numAcon / numBcon)
btRes=(DECTOBT(decRes))
return(btRes)
#inverts the positive and negative numerals in a balanced ternary integer,
#(ie 1T0T would become T101 and vice versa)
def BTINVERT(numtoinvert):
BTINV1 = numtoinvert.replace("-", "P").replace("+", "-").replace("P", "+")
#print BTINV2
return (BTINV1)
def trailzerostrip(numtostri):
pritokfg=0
#print ("argh -.-" + numtostri)
numtostri = numtostri.replace("-", "T").replace("+", "1")
#numtostri = (numflip(numtostri))
numretbankd=""
#print (numtostri)
allzero=1
for fnumt in numtostri:
if (fnumt=="T" or fnumt=="1"):
pritokfg=1
allzero=0
if pritokfg==1:
numretbankd = (numretbankd + fnumt)
if pritokfg==0:
nullbox=fnumt
#print (fnumt)
if allzero==1:
numretbankd="0"
numretbankd = numretbankd
#print (numretbankd.replace("T", "-").replace("1", "+"))
return (numretbankd.replace("T", "-").replace("1", "+"))
#prodotype addition function.
#preserved for its interesting logic
#see how this manages to emulate the logical function of a balanced
#ternary ripple carry adder of just large enough size to complete the addition
def btaddreal(numA, numB):
#check to ensure any final carries are preformed.
numA=("E" + numA)
numB=("E" + numB)
numA=(numflip(numA))
numB=(numflip(numB))
numAcnt=0
numBcnt=0
curregA=1
curregB=1
carry="0"
eotA=0
eotB=0
resbt=""
for anA in numA:
numAcnt += 1
for anB in numB:
numBcnt += 1
if (numAcnt > numBcnt):
forlist = numA
overload=numAcnt
if (numAcnt < numBcnt):
forlist = numB
overload=numBcnt
if (numAcnt==numBcnt):
forlist = numA
overload=numAcnt
overcnt=1
for dxpink in forlist:
loopregA=1
loopregB=1
for Areg in numA:
if curregA==loopregA:
returnedA=1
Aval = Areg
break
loopregA += 1
for Breg in numB:
if curregB==loopregB:
returnedB=1
Bval = Breg
break
loopregB += 1
#print ("A:" + Aval + str(returnedA))
#print ("B:" + Bval + str(returnedB))
#Aval=+ rules:
if Aval=="E":
eotA=1
Aval="0"
if Bval=="E":
eotB=1
Bval="0"
if (Aval=="+" and Bval=="+"):
if carry=="0":
resbt = ("-" + resbt)
carry="+"
elif carry=="+":
resbt = ("0" + resbt)
carry="+"
elif carry=="-":
resbt = ("+" + resbt)
carry="0"
elif (Aval=="+" and Bval=="0"):
if carry=="0":
resbt = ("+" + resbt)
carry="0"
elif carry=="+":
resbt = ("-" + resbt)
carry="+"
elif carry=="-":
resbt = ("0" + resbt)
carry="0"
elif (Aval=="+" and Bval=="-"):
if carry=="0":
resbt = ("0" + resbt)
carry="0"
elif carry=="+":
resbt = ("+" + resbt)
carry="0"
elif carry=="-":
resbt = ("-" + resbt)
carry="0"
#Aval=- rules
elif (Aval=="-" and Bval=="-"):
if carry=="0":
resbt = ("+" + resbt)
carry="-"
elif carry=="-":
resbt = ("0" + resbt)
carry="-"
elif carry=="+":
resbt = ("-" + resbt)
carry="0"
elif (Aval=="-" and Bval=="0"):
if carry=="0":
resbt = ("-" + resbt)
carry="0"
elif carry=="+":
resbt = ("0" + resbt)
carry="0"
elif carry=="-":
resbt = ("+" + resbt)
carry="-"
elif (Aval=="-" and Bval=="+"):
if carry=="0":
resbt = ("0" + resbt)
carry="0"
elif carry=="+":
resbt = ("+" + resbt)
carry="0"
elif carry=="-":
resbt = ("-" + resbt)
carry="0"
#Aval=0 rules
elif (Aval=="0" and Bval=="0"):
if carry=="0":
resbt = ("0" + resbt)
carry="0"
elif carry=="+":
resbt = ("+" + resbt)
carry="0"
elif carry=="-":
resbt = ("-" + resbt)
carry="0"
elif (Aval=="0" and Bval=="-"):
if carry=="0":
resbt = ("-" + resbt)
carry="0"
elif carry=="+":
resbt = ("0" + resbt)
carry="0"
elif carry=="-":
resbt = ("+" + resbt)
carry="-"
elif (Aval=="0" and Bval=="+"):
if carry=="0":
resbt = ("+" + resbt)
carry="0"
elif carry=="+":
resbt = ("-" + resbt)
carry="+"
elif carry=="-":
resbt = ("0" + resbt)
carry="0"
curregA += 1
curregB += 1
Aval="0"
Bval="0"
returnedA=0
returnedB=0
#print ()
buzzt=trailzerostrip(str(resbt))
vexping=str(buzzt)
return (buzzt)
#count up based Decimal to balanced ternary converter.
def DECTOBTold(NUMTOCONV1):
decicnt=0
prevbtnum="0"
charlst1=str(NUMTOCONV1)
for fstdig in charlst1:
firstsym=fstdig
break
if firstsym=="-":
btcntdig="-"
elif firstsym!="-":
btcntdig="+"
#print("actual decimal count| BT count in decimal | BT count")
while decicnt!=NUMTOCONV1:
#print (str(decicnt) + "|" + str(libbaltcalc.BTTODEC(prevbtnum)) + "|" + prevbtnum)
prevbtnum=(btadd(prevbtnum, btcntdig))
vixiestr=prevbtnum
if firstsym=="-":
decicnt -= 1
elif firstsym!="-":
decicnt += 1
return (vixiestr)
#print(DECTOBT(19))
#gets a balanced ternary number from the user an parses it based on various
#balanced ternary notation conventions. currently only the 1,0,T and +,0,- conventions.
def BTstrgetsort():
NUMPARS = raw_input('>:')
NUMPARS = NUMPARS.replace("1", "+").replace("T", "-")
return (NUMPARS)
#gets a sigle-trit balanced ternary number from the user an parses it based on various
#balanced ternary notation conventions. currently only the 1,0,T and +,0,- conventions.
def BTstrgetsingle():
NUMPX = BTstrgetsort()
for fstdig in NUMPX:
return (fstdig)
# a "programmable" biased and gate. returns a positive if:
#input a (inpA) = input b (inpB) = polarity line (polarset)
#else it returns zero
def progbiasand(polarset, inpA, inpB):
if (inpA==polarset and inpB==polarset):
return("+")
elif (inpA!=polarset or inpB!=polarset):
return("0")
#a polarized and gate
#returns + if both input A (inpA) and input B (inpB) = +
#returns - if both input A (inpA) and input B (inpB) = -
#otherwise it returns zero
def polarityand(inpA, inpB):
if (inpA=="+" and inpB=="+"):
return("+")
elif (inpA=="-" and inpB=="-"):
return("-")
elif (inpA!="+" or inpB!="+"):
return("0")
elif (inpA!="-" or inpB!="-"):
return("0")
# a programmable biased or gate returns "+" if either or both inputs equal the pollarity line (polarset)
#else it returns "0"
def progbiasor(polarset, inpA, inpB):
if (inpA==polarset or inpB==polarset):
return("+")
elif (inpA!=polarset or inpB!=polarset):
return("0")
# a programmable biased orn gate returns "+" if either equal the pollarity line (polarset)
#returns "0" either if neither or both inputs equal the pollarity line (polarset)
def progbiasnor(polarset, inpA, inpB):
if (inpA==polarset and inpB==polarset):
return("0")
elif (inpA!=polarset and inpB==polarset):
return("+")
elif (inpA==polarset and inpB!=polarset):
return("+")
elif (inpA!=polarset and inpB!=polarset):
return("0")