-
Notifications
You must be signed in to change notification settings - Fork 61
/
util.go
157 lines (137 loc) · 3.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
package decimal
import (
"fmt"
"math"
"math/big"
"sync"
"github.com/ericlagergren/decimal/internal/arith"
"github.com/ericlagergren/decimal/internal/arith/checked"
"github.com/ericlagergren/decimal/internal/arith/pow"
"github.com/ericlagergren/decimal/internal/c"
)
const debug = true
var (
_ fmt.Stringer = (*Big)(nil)
_ fmt.Formatter = (*Big)(nil)
)
var intPool = sync.Pool{New: func() interface{} { return new(big.Int) }}
func get() *big.Int { return intPool.Get().(*big.Int) }
func getInt(x *big.Int) *big.Int { return get().Set(x) }
func getInt64(x int64) *big.Int { return get().SetInt64(x) }
func putInt(b *big.Int) { intPool.Put(b) }
// cmpNorm compares x and y in the range [0.1, 0.999...] and returns true if x
// > y.
func cmpNorm(x int64, xs int32, y int64, ys int32) (ok bool) {
if debug && (x == 0 || y == 0) {
panic("x and/or y cannot be zero")
}
if diff := xs - ys; diff != 0 {
// TODO: should we check the bool result here?
if diff < 0 {
x, _ = checked.MulPow10(x, -diff)
} else {
y, _ = checked.MulPow10(y, diff)
}
}
if x != c.Inflated {
if y != c.Inflated {
return arith.AbsCmp(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.
func cmpNormBig(x *big.Int, xs int32, y *big.Int, ys int32) (ok bool) {
diff := xs - ys
if diff < 0 {
x1 := new(big.Int).Set(x)
return checked.MulBigPow10(x1, -diff).Cmp(y) > 0
}
y1 := new(big.Int).Set(y)
return x.Cmp(checked.MulBigPow10(y1, diff)) > 0
}
// findScale determines the precision of a float64.
func findScale(f float64) (precision int32) {
switch {
case f == 0.0, math.Floor(f) == f:
return 0
case math.IsNaN(f), math.IsInf(f, 0):
return c.BadScale
}
e := float64(1)
p := int32(0)
for {
e *= 10
p++
cmp := round(f*e) / e
if math.IsNaN(cmp) || cmp == f {
break
}
}
return p
}
// TODO(eric): use math.Round when 1.10 lands.
// The default rounding should be unbiased rounding.
// It takes marginally longer than
//
// if f < 0 {
// return math.Ceil(f - 0.5)
// }
// return math.Floor(f + 0.5)
//
// But returns more accurate results.
func round(f float64) float64 {
d, frac := math.Modf(f)
if f > 0.0 && (frac > +0.5 || (frac == 0.5 && uint64(d)%2 != 0)) {
return d + 1.0
}
if f < 0.0 && (frac < -0.5 || (frac == -0.5 && uint64(d)%2 != 0)) {
return d - 1.0
}
return d
}
// "stolen" from https://golang.org/pkg/math/big/#Rat.SetFloat64
// Removed non-finite case because we already check for
// Inf/NaN values
func bigIntFromFloat(f float64) *big.Int {
const expMask = 1<<11 - 1
bits := math.Float64bits(f)
mantissa := bits & (1<<52 - 1)
exp := int((bits >> 52) & expMask)
if exp == 0 { // denormal
exp -= 1022
} else { // normal
mantissa |= 1 << 52
exp -= 1023
}
shift := 52 - exp
// Optimization (?): partially pre-normalise.
for mantissa&1 == 0 && shift > 0 {
mantissa >>= 1
shift--
}
if shift < 0 {
shift = -shift
}
var a big.Int
a.SetUint64(mantissa)
return a.Lsh(&a, uint(shift))
}
// scalex adjusts x by scale. If scale < 0, x = x * 10^-scale, otherwise
// x = x / 10^scale.
func scalex(x int64, scale int32) (sx int64, ok bool) {
if scale < 0 {
sx, ok = checked.MulPow10(x, -scale)
if !ok {
return 0, false
}
return sx, true
}
p, ok := pow.Ten64(int64(scale))
if !ok {
return 0, false
}
return x / p, true
}