forked from ericlagergren/decimal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
201 lines (181 loc) · 4.34 KB
/
util.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
package decimal
import (
"math/big"
"github.com/grodriguez85/decimal/internal/arith"
"github.com/grodriguez85/decimal/internal/arith/checked"
cst "github.com/grodriguez85/decimal/internal/c"
)
func (z *Big) norm() *Big {
if arith.IsUint64(&z.unscaled) {
if v := z.unscaled.Uint64(); v != cst.Inflated {
z.compact = v
z.precision = arith.Length(v)
return z
}
}
z.precision = arith.BigLength(&z.unscaled)
z.compact = cst.Inflated
return z
}
func (c Context) fix(z *Big) *Big {
adj := z.adjusted()
if adj > c.maxScale() {
prec := precision(c)
if z.compact == 0 {
z.exp = c.maxScale()
z.Context.Conditions |= Clamped
return z
}
switch m := c.RoundingMode; m {
case ToNearestAway, ToNearestEven:
z.SetInf(z.Signbit())
case AwayFromZero:
// OK
case ToZero:
z.exp = c.maxScale() - prec + 1
case ToPositiveInf, ToNegativeInf:
if m == ToPositiveInf == z.Signbit() {
z.exp = c.maxScale() - prec + 1
} else {
z.SetInf(false)
}
}
z.Context.Conditions |= Overflow | Inexact | Rounded
return z
}
if adj < c.minScale() {
tiny := c.etiny()
if z.compact == 0 {
if z.exp < tiny {
z.setZero(z.form, tiny)
z.Context.Conditions |= Clamped
}
return z
}
z.Context.Conditions |= Subnormal
if z.exp < tiny {
if c.shiftr(z, uint64(tiny-z.exp)) {
z.compact = 1
}
z.Context.Conditions |= Underflow
z.exp = tiny
if z.compact == 0 {
z.Context.Conditions |= Clamped
}
}
}
return z
}
// alias returns z if z != x, otherwise a newly-allocated big.Int.
func alias(z, x *big.Int) *big.Int {
if z != x {
// We have to check the first element of their internal slices since
// Big doesn't store a pointer to a big.Int.
zb, xb := z.Bits(), x.Bits()
if cap(zb) > 0 && cap(xb) > 0 && &zb[0:cap(zb)][cap(zb)-1] != &xb[0:cap(xb)][cap(xb)-1] {
return z
}
}
return new(big.Int)
}
func (z *Big) invalidContext(c Context) bool {
switch {
case c.Precision < 0:
z.setNaN(InvalidContext, qnan, invctxpltz)
case c.Precision > UnlimitedPrecision:
z.setNaN(InvalidContext, qnan, invctxpgtu)
case c.RoundingMode >= unnecessary:
z.setNaN(InvalidContext, qnan, invctxrmode)
case c.OperatingMode > Go:
z.setNaN(InvalidContext, qnan, invctxomode)
case c.MaxScale > MaxScale:
z.setNaN(InvalidContext, qnan, invctxsgtu)
case c.MinScale < MinScale:
z.setNaN(InvalidContext, qnan, invctxsltu)
default:
return false
}
return true
}
func precision(c Context) (p int) {
if p := c.Precision; p != 0 {
return p
}
return DefaultPrecision
}
// copybits can be useful when we want to allocate a big.Int without calling
// new or big.Int.Set. For example:
//
// var x big.Int
// if foo {
// x.SetBits(copybits(y.Bits()))
// }
// ...
//
func copybits(x []big.Word) []big.Word {
z := make([]big.Word, len(x))
copy(z, x)
return z
}
// cmpNorm compares x and y in the range [0.1, 0.999...] and returns true if x
// > y.
func cmpNorm(x uint64, xs int, y uint64, ys int) (ok bool) {
goodx, goody := true, true
// xs, ys > 0, so no overflow
if diff := xs - ys; diff != 0 {
if diff < 0 {
x, goodx = checked.MulPow10(x, uint64(-diff))
} else {
y, goody = checked.MulPow10(y, uint64(diff))
}
}
if goodx {
if goody {
return arith.Cmp(x, y) > 0
}
return false
}
return true
}
// cmpNormBig compares x and y in the range [0.1, 0.999...] and returns true if
// x > y. It uses z as backing storage, provided it does not alias x or y.
func cmpNormBig(z, x *big.Int, xs int, y *big.Int, ys int) (ok bool) {
if xs != ys {
z = alias(alias(z, x), y)
if xs < ys {
x = checked.MulBigPow10(z, x, uint64(ys-xs))
} else {
y = checked.MulBigPow10(z, y, uint64(xs-ys))
}
}
// x and y are non-negative
return x.Cmp(y) > 0
}
// scalex adjusts x by scale. If scale > 0, x = x * 10^scale, otherwise
// x = x / 10^-scale.
func scalex(x uint64, scale int) (sx uint64, ok bool) {
if scale > 0 {
if sx, ok = checked.MulPow10(x, uint64(scale)); !ok {
return 0, false
}
return sx, true
}
p, ok := arith.Pow10(uint64(-scale))
if !ok {
return 0, false
}
return x / p, true
}
// bigScalex sets z to the big.Int equivalient of scalex.
func bigScalex(z, x *big.Int, scale int) *big.Int {
if scale > 0 {
return checked.MulBigPow10(z, x, uint64(scale))
}
return z.Quo(x, arith.BigPow10(uint64(-scale)))
}
func min(x, y int) int {
if x < y {
return x
}
return y
}