-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlibbaltfloat.py
More file actions
executable file
·188 lines (166 loc) · 3.99 KB
/
libbaltfloat.py
File metadata and controls
executable file
·188 lines (166 loc) · 3.99 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
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
import math
#import libbaltcalc
#please note: This Library is NOT YET WORKING CORRECTLY.
#prototype floating point library.
#this took a bit of work.
#not the simplest thing to do.
#definately more complex than integers :p
#and will someone please fix the mess of a decimal to balanced ternary
#float conversion function. >.<
def numflip(numtoflip):
return(numtoflip[::-1])
#returns first part of a balanced ternary float.
def uprad(NUMTOPROC):
radcnt=1
for radpic in (NUMTOPROC.split('.')):
if radcnt==(1):
return(radpic)
radcnt += 1
return
def downrad(NUMTOPROC):
radcnt=1
for radpic in (NUMTOPROC.split('.')):
if radcnt==(2):
return(radpic)
radcnt += 1
return
def radmerge(uprd, downrd):
return(uprd + "." + downrd)
#this complicated function converts Balanced Ternary
#Floats to Decimal floats. :p
#if the input number is not a float, it acts like the libbaltcalc equiv.
def BTTODEC(NUMTOCONV1):
FLIPPEDSTR2=""
FLIPPEDSTR1=(numflip(uprad(NUMTOCONV1)))
FLIPPEDSTR2=(downrad(NUMTOCONV1))
if FLIPPEDSTR2 is None:
NULLBAR2=1
else:
NULLBAR2=0
#for f in FLIPPEDSTR2:
# print f
EXTRAP1=0
SUMDEC1=0
EXTRAP2=-1
for btnumlst1 in FLIPPEDSTR1:
EXTPOLL1 = (3**EXTRAP1)
if btnumlst1==("+"):
SUMDEC1 += EXTPOLL1
if btnumlst1==("-"):
SUMDEC1 -= EXTPOLL1
EXTRAP1 += 1
#print SUMDEC1
if NULLBAR2==0:
for btnumlst2 in FLIPPEDSTR2:
#print btnumlst2
EXTPOLL2 = (3**EXTRAP2)
#print "foo"
#print EXTPOLL2
#EXTPOLL2=(float("0." + str(EXTPOLL2)))
if btnumlst2==("+"):
SUMDEC1 += EXTPOLL2
if btnumlst2==("-"):
SUMDEC1 -= EXTPOLL2
#print ("foo>" + str(SUMDEC1))
EXTRAP2 -= 1
#print SUMDEC1
SUMDECMAIN=(SUMDEC1)
return (SUMDECMAIN)
#this complicated function converts Decimal
#Floats to Balanced Ternary floats. :p
#if teh input number is not a float, it acts like the libbaltcalc equiv.
#BROKEN!!!!! (SEE BELOW)
def DECTOBT(NUMTOCONVB):
digbat=""
digbat2=""
NUMTOCONV1=int(uprad(str(NUMTOCONVB)))
PRETEST1=(downrad(str(NUMTOCONVB)))
if PRETEST1 is None:
NULLBAR=1
#print "WHO"
else:
NULLBAR=0
NUMTOCONV2=int(downrad(str(NUMTOCONVB)))
#print NUMTOCONV2
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
if digbat=="":
digbat="0"
#this section is Broken!!!!!!!!!!
#pls fix this. 0.+-- is not even close to 0.5 XD
if NULLBAR==0:
#while NUMTOCONV2 != 1:
#if NUMTOCONV2 % 3 == 0:
##note_digit(0)
#digbat2=("0" + digbat2)
#elif NUMTOCONV2 % 3 == 1:
##note_digit(1)
#digbat2=("+" + digbat2)
#elif NUMTOCONV2 % 3 == 2:
##note_digit(-1)
#digbat2=("-" + digbat2)
#NUMTOCONV2 = (NUMTOCONV2 + 1) // 3
CRAD=-1
CRADP=1
digbat2=""
for FNUM in str(NUMTOCONV2):
BUFFD=(10**CRAD)
BUFFC=(int(FNUM))
while BUFFC > CRADP:
BUFFC=(BUFFC*BUFFD)
print BUFFC
print "fo"
CRAD -=1
CRADP +=1
print digbat2
digbat3=(digbat + "." + digbat2)
else:
digbat3=digbat
#print NUMTOCONV1
#zero exception
if (str(digbat3)==""):
digbat="0"
return(digbat3)
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)
#this should be a tad more acurate than its libbaltcalc counterpart.
def btdev(numA, numB):
numAcon=BTTODEC(numA)
numBcon=BTTODEC(numB)
decRes=(float(numAcon) / float(numBcon))
print(decRes)
btRes=(DECTOBT(decRes))
return(btRes)
print(btdev("+", "+-"))
#print(BTTODEC(DECTOBT(55.3)))
print(DECTOBT(0.5))
#BTTODEC("+-0+.-")
print(BTTODEC("0.+-"))
#print(DECTOBT("1"))
#print uprad("+-0+.-0")
#print downrad("+-0+.-0")