Repository navigation
Expand file tree
/
Copy pathprecise.py
More file actions
297 lines (246 loc) · 8.68 KB
/
Copy pathprecise.py
File metadata and controls
297 lines (246 loc) · 8.68 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
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
# Author: carlo.revelli@berkeley.edu
#
# Precise
# Representation
# Expanding
# CCXT
# Internal
# Scientific
# Exponents
#
# (╯°□°)╯︵ ┻━┻
class Precise:
def __init__(self, number, decimals=None):
if decimals is None:
modifier = 0
number = number.lower()
if 'e' in number:
number, modifier = number.split('e')
modifier = int(modifier)
decimal_index = number.find('.')
if decimal_index > -1:
self.decimals = len(number) - decimal_index - 1
self.integer = int(number.replace('.', ''))
else:
self.decimals = 0
self.integer = int(number)
self.decimals = self.decimals - modifier
else:
self.integer = number
self.decimals = decimals
self.base = 10
def __add__(self, other):
return self.add(other)
def __sub__(self, other):
return self.sub(other)
def __mul__(self, other):
return self.mul(other)
def __truediv__(self, other):
return self.div(other)
def __mod__(self, other):
return self.mod(other)
def __neg__(self):
return self.neg()
def __abs__(self):
return self.abs()
def __min__(self, other):
return self.min(other)
def __max__(self, other):
return self.max(other)
def __lt__(self, other):
return self.lt(other)
def __le__(self, other):
return self.le(other)
def __gt__(self, other):
return self.gt(other)
def __ge__(self, other):
return self.ge(other)
def __eq__(self, other):
if isinstance(other, str):
# Allow comparisons with Precise("5") == "5"
return str(self) == other
return self.equals(other)
def mul(self, other):
integer_result = self.integer * other.integer
return Precise(integer_result, self.decimals + other.decimals)
def div(self, other, precision=18):
distance = precision - self.decimals + other.decimals
if distance == 0:
numerator = self.integer
elif distance < 0:
exponent = self.base ** -distance
numerator = self.integer // exponent
else:
exponent = self.base ** distance
numerator = self.integer * exponent
result, mod = divmod(numerator, other.integer)
# python floors negative numbers down instead of truncating
# if mod is zero it will be floored to itself so we do not add one
result = result + 1 if result < 0 and mod else result
return Precise(result, precision)
def add(self, other):
if self.decimals == other.decimals:
integer_result = self.integer + other.integer
return Precise(integer_result, self.decimals)
else:
smaller, bigger = [other, self] if self.decimals > other.decimals else [self, other]
exponent = bigger.decimals - smaller.decimals
normalised = smaller.integer * (self.base ** exponent)
result = normalised + bigger.integer
return Precise(result, bigger.decimals)
def sub(self, other):
negative = Precise(-other.integer, other.decimals)
return self.add(negative)
def abs(self):
return Precise(abs(self.integer), self.decimals)
def neg(self):
return Precise(-self.integer, self.decimals)
def mod(self, other):
rationizerNumberator = max(-self.decimals + other.decimals, 0)
numerator = self.integer * (self.base ** rationizerNumberator)
rationizerDenominator = max(-other.decimals + self.decimals, 0)
denominator = other.integer * (self.base ** rationizerDenominator)
result = numerator % denominator
return Precise(result, rationizerDenominator + other.decimals)
def orn(self, other):
integer_result = self.integer | other.integer
return Precise(integer_result, self.decimals)
def min(self, other):
return self if self.lt(other) else other
def max(self, other):
return self if self.gt(other) else other
def gt(self, other):
add = self.sub(other)
return add.integer > 0
def ge(self, other):
add = self.sub(other)
return add.integer >= 0
def lt(self, other):
return other.gt(self)
def le(self, other):
return other.ge(self)
def reduce(self):
string = str(self.integer)
start = len(string) - 1
if start == 0:
if string == "0":
self.decimals = 0
return self
for i in range(start, -1, -1):
if string[i] != '0':
break
difference = start - i
if difference == 0:
return self
self.decimals -= difference
self.integer = int(string[:i + 1])
def equals(self, other):
self.reduce()
other.reduce()
return self.decimals == other.decimals and self.integer == other.integer
def __str__(self):
self.reduce()
sign = '-' if self.integer < 0 else ''
integer_array = list(str(abs(self.integer)).rjust(self.decimals, '0'))
index = len(integer_array) - self.decimals
if index == 0:
item = '0.'
elif self.decimals < 0:
item = '0' * (-self.decimals)
elif self.decimals == 0:
item = ''
else:
item = '.'
integer_array.insert(index, item)
return sign + ''.join(integer_array)
def __repr__(self):
return "Precise(" + str(self) + ")"
def __float__(self):
return float(str(self))
@staticmethod
def string_mul(string1, string2):
if string1 is None or string2 is None:
return None
return str(Precise(string1).mul(Precise(string2)))
@staticmethod
def string_div(string1, string2, precision=18):
if string1 is None or string2 is None:
return None
string2_precise = Precise(string2)
if string2_precise.integer == 0:
return None
return str(Precise(string1).div(string2_precise, precision))
@staticmethod
def string_add(string1, string2):
if string1 is None and string2 is None:
return None
if string1 is None:
return string2
elif string2 is None:
return string1
return str(Precise(string1).add(Precise(string2)))
@staticmethod
def string_sub(string1, string2):
if string1 is None or string2 is None:
return None
return str(Precise(string1).sub(Precise(string2)))
@staticmethod
def string_abs(string):
if string is None:
return None
return str(Precise(string).abs())
@staticmethod
def string_neg(string):
if string is None:
return None
return str(Precise(string).neg())
@staticmethod
def string_mod(string1, string2):
if string1 is None or string2 is None:
return None
return str(Precise(string1).mod(Precise(string2)))
@staticmethod
def string_or(string1, string2):
if string1 is None or string2 is None:
return None
return str(Precise(string1).orn(Precise(string2)))
@staticmethod
def string_equals(string1, string2):
if string1 is None or string2 is None:
return None
return Precise(string1).equals(Precise(string2))
@staticmethod
def string_eq(string1, string2):
if string1 is None or string2 is None:
return None
return Precise(string1).equals(Precise(string2))
@staticmethod
def string_min(string1, string2):
if string1 is None or string2 is None:
return None
return str(Precise(string1).min(Precise(string2)))
@staticmethod
def string_max(string1, string2):
if string1 is None or string2 is None:
return None
return str(Precise(string1).max(Precise(string2)))
@staticmethod
def string_gt(string1, string2):
if string1 is None or string2 is None:
return None
return Precise(string1).gt(Precise(string2))
@staticmethod
def string_ge(string1, string2):
if string1 is None or string2 is None:
return None
return Precise(string1).ge(Precise(string2))
@staticmethod
def string_lt(string1, string2):
if string1 is None or string2 is None:
return None
return Precise(string1).lt(Precise(string2))
@staticmethod
def string_le(string1, string2):
if string1 is None or string2 is None:
return None
return Precise(string1).le(Precise(string2))