-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsigned_decimal.move.template
266 lines (223 loc) · 7.9 KB
/
signed_decimal.move.template
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
// Auto generated from gen-move-math
// https://github.com/fardream/gen-move-math
// Manual edit with caution.
// Arguments: {{.Args}}
// Version: {{.Version}}
module {{.Address}}::{{.ModuleName}} {
use {{.Address}}::{{.DoubleWidthModule}};
use {{.Address}}::{{.SignedIntModule}}::{Self, {{.SignedIntType}}};
const E_INVALID_DECIMAL: u64 = 1001;
const E_OVERFLOW: u64 = 1002;
// PRECISION is the max decimals in operations.
const PRECISION: u8 = {{.Precision}};
// {{.TypeName}}
struct {{.TypeName}} has store, copy, drop {
value: {{.SignedIntType}},
decimal: u8,
}
// Get the decimal of this value
public fun get_decimal(d: &{{.TypeName}}): u8 {
d.decimal
}
// is zero indicates if the value is 0.
public fun is_zero(d: &{{.TypeName}}): bool {
{{.SignedIntModule}}::is_zero(d.value)
}
// equalize_decimal make x and y to be of same decimal
public fun equalize_decimal(x: {{.TypeName}}, y: {{.TypeName}}): ({{.TypeName}}, {{.TypeName}}) {
let x = reduce(&x);
let y = reduce(&y);
if (x.decimal > y.decimal) {
y = raise_decimal(&y, x.decimal);
} else if (x.decimal < y.decimal) {
x = raise_decimal(&x, y.decimal);
};
(x, y)
}
// equal checks if x equals y
public fun equal(x: {{.TypeName}}, y: {{.TypeName}}): bool {
let (x, y) = equalize_decimal(x, y);
{{.SignedIntModule}}::equal(x.value, y.value)
}
// greater: x > y?
public fun greater(x: {{.TypeName}}, y: {{.TypeName}}): bool {
let (x, y) = equalize_decimal(x, y);
{{.SignedIntModule}}::greater(x.value, y.value)
}
// less: x < y?
public fun less(x: {{.TypeName}}, y: {{.TypeName}}): bool {
let (x, y) = equalize_decimal(x, y);
{{.SignedIntModule}}::less(x.value, y.value)
}
// new creates a new signed decimal.
public fun new(abs: {{.BaseTypeName}}, negative: bool, decimal: u8): {{.TypeName}} {
{{.TypeName}} {
value: {{.SignedIntModule}}::new(abs, negative),
decimal,
}
}
// new_with_value creates a new signed decimal with a signed integer.
public fun new_with_value(value: {{.SignedIntType}}, decimal: u8): {{.TypeName}} {
{{.TypeName}} {
value,
decimal,
}
}
// get the underlying signed integer.
public fun get_value(d: &{{.TypeName}}): {{.SignedIntType}} {
d.value
}
// get the absolute value of the signed integer.
public fun get_value_abs(d: &{{.TypeName}}): {{.BaseTypeName}} {
{{.SignedIntModule}}::abs(d.value)
}
// check if value is negative
public fun is_negative(d: &{{.TypeName}}): bool {
{{.SignedIntModule}}::is_negative(d.value)
}
// lower the decimal.
// 1st return value is the value with lowered decimal, 2nd return value is the reminder.
public fun lower_decimal(d: &{{.TypeName}}, decimal: u8): ({{.TypeName}}, {{.TypeName}}) {
let v = get_value(d);
let current_decimal = get_decimal(d);
assert!(
current_decimal > decimal,
E_INVALID_DECIMAL
);
let m: {{.BaseTypeName}} = 1;
let i = current_decimal;
while (i > decimal) {
m = m * 10;
i = i - 1;
};
let m = {{.SignedIntModule}}::new(m, false);
(
new_with_value({{.SignedIntModule}}::divide(v, m), decimal),
new_with_value({{.SignedIntModule}}::mod(v, m), current_decimal)
)
}
// raise decimal
public fun raise_decimal(d: &{{.TypeName}}, decimal: u8): {{.TypeName}} {
let v = get_value(d);
let current_decimal = get_decimal(d);
assert!(
current_decimal < decimal,
E_INVALID_DECIMAL
);
let m: {{.BaseTypeName}} = 1;
let i = current_decimal;
while (i < decimal) {
m = m * 10;
i = i + 1;
};
let m = {{.SignedIntModule}}::new(m, false);
new_with_value({{.SignedIntModule}}::multiply(v, m), decimal)
}
// reduce tries to remove trailing 0s from the value.
public fun reduce(d: &{{.TypeName}}): {{.TypeName}} {
if (is_zero(d)) {
new(0, false, 0)
} else {
let v = get_value_abs(d);
let i = get_decimal(d);
while (i > 0) {
if (v%10 == 0) {
i = i - 1;
v = v/10;
} else {
break
};
};
new(v, is_negative(d), i)
}
}
public fun add(x: {{.TypeName}}, y: {{.TypeName}}): {{.TypeName}} {
let dx = get_decimal(&x);
let dy = get_decimal(&y);
if (dx > dy) {
y = raise_decimal(&y, dx);
} else if (dy > dx) {
x = raise_decimal(&x, dy);
dx = dy;
};
reduce(&new_with_value({{.SignedIntModule}}::add(get_value(&x), get_value(&y)), dx))
}
public fun subtract(x: {{.TypeName}}, y: {{.TypeName}}): {{.TypeName}} {
let dx = get_decimal(&x);
let dy = get_decimal(&y);
if (dx > dy) {
y = raise_decimal(&y, dx);
} else if (dy > dx) {
x = raise_decimal(&x, dy);
dx = dy;
};
reduce(&new_with_value({{.SignedIntModule}}::subtract(get_value(&x), get_value(&y)), dx))
}
public fun multiply(x: {{.TypeName}}, y: {{.TypeName}}): {{.TypeName}} {
let xv = get_value_abs(&x);
let yv = get_value_abs(&y);
let (lo, hi) = {{.DoubleWidthModule}}::underlying_mul_with_carry(xv, yv);
let vabs = {{.DoubleWidthModule}}::new(hi, lo);
let d = get_decimal(&x) + get_decimal(&y);
let ten= {{.DoubleWidthModule}}::new(0, 10);
while (d > 0) {
let (k, r) = {{.DoubleWidthModule}}::divide_mod(vabs, ten);
if ({{.DoubleWidthModule}}::is_zero(&r)) {
vabs = k;
d = d - 1;
} else {
break
};
};
assert!(
{{.DoubleWidthModule}}::hi(vabs) == 0,
E_OVERFLOW
);
let is_negative = is_negative(&x) != is_negative(&y);
new({{.DoubleWidthModule}}::lo(vabs), is_negative, d)
}
public fun divide(x: {{.TypeName}}, y: {{.TypeName}}): {{.TypeName}} {
let yv_abs = {{.DoubleWidthModule}}::new(0, get_value_abs(&y));
let xv_abs = {{.DoubleWidthModule}}::new(0, get_value_abs(&x));
let dx = get_decimal(&x);
let dy = get_decimal(&y);
let ten = {{.DoubleWidthModule}}::new(0, 10);
let precision = if (dx > PRECISION) {
0
} else {
PRECISION - dx
};
let ed = 0;
while (ed < dy || ed < precision) {
xv_abs = {{.DoubleWidthModule}}::multiply(xv_abs, ten);
ed = ed + 1;
};
let vabs = {{.DoubleWidthModule}}::divide(xv_abs, yv_abs);
assert!(
{{.DoubleWidthModule}}::hi(vabs) == 0,
E_OVERFLOW
);
new({{.DoubleWidthModule}}::lo(vabs), is_negative(&x) != is_negative(&y), dx + ed - dy)
}
public fun divide_mod(x: {{.TypeName}}, y: {{.TypeName}}): ({{.TypeName}}, {{.TypeName}}) {
let d = divide(x, y);
let r = subtract(x, multiply(d, y));
(d, r)
}
public fun mod(x: {{.TypeName}}, y: {{.TypeName}}): {{.TypeName}} {
let (_, r) = divide_mod(x, y);
r
}
{{ if .DoTest }}
#[test]
fun test_decimal{{.BaseWidth}}_p{{.Precision}}() {
let c = new(1800, false, 0);
let d = new(3600, false, 0);
let r = divide(c, d);
assert!(equal(r, new(5, false, 1)), (r.decimal as u64));
let c = new(5, false, 1);
let d = new(2, true, 2);
let r = multiply(c, d);
assert!(equal(r, new(10, true, 3)), (r.decimal as u64))
}
{{end}}}