-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathExponential.py
More file actions
283 lines (228 loc) · 6.52 KB
/
Copy pathExponential.py
File metadata and controls
283 lines (228 loc) · 6.52 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
# Exponential math library
# Performs high precision math operation
import smartpy as sp
MathErrors = sp.io.import_script_from_url(
"file:contracts/errors/MathErrors.py")
EC = MathErrors.ErrorCodes
"""
Stores decimals with a fixed precision of 18 decimal places
Thus, if we wanted to store the 5.1, mantissa would store 5.1e18. That is:
`TExp({mantissa: 5100000000000000000})`.
"""
TExp = sp.TRecord(mantissa=sp.TNat)
class Exponential(sp.Contract):
def __init__(self, **extra_storage):
scale = int(1e18)
self.init(expScale=sp.nat(scale), halfExpScale=sp.nat(
scale // 2), **extra_storage)
"""
Constructs TExp from the given number value, without applying expScale
For example, makeExp(15) = Exp{mantissa: 15}
params:
value: TNat
return: TExp
"""
def makeExp(self, value):
sp.set_type(value, sp.TNat)
return sp.record(mantissa=value)
"""
Constructs TExp from the given number value, applying expScale
For example, makeExp(15) = Exp{mantissa: 15 * expScale}
params:
value: TNat
return: TExp
"""
def toExp(self, value):
sp.set_type(value, sp.TNat)
return self.makeExp(value * self.data.expScale)
"""
Truncates the given exp to a whole number value.
For example, truncate(Exp{mantissa: 15 * expScale}) = 15
params:
exp: TExp
return: TNat
"""
def truncate(self, exp):
sp.set_type(exp, TExp)
return exp.mantissa // self.data.expScale
"""
Multiply an Exp by a scalar, then truncate to return an unsigned integer.
params:
a: TExp
scalar: TNat
return: TNat
"""
def mulScalarTruncate(self, a, scalar):
sp.set_type(a, TExp)
return self.truncate(self.mul_exp_nat(a, scalar))
"""
Multiply an Exp by a scalar, truncate, then add an to an unsigned integer, returning an unsigned integer.
params:
a: TExp
scalar: TNat
addend: TNat
return: TNat
"""
def mulScalarTruncateAdd(self, a, scalar, addend):
sp.set_type(a, TExp)
sp.set_type(scalar, sp.TNat)
sp.set_type(addend, sp.TNat)
return self.mulScalarTruncate(a, scalar) + addend
"""
Multiply two Exp, round up to 1e-18, returning a new exponential.
params:
a: TExp
b: TExp
return: TExp
"""
def mulExpRounded(self, a, b):
sp.set_type(a, TExp)
sp.set_type(b, TExp)
mul_rounded = a.mantissa * b.mantissa + self.data.halfExpScale
return self.makeExp(mul_rounded // self.data.expScale)
"""
Checks if first Exp is less than second Exp.
params:
a: TExp
b: TExp
return: bool
"""
def lessThanExp(self, a, b):
sp.set_type(a, TExp)
sp.set_type(b, TExp)
return a.mantissa < b.mantissa
"""
Checks if left Exp <= right Exp.
params:
a: TExp
b: TExp
return: bool
"""
def lessThanOrEqualExp(self, a, b):
sp.set_type(a, TExp)
sp.set_type(b, TExp)
return a.mantissa <= b.mantissa
# Raw operations
"""
params:
a: TExp
b: TExp
return: TExp
"""
def add_exp_exp(self, a, b):
sp.set_type(a, TExp)
sp.set_type(b, TExp)
return self.makeExp(a.mantissa + b.mantissa)
"""
params:
a: TExp
b: TNat
return: TExp
"""
def add_exp_nat(self, a, b):
sp.set_type(a, TExp)
sp.set_type(b, sp.TNat)
return self.makeExp(a.mantissa + b * self.data.expScale)
"""
params:
a: TNat
b: TNat
return: TNat
"""
def add_nat_nat(self, a, b):
sp.set_type(a, sp.TNat)
sp.set_type(b, sp.TNat)
return a + b
"""
params:
a: TExp
b: TExp
return: TExp
"""
def sub_exp_exp(self, a, b):
sp.set_type(a, TExp)
sp.set_type(b, TExp)
return self.makeExp(self.sub_nat_nat(a.mantissa, b.mantissa))
"""
params:
a: TNat
b: TNat
return: TNat
"""
def sub_nat_nat(self, a, b):
sp.set_type(a, sp.TNat)
sp.set_type(b, sp.TNat)
return sp.as_nat(a - b, message=EC.SUBTRACTION_UNDERFLOW)
"""
params:
a: TExp
b: TExp
return: TExp
"""
def mul_exp_exp(self, a, b):
sp.set_type(a, TExp)
sp.set_type(b, TExp)
return self.makeExp(a.mantissa * b.mantissa // self.data.expScale)
"""
params:
a: TExp
b: TNat
return: TExp
"""
def mul_exp_nat(self, a, b):
sp.set_type(a, TExp)
sp.set_type(b, sp.TNat)
return self.makeExp(a.mantissa * b)
"""
params:
a: TNat
b: TExp
return: TNat
"""
def mul_nat_exp(self, a, b):
sp.set_type(a, sp.TNat)
sp.set_type(b, TExp)
return a * b.mantissa // self.data.expScale
"""
params:
a: TExp
b: TExp
return: TExp
"""
def div_exp_exp(self, a, b):
sp.set_type(a, TExp)
sp.set_type(b, TExp)
sp.verify(b.mantissa > 0, EC.DIVISION_BY_ZERO)
return self.makeExp(a.mantissa * self.data.expScale // b.mantissa)
"""
params:
a: TExp
b: TNat
return: TExp
"""
def div_exp_nat(self, a, b):
sp.set_type(a, TExp)
sp.set_type(b, sp.TNat)
sp.verify(b > 0, EC.DIVISION_BY_ZERO)
return self.makeExp(a.mantissa // b)
"""
params:
a: TNat
b: TExp
return: TNat
"""
def div_nat_exp(self, a, b):
sp.set_type(a, sp.TNat)
sp.set_type(b, TExp)
sp.verify(b.mantissa > 0, EC.DIVISION_BY_ZERO)
return a * self.data.expScale // b.mantissa
def div_nat_exp_ceil(self, a, b):
sp.set_type(a, sp.TNat)
sp.set_type(b, TExp)
sp.verify(b.mantissa > 0, EC.DIVISION_BY_ZERO)
numerator = sp.compute(a * self.data.expScale)
denominator = b.mantissa
result = sp.compute(numerator // denominator)
sp.if numerator % denominator > 0:
result += 1
return result