-
Notifications
You must be signed in to change notification settings - Fork 0
/
operations.go
308 lines (261 loc) · 7.95 KB
/
operations.go
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
298
299
300
301
302
303
304
305
306
307
308
package decimal
import (
gomath "math"
"strings"
"github.com/ericlagergren/decimal"
"github.com/ericlagergren/decimal/math"
)
// Smallest positive number approaching zero in Golang
// It is a very small nonzero number representin needed
// as threshold denominator so we don't divide by zero in AlmostEqual
var epsilon = NewFromFloat64(gomath.Nextafter(1, 2) - 1)
// Cmp compares n to the decimal instance
func (dec Decimal) Cmp(n Decimal) int {
return dec.native().Cmp(n.native())
}
// Cmp compares a to b
func Cmp(a Decimal, b Decimal) int {
return a.Cmp(b)
}
// Equals returns true if n has the same value as the decimal instance
func (dec Decimal) Equals(n Decimal) bool {
return dec.Cmp(n) == 0
}
// Equals returns true if a has the same value as b
func Equals(a Decimal, b Decimal) bool {
return a.Equals(b)
}
// EqualsInterface returns true if v is an decimal and has the same value as the decimal instance
func (dec Decimal) EqualsInterface(v interface{}) bool {
switch x := v.(type) {
case Decimal:
return dec.Cmp(x) == 0
case *Decimal:
return dec.Cmp(*x) == 0
}
return false
}
// EqualsInterface returns true if v is an decimal and has the same value as d
func EqualsInterface(d Decimal, v interface{}) bool {
return d.EqualsInterface(v)
}
// Add adds n to the decimal instance
func (dec Decimal) Add(n Decimal) Decimal {
dec.native().Add(dec.native(), n.native())
return Decimal{dec.native()}
}
// Add adds a to b and returns a new decimal instance
// a and b will not be modified
func Add(a Decimal, b Decimal) Decimal {
d := NewFromDecimal(a)
return d.Add(b)
}
// Sub substracts n to the decimal instance
func (dec Decimal) Sub(n Decimal) Decimal {
dec.native().Sub(dec.native(), n.native())
return Decimal{dec.native()}
}
// Sub substracts b from a and returns a new decimal instance
// a and b will not be modified
func Sub(a Decimal, b Decimal) Decimal {
d := NewFromDecimal(a)
return d.Sub(b)
}
// Div divides n on the decimal instance
func (dec Decimal) Div(n Decimal) Decimal {
dec.native().Quo(dec.native(), n.native())
return Decimal{dec.native()}
}
// Div divides b from a and returns a new decimal instance
// a and b will not be modified
func Div(a Decimal, b Decimal) Decimal {
d := NewFromDecimal(a)
return d.Div(b)
}
// Mul multiplies n to the decimal instance
func (dec Decimal) Mul(n Decimal) Decimal {
dec.native().Mul(dec.native(), n.native())
return Decimal{dec.native()}
}
// Mul multiplies a to b and returns a new decimal instance
// a and b will not be modified
func Mul(a Decimal, b Decimal) Decimal {
d := NewFromDecimal(a)
return d.Mul(b)
}
// Mod modulos n on the decimal instance
func (dec Decimal) Mod(n Decimal) Decimal {
dec.native().Rem(dec.native(), n.native())
return Decimal{dec.native()}
}
// Mod modulos b on a and returns a new decimal instance
// a and b will not be modified
func Mod(a Decimal, b Decimal) Decimal {
d := NewFromDecimal(a)
return d.Mod(b)
}
// Floor rounds the instance down to the next whole number
func (dec Decimal) Floor() Decimal {
math.Floor(dec.native(), dec.native())
return Decimal{dec.native()}
}
// Floor rounds d down to the next whole number and returns it as a new instance
// d will not be modified
func Floor(a Decimal) Decimal {
d := NewFromDecimal(a)
return d.Floor()
}
// Ceil rounds the instance up to the next whole number
func (dec Decimal) Ceil() Decimal {
math.Ceil(dec.native(), dec.native())
return Decimal{dec.native()}
}
// Ceil rounds d up to the next whole number and returns it as a new instance
// d will not be modified
func Ceil(a Decimal) Decimal {
d := NewFromDecimal(a)
return d.Ceil()
}
// Round rounds the instance to the specific digits
func (dec Decimal) Round(digits int) Decimal {
dec.native().Round(digits)
return Decimal{dec.native()}
}
// Round rounds d to the specific digits and returns it as a new instance
// d will not be modified
func Round(a Decimal, digits int) Decimal {
d := NewFromDecimal(a)
return d.Round(digits)
}
// RoundDown rounds the instance down to the specific digits
func (dec Decimal) RoundDown(digits int) Decimal {
roundingMode := dec.nat.Context.RoundingMode
dec.nat.Context.RoundingMode = decimal.ToZero
dec.native().Round(digits)
dec.nat.Context.RoundingMode = roundingMode
return Decimal{dec.native()}
}
// RoundDown rounds d down to the specific digits and returns it as a new instance
// d will not be modified
func RoundDown(a Decimal, digits int) Decimal {
d := NewFromDecimal(a)
return d.RoundDown(digits)
}
// RoundToInt rounds the instance to the nearest integer
func (dec Decimal) RoundToInt() Decimal {
d := dec.native().RoundToInt()
return Decimal{d}
}
func RoundToInt(a Decimal) Decimal {
d := NewFromDecimal(a)
return d.RoundToInt()
}
// Truncate truncates the instance to the specific digits
func (dec Decimal) Truncate(digits int) Decimal {
parts := strings.SplitN(dec.String(), ".", 2)
if len(parts) <= 1 {
v, _ := NewFromString(parts[0])
dec.native().Copy(v.native())
return v
}
if digits > len(parts[1])-1 {
digits = len(parts[1])
}
v, _ := NewFromString(parts[0] + "." + parts[1][:digits])
dec.native().Copy(v.native())
return v
}
// Truncate truncates d to the specific digits and returns it as a new instance
// d will not be modified
func Truncate(a Decimal, digits int) Decimal {
d := NewFromDecimal(a)
return d.Truncate(digits)
}
// Quantize sets dec to the number equal in value and sign to dec with the scale, digits.
func (dec Decimal) Quantize(digits int) Decimal {
dec.native().Quantize(digits)
return Decimal{dec.native()}
}
// Quantize sets a to the number equal in value and sign to a with the scale, digits.
// a will not be modified.
func Quantize(a Decimal, digits int) Decimal {
d := NewFromDecimal(a)
return d.Quantize(digits)
}
// RoundToDigits rounds a to make it have as many digits if possible.
func (dec Decimal) RoundToDigits(digits int) Decimal {
prec := dec.native().Precision()
scale := dec.native().Scale()
// if we have more significant digits (prec) than
// digits after decimal point (scale) then we want
// to have the digits after decimal point
// set to (digits - (prec - scale))
// for example, 1.23 has prec=3 and scale=2
// we want to round it to 2 digits
// so we want the new scale to equal (2 - (3 - 2))=1
if scale < prec {
left := prec - scale
digits = digits - left
}
if digits < 0 {
digits = 0
}
dec.native().Quantize(digits)
return Decimal{dec.native()}
}
// RoundToDigits rounds a to make it have as many digits if possible.
// a will not be modified.
func RoundToDigits(a Decimal, digits int) Decimal {
d := NewFromDecimal(a)
return d.RoundToDigits(digits)
}
// Precision returns precision of dec.
func (dec Decimal) Precision() int {
return dec.native().Precision()
}
// Scale returns scale of dec.
func (dec Decimal) Scale() int {
return dec.native().Scale()
}
// Abs returns absolute value of a
func Abs(a Decimal) Decimal {
d := NewFromDecimal(a)
return d.Abs()
}
// Abs returns absolute value of dec
func (dec Decimal) Abs() Decimal {
dec.native().Abs(dec.native())
return Decimal{dec.native()}
}
// Min returns the smallest of a and b
func Min(a, b Decimal) Decimal {
if a.Cmp(b) <= 0 {
return Decimal{a.native()}
}
return Decimal{b.native()}
}
// Min returns the largest of a and b
func Max(a, b Decimal) Decimal {
if a.Cmp(b) >= 0 {
return Decimal{a.native()}
}
return Decimal{b.native()}
}
// AlmostEquals checks if n almost equal to dec within a relative tolerance
// Relative tolerance is the ratio of the difference between the two values over the smallest of them
func (dec Decimal) AlmostEquals(n, tolerance Decimal) bool {
// check if the 2 numbers are exactly equal first
if dec.Equals(n) {
return true
}
min := Min(dec, n)
diff := Abs(Sub(dec, n))
if Abs(min).Equals(Zero()) {
return diff.Cmp(tolerance) < 0
}
return Div(diff, Max(epsilon, min)).Cmp(tolerance) < 0
}
// AlmostEquals checks if a almost equal to b within a relative tolerance
func AlmostEquals(a, b, tolerance Decimal) bool {
return a.AlmostEquals(b, tolerance)
}