forked from regen-network/mainnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
math.go
88 lines (71 loc) · 1.58 KB
/
math.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
package main
import "github.com/cockroachdb/apd/v2"
type Dec struct {
dec apd.Decimal
}
var dec128Context = apd.Context{
Precision: 34,
MaxExponent: apd.MaxExponent,
MinExponent: apd.MinExponent,
Traps: apd.DefaultTraps,
}
func NewDecFromString(s string) (Dec, error) {
d, _, err := apd.NewFromString(s)
if err != nil {
return Dec{}, err
}
return Dec{*d}, nil
}
func NewDecFromInt64(x int64) Dec {
var res Dec
res.dec.SetInt64(x)
return res
}
func (x Dec) Add(y Dec) (Dec, error) {
var z Dec
_, err := apd.BaseContext.Add(&z.dec, &x.dec, &y.dec)
return z, err
}
func (x Dec) Sub(y Dec) (Dec, error) {
var z Dec
_, err := apd.BaseContext.Sub(&z.dec, &x.dec, &y.dec)
return z, err
}
func (x Dec) Quo(y Dec) (Dec, error) {
var z Dec
_, err := dec128Context.Quo(&z.dec, &x.dec, &y.dec)
return z, err
}
func (x Dec) QuoInteger(y Dec) (Dec, error) {
var z Dec
_, err := dec128Context.QuoInteger(&z.dec, &x.dec, &y.dec)
return z, err
}
func (x Dec) Rem(y Dec) (Dec, error) {
var z Dec
_, err := dec128Context.Rem(&z.dec, &x.dec, &y.dec)
return z, err
}
func (x Dec) Mul(y Dec) (Dec, error) {
var z Dec
_, err := dec128Context.Mul(&z.dec, &x.dec, &y.dec)
return z, err
}
func (x Dec) Int64() (int64, error) {
return x.dec.Int64()
}
func (x Dec) String() string {
return x.dec.Text('f')
}
func (x Dec) IsEqual(y Dec) bool {
return x.dec.Cmp(&y.dec) == 0
}
func (x Dec) IsZero() bool {
return x.dec.IsZero()
}
func (x Dec) IsPositive() bool {
return !x.dec.Negative && !x.dec.IsZero()
}
func (x Dec) IsNegative() bool {
return x.dec.Negative && !x.dec.IsZero()
}