forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decimal.go
330 lines (291 loc) · 8.25 KB
/
decimal.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
// Copyright 2016 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
//
// Author: Nathan VanBenschoten (nvanbenschoten@gmail.com)
package decimal
import (
"fmt"
"math"
"strconv"
"strings"
"gopkg.in/inf.v0"
)
// NewDecFromFloat allocates and returns a new Dec set to the given
// float64 value. The function will panic if the float is NaN or ±Inf.
func NewDecFromFloat(f float64) *inf.Dec {
return SetFromFloat(new(inf.Dec), f)
}
// SetFromFloat sets z to the given float64 value and returns z. The
// function will panic if the float is NaN or ±Inf.
func SetFromFloat(z *inf.Dec, f float64) *inf.Dec {
switch {
case math.IsInf(f, 0):
panic("cannot create a decimal from an infinte float")
case math.IsNaN(f):
panic("cannot create a decimal from an NaN float")
}
s := strconv.FormatFloat(f, 'e', -1, 64)
// Determine the decimal's exponent.
var e10 int64
e := strings.IndexByte(s, 'e')
for i := e + 2; i < len(s); i++ {
e10 = e10*10 + int64(s[i]-'0')
}
switch s[e+1] {
case '-':
e10 = -e10
case '+':
default:
panic(fmt.Sprintf("malformed float: %v -> %s", f, s))
}
e10++
// Determine the decimal's mantissa.
var mant int64
i := 0
neg := false
if s[0] == '-' {
i++
neg = true
}
for ; i < e; i++ {
if s[i] == '.' {
continue
}
mant = mant*10 + int64(s[i]-'0')
e10--
}
if neg {
mant = -mant
}
return z.SetUnscaled(mant).SetScale(inf.Scale(-e10))
}
// Float64FromDec converts a decimal to a float64 value, returning
// the value and any error that occurred. This converson exposes a
// possible loss of information.
func Float64FromDec(dec *inf.Dec) (float64, error) {
return strconv.ParseFloat(dec.String(), 64)
}
// Mod performs the modulo arithmatic x % y and stores the
// result in z, which is also the return value. It is valid for z
// to be nil, in which case it will be allocated internally.
// Mod will panic if the y is zero.
//
// The modulo calculation is implemented using the algorithm:
// x % y = x - (y * ⌊x / y⌋).
func Mod(z, x, y *inf.Dec) *inf.Dec {
switch z {
case nil:
z = new(inf.Dec)
case x:
x = new(inf.Dec)
x.Set(z)
if z == y {
y = x
}
case y:
y = new(inf.Dec)
y.Set(z)
}
z.QuoRound(x, y, 0, inf.RoundDown)
return z.Sub(x, z.Mul(z, y))
}
// Sqrt calculates the square root of x to the specified scale
// and stores the result in z, which is also the return value.
// The function will panic if x is a negative number.
//
// The square root calculation is implemented using Newton's Method.
// We start with an initial estimate for sqrt(d), and then iterate:
// x_{n+1} = 1/2 * ( x_n + (d / x_n) ).
func Sqrt(z, x *inf.Dec, s inf.Scale) *inf.Dec {
switch z {
case nil:
z = new(inf.Dec)
case x:
x = new(inf.Dec)
x.Set(z)
}
// Validate the sign of x.
switch x.Sign() {
case -1:
panic(fmt.Sprintf("square root of negative number: %s", x))
case 0:
return z.SetUnscaled(0).SetScale(0)
}
// Use half as the initial estimate.
z.Mul(x, decimalHalf)
// Iterate.
tmp := new(inf.Dec)
for loop := newLoop("sqrt", z, s, 1); ; {
tmp.QuoRound(x, z, s+2, inf.RoundHalfUp) // t = d / x_n
tmp.Add(tmp, z) // t = x_n + (d / x_n)
z.Mul(tmp, decimalHalf) // x_{n+1} = 0.5 * t
if loop.done(z) {
break
}
}
// Round to the desired scale.
return z.Round(z, s, inf.RoundHalfUp)
}
// Cbrt calculates the cube root of x to the specified scale
// and stores the result in z, which is also the return value.
//
// The cube root calculation is implemented using Newton-Raphson
// method. We start with an initial estimate for cbrt(d), and
// then iterate:
// x_{n+1} = 1/3 * ( 2 * x_n + (d / x_n / x_n) ).
func Cbrt(z, x *inf.Dec, s inf.Scale) *inf.Dec {
switch z {
case nil:
z = new(inf.Dec)
case x:
x = new(inf.Dec)
x.Set(z)
}
// Validate the sign of x.
switch x.Sign() {
case -1:
// Make sure args aren't mutated and return -Cbrt(-x).
x = new(inf.Dec).Neg(x)
z = Cbrt(z, x, s)
return z.Neg(z)
case 0:
return z.SetUnscaled(0).SetScale(0)
}
z.Set(x)
exp8 := 0
// Follow Ken Turkowski paper:
// https://people.freebsd.org/~lstewart/references/apple_tr_kt32_cuberoot.pdf
//
// Computing the cube root of any number is reduced to computing
// the cube root of a number between 0.125 and 1. After the next loops,
// x = z * 8^exp8 will hold.
for z.Cmp(decimalOneEighth) < 0 {
exp8--
z.Mul(z, decimalEight)
}
for z.Cmp(decimalOne) > 0 {
exp8++
z.Mul(z, decimalOneEighth)
}
// Use this polynomial to approximate the cube root between 0.125 and 1.
// z = (-0.46946116 * z + 1.072302) * z + 0.3812513
// It will serve as an initial estimate, hence the precision of this
// computation may only impact performance, not correctness.
z0 := new(inf.Dec).Set(z)
z.Mul(z, decimalCbrtC1)
z.Add(z, decimalCbrtC2)
z.Mul(z, z0)
z.Add(z, decimalCbrtC3)
for ; exp8 < 0; exp8++ {
z.Mul(z, decimalHalf)
}
for ; exp8 > 0; exp8-- {
z.Mul(z, decimalTwo)
}
z0.Set(z)
// Loop until convergence.
for loop := newLoop("cbrt", x, s, 1); ; {
// z = (2.0 * z0 + x / (z0 * z0) ) / 3.0;
z.Set(z0)
z.Mul(z, z0)
z.QuoRound(x, z, s+2, inf.RoundHalfUp)
z.Add(z, z0)
z.Add(z, z0)
z.QuoRound(z, decimalThree, s+2, inf.RoundHalfUp)
if loop.done(z) {
break
}
z0.Set(z)
}
// Round to the desired scale.
return z.Round(z, s, inf.RoundHalfUp)
}
// LogN computes the log of x with base n to the specified scale and
// stores the result in z, which is also the return value. The function
// will panic if x is a negative number or if n is a negative number.
func LogN(z *inf.Dec, x *inf.Dec, n *inf.Dec, s inf.Scale) *inf.Dec {
if z == n {
n = new(inf.Dec).Set(n)
}
z = Log(z, x, s+1)
return z.QuoRound(z, Log(nil, n, s+1), s, inf.RoundHalfUp)
}
// Log10 computes the log of x with base 10 to the specified scale and
// stores the result in z, which is also the return value. The function
// will panic if x is a negative number.
func Log10(z *inf.Dec, x *inf.Dec, s inf.Scale) *inf.Dec {
z = Log(z, x, s)
return z.QuoRound(z, decimalLog10, s, inf.RoundHalfUp)
}
// Log computes the natural log of x using the Maclaurin series for
// log(1-x) to the specified scale and stores the result in z, which
// is also the return value. The function will panic if x is a negative
// number.
func Log(z *inf.Dec, x *inf.Dec, s inf.Scale) *inf.Dec {
// Validate the sign of x.
if x.Sign() <= 0 {
panic(fmt.Sprintf("natural log of non-positive value: %s", x))
}
// Allocate if needed and make sure args aren't mutated.
x = new(inf.Dec).Set(x)
if z == nil {
z = new(inf.Dec)
} else {
z.SetUnscaled(0).SetScale(0)
}
// The series wants x < 1, and log 1/x == -log x, so exploit that.
invert := false
if x.Cmp(decimalOne) > 0 {
invert = true
x.QuoRound(decimalOne, x, s*2, inf.RoundHalfUp)
}
// x = mantissa * 2**exp, and 0.5 <= mantissa < 1.
// So log(x) is log(mantissa)+exp*log(2), and 1-x will be
// between 0 and 0.5, so the series for 1-x will converge well.
// (The series converges slowly in general.)
exp2 := int64(0)
for x.Cmp(decimalHalf) < 0 {
x.Mul(x, decimalTwo)
exp2--
}
exp := inf.NewDec(exp2, 0)
exp.Mul(exp, decimalLog2)
if invert {
exp.Neg(exp)
}
// y = 1-x (whereupon x = 1-y and we use that in the series).
y := inf.NewDec(1, 0)
y.Sub(y, x)
// The Maclaurin series for log(1-y) == log(x) is: -y - y²/2 - y³/3 ...
yN := new(inf.Dec).Set(y)
term := new(inf.Dec)
n := inf.NewDec(1, 0)
// Loop over the Maclaurin series given above until convergence.
for loop := newLoop("log", x, s, 40); ; {
n.SetUnscaled(int64(loop.i + 1))
term.QuoRound(yN, n, s+2, inf.RoundHalfUp)
z.Sub(z, term)
if loop.done(z) {
break
}
// Advance y**index (multiply by y).
yN.Mul(yN, y)
}
if invert {
z.Neg(z)
}
z.Add(z, exp)
// Round to the desired scale.
return z.Round(z, s, inf.RoundHalfUp)
}