-
Notifications
You must be signed in to change notification settings - Fork 116
/
Decimal.move
169 lines (140 loc) · 4.71 KB
/
Decimal.move
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
address 0x1 {
module Decimal {
// The Move Decimal data structure is optimized for readability and compatibility.
// In particular it is indended for compatibility with the underlying rust_decimal crate https://github.com/paupino/rust-decimal. In that library a new decimal type is initialized with Decimal::from_i128_with_scale(mantissa: i128, scale: u32)
// Note: While the underlying Rust crate type has optimal storage characteristics, this Move decimal representation is NOT optimized for storage.
struct Decimal has key, store, drop {
sign: bool,
int: u128,
scale: u8, // max intger is number 28
}
// while stored in u128, the largest integer possible in the rust_decimal vm dependency is 2^96
const MAX_RUST_DECIMAL_U128: u128 = 79228162514264337593543950335;
// pair decimal ops
const ADD: u8 = 1;
const SUB: u8 = 2;
const MUL: u8 = 3;
const DIV: u8 = 4;
const POW: u8 = 5;
const ROUND: u8 = 6;
// single ops
const SQRT: u8 = 100;
const TRUNC: u8 = 101;
const ROUND_MID_TO_EVEN: u8 = 0; // This is the default in the rust_decimal lib.
const ROUND_MID_FROM_ZERO: u8 = 1;
native public fun decimal_demo(sign: bool, int: u128, scale: u8): (bool, u128, u8);
native public fun single_op(op_id: u8, sign: bool, int: u128, scale: u8): (bool, u128, u8);
native public fun pair_op(
op_id: u8,
rounding_strategy_id: u8,
// left number
sign_1: bool,
int_1: u128,
scale_1: u8,
// right number
sign_2: bool,
int_2: u128,
scale_3: u8
): (bool, u128, u8);
public fun new(sign: bool, int: u128, scale: u8): Decimal {
assert(int < MAX_RUST_DECIMAL_U128, 01);
// check scale < 28
assert(scale < 28, 02);
return Decimal {
sign: sign,
int: int,
scale: scale
}
}
/////// SUGAR /////////
public fun trunc(d: &Decimal): Decimal {
let (sign, int, scale) = single_op(TRUNC, *&d.sign, *&d.int, *&d.scale);
return Decimal {
sign: sign,
int: int,
scale: scale,
}
}
public fun sqrt(d: &Decimal): Decimal {
let (sign, int, scale) = single_op(SQRT, *&d.sign, *&d.int, *&d.scale);
return Decimal {
sign: sign,
int: int,
scale: scale,
}
}
public fun add(l: &Decimal, r: &Decimal): Decimal {
let (sign, int, scale) = pair_op(ADD, ROUND_MID_TO_EVEN, *&l.sign, *&l.int, *&l.scale, *&r.sign, *&r.int, *&r.scale);
return Decimal {
sign: sign,
int: int,
scale: scale,
}
}
public fun sub(l: &Decimal, r: &Decimal): Decimal {
let (sign, int, scale) = pair_op(SUB, ROUND_MID_TO_EVEN, *&l.sign, *&l.int, *&l.scale, *&r.sign, *&r.int, *&r.scale);
return Decimal {
sign: sign,
int: int,
scale: scale,
}
}
public fun mul(l: &Decimal, r: &Decimal): Decimal {
let (sign, int, scale) = pair_op(MUL, ROUND_MID_TO_EVEN, *&l.sign, *&l.int, *&l.scale, *&r.sign, *&r.int, *&r.scale);
return Decimal {
sign: sign,
int: int,
scale: scale,
}
}
public fun div(l: &Decimal, r: &Decimal): Decimal {
let (sign, int, scale) = pair_op(DIV, ROUND_MID_TO_EVEN, *&l.sign, *&l.int, *&l.scale, *&r.sign, *&r.int, *&r.scale);
return Decimal {
sign: sign,
int: int,
scale: scale,
}
}
public fun rescale(l: &Decimal, r: &Decimal): Decimal {
let (sign, int, scale) = pair_op(0, ROUND_MID_TO_EVEN, *&l.sign, *&l.int, *&l.scale, *&r.sign, *&r.int, *&r.scale);
return Decimal {
sign: sign,
int: int,
scale: scale,
}
}
public fun round(l: &Decimal, r: &Decimal, strategy: u8): Decimal {
let (sign, int, scale) = pair_op(ROUND, strategy, *&l.sign, *&l.int, *&l.scale, *&r.sign, *&r.int, *&r.scale);
return Decimal {
sign: sign,
int: int,
scale: scale,
}
}
public fun power(l: &Decimal, r: &Decimal): Decimal {
let (sign, int, scale) = pair_op(POW, ROUND_MID_TO_EVEN, *&l.sign, *&l.int, *&l.scale, *&r.sign, *&r.int, *&r.scale);
return Decimal {
sign: sign,
int: int,
scale: scale,
}
}
///// GETTERS /////
// unwrap creates a new decimal instance
public fun unwrap(d: &Decimal): (bool, u128, u8) {
return (*&d.sign, *&d.int, *&d.scale)
}
// borrow sign
public fun borrow_sign(d: &Decimal): &bool {
return &d.sign
}
// borrows the value of the integer
public fun borrow_int(d: &Decimal): &u128 {
return &d.int
}
// borrow sign
public fun borrow_scale(d: &Decimal): &u8 {
return &d.scale
}
}
}