forked from ThomasTheSpaceFox/libbaltcalc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlibbal9.py
executable file
·71 lines (62 loc) · 1.31 KB
/
libbal9.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
#!/usr/bin/env python
def inttob9(intval):
remainder = int(intval)
output=""
while remainder!=0:
#print(remainder)
chunk, remainder = b9chop(remainder, 1)
#print(str(remainder) + " " + str(chunk))
output=ditrit_b9_dict[chunk] + output
if output=="":
return "0"
return output
def b9toint(base9_string):
total=0
for i in base9_string:
ditrit=b9_ditrit_dict[i]
total=b9merge(total, ditrit, 1)
return total
b9_ditrit_dict={
"4": +4,
"3": +3,
"2": +2,
"1": +1,
"0": 0,
"Z": -1,
"Y": -2,
"X": -3,
"W": -4}
ditrit_b9_dict={}
for f in b9_ditrit_dict:
ditrit_b9_dict[b9_ditrit_dict[f]]=f
def b9chop(decimal_int, split_point):
split_point=split_point*2
tritcount=0
retlist=[]
issplit=False
for f in [0, 0]:
#print("--")
while decimal_int != 0:
#print(decimal_int)
#
#print(f)
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 b9merge(decimal_int_upper, decimal_int_lower, length_of_lower):
length_of_lower=length_of_lower*2
return ((decimal_int_upper * (3**length_of_lower)) + decimal_int_lower)