forked from ThomasTheSpaceFox/libbaltcalc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlibbaltcalc.py
executable file
·412 lines (386 loc) · 10.1 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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#!/usr/bin/env python
#v3.2.0
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 tritchop(decimal_int, split_point):
tritcount=0
retlist=[]
issplit=False
for f in [0, 0]:
while decimal_int != 0:
if decimal_int % 3 == 0:
#0
pass
elif decimal_int % 3 == 1:
#+
f+=(3**tritcount)
elif decimal_int % 3 == 2:
#-
f-=(3**tritcount)
tritcount+=1
decimal_int = (decimal_int + 1) // 3
if tritcount==split_point and not issplit:
tritcount=0
issplit=True
break
retlist.append(f)
return retlist
def tritmerge(decimal_int_upper, decimal_int_lower, length_of_lower):
return ((decimal_int_upper * (3**length_of_lower)) + decimal_int_lower)
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 btdivcpu(numA, numB):
numAcon=BTTODEC(numA)
numBcon=BTTODEC(numB)
try:
decRes=(numAcon // numBcon)
except ZeroDivisionError:
#Special zero divisoon return for SBTCVM to detect. "ZDIV"
return "ZDIV"
btRes=(DECTOBT(decRes))
return(btRes)
def btdiv(numA, numB):
numAcon=BTTODEC(numA)
numBcon=BTTODEC(numB)
decRes=(numAcon // numBcon)
btRes=(DECTOBT(decRes))
return(btRes)
btdev=btdiv
def mpi(tritlen):
return (((3**(tritlen))-1)//2)
def mni(tritlen):
return ( - ((3**(tritlen))-1)//2)
def mcv(tritlen):
return (3**(tritlen))
#inverts the positive and negative numerals in a balanced ternary integer,
#(ie +-0- would become -+0+ 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", "+"))
# 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")
#trit truncation helper for btint.bttrunk method.
def trunkhelper(tritlen, decint):
#(('0'*tritlen) + DECTOBT(self.intval))[:tritlen]
btstr=DECTOBT(decint)
if len(btstr)<tritlen:
return (('0' * (tritlen - len(btstr))) + btstr)
else:
return btstr[-tritlen:]
def dectrunkhelper(tritlen, decint):
mpival=mpi(tritlen)
if decint<-mpival:
return -mpival
elif decint>mpival:
return mpival
else:
return decint
class btint(object):
__slots__ = ('intval')
def __init__(self, stringint):
#store integer in signed decimal integer.
if type(stringint) is int:
self.intval=stringint
else:
try:
self.intval=stringint.intval
except AttributeError:
self.intval=BTTODEC(str(stringint).replace("p", "+").replace("n", "-"))
def __str__(self):
return DECTOBT(self.intval)
def __int__(self):
return self.intval
def dec(self):
return self.intval
def bt(self):
return DECTOBT(self.intval)
def p0n(self):
return (DECTOBT(self.intval).replace("+", "p").replace("-", "n"))
def copy(self):
return btint(self.intval)
def changeval(self, newval):
if type(newval) is int:
self.intval=newval
else:
try:
self.intval=newval.intval
except AttributeError:
self.intval=BTTODEC(str(newval).replace("p", "+").replace("n", "-"))
#addition
def __add__(self, other):
if isinstance(other, btint):
return btint((self.intval + other.intval))
elif isinstance(other, int):
return btint((self.intval + other))
else:
return NotImplemented
def __radd__(self, other):
if isinstance(other, int):
return btint((other + self.intval))
else:
return NotImplemented
def __iadd__(self, other):
if isinstance(other, btint):
self.intval += other.intval
return self
elif isinstance(other, int):
self.intval += other
return self
else:
return NotImplemented
#subtraction
def __sub__(self, other):
if isinstance(other, btint):
return btint((self.intval - other.intval))
elif isinstance(other, int):
return btint((self.intval - other))
else:
return NotImplemented
def __rsub__(self, other):
if isinstance(other, int):
return btint((other - self.intval))
else:
return NotImplemented
def __isub__(self, other):
if isinstance(other, btint):
self.intval -= other.intval
return self
elif isinstance(other, int):
self.intval -= other
return self
else:
return NotImplemented
#division
def __floordiv__(self, other):
if isinstance(other, btint):
return btint((self.intval // other.intval))
elif isinstance(other, int):
return btint((self.intval // other))
else:
return NotImplemented
def __rfloordiv__(self, other):
if isinstance(other, int):
return btint((other // self.intval))
else:
return NotImplemented
def __ifloordiv__(self, other):
if isinstance(other, btint):
self.intval //= other.intval
return self
elif isinstance(other, int):
self.intval //= other
return self
else:
return NotImplemented
#multiplication
def __mul__(self, other):
if isinstance(other, btint):
return btint((self.intval * other.intval))
elif isinstance(other, int):
return btint((self.intval * other))
else:
return NotImplemented
def __rmul__(self, other):
if isinstance(other, int):
return btint((other * self.intval))
else:
return NotImplemented
def __imul__(self, other):
if isinstance(other, btint):
self.intval *= other.intval
return self
elif isinstance(other, int):
self.intval *= other
return self
else:
return NotImplemented
#compare
def __cmp__(self, other):
if isinstance(other, btint):
if self.intval<other.intval:
return -1
elif self.intval>other.intval:
return 1
else:
return 0
elif isinstance(other, int):
if self.intval<other:
return -1
elif self.intval>other:
return 1
else:
return 0
else:
return NotImplemented
def __lt__(self, other):
if isinstance(other, btint):
return self.intval<other.intval
elif isinstance(other, int):
return self.intval<other
else:
return NotImplemented
def __le__(self, other):
if isinstance(other, btint):
return self.intval<=other.intval
elif isinstance(other, int):
return self.intval<=other
else:
return NotImplemented
def __gt__(self, other):
if isinstance(other, btint):
return self.intval>other.intval
elif isinstance(other, int):
return self.intval>other
else:
return NotImplemented
def __ge__(self, other):
if isinstance(other, btint):
return self.intval>=other.intval
elif isinstance(other, int):
return self.intval>=other
else:
return NotImplemented
def __ne__(self, other):
if isinstance(other, btint):
return self.intval!=other.intval
elif isinstance(other, int):
return self.intval!=other
else:
return NotImplemented
def __eq__(self, other):
if isinstance(other, btint):
return self.intval==other.intval
elif isinstance(other, int):
return self.intval==other
else:
return NotImplemented
#length (measured in trits)
def __len__(self):
return len(DECTOBT(self.intval))
#others
def __abs__(self):
return btint(abs(self.intval))
def __neg__(self):
return btint( - self.intval)
def __pos__(self):
return btint( + self.intval)
def __invert__(self):
return btint( - self.intval)
def invert(self):
return btint( - self.intval)
#truncation
def bttrunk(self, tritlen):
return trunkhelper(tritlen, self.intval)
def dectrunk(self, tritlen):
return btint(dectrunkhelper(tritlen, self.intval))