1 // Package inf (type inf.Dec) implements "infinite-precision" decimal
3 // "Infinite precision" describes two characteristics: practically unlimited
4 // precision for decimal number representation and no support for calculating
5 // with any specific fixed precision.
6 // (Although there is no practical limit on precision, inf.Dec can only
7 // represent finite decimals.)
9 // This package is currently in experimental stage and the API may change.
11 // This package does NOT support:
12 // - rounding to specific precisions (as opposed to specific decimal positions)
13 // - the notion of context (each rounding must be explicit)
14 // - NaN and Inf values, and distinguishing between positive and negative zero
15 // - conversions to and from float32/64 types
17 // Features considered for possible addition:
18 // + formatting options
20 // + combined operations such as AddRound/MulAdd etc
21 // + exchanging data in decimal32/64/128 formats
23 package inf // import "gopkg.in/inf.v0"
26 // - avoid excessive deep copying (quo and rounders)
35 // A Dec represents a signed arbitrary-precision decimal.
36 // It is a combination of a sign, an arbitrary-precision integer coefficient
37 // value, and a signed fixed-precision exponent value.
38 // The sign and the coefficient value are handled together as a signed value
39 // and referred to as the unscaled value.
40 // (Positive and negative zero values are not distinguished.)
41 // Since the exponent is most commonly non-positive, it is handled in negated
42 // form and referred to as scale.
44 // The mathematical value of a Dec equals:
46 // unscaled * 10**(-scale)
48 // Note that different Dec representations may have equal mathematical values.
50 // unscaled scale String()
51 // -------------------------
60 // The zero value for a Dec represents the value 0 with scale 0.
62 // Operations are typically performed through the *Dec type.
63 // The semantics of the assignment operation "=" for "bare" Dec values is
64 // undefined and should not be relied on.
66 // Methods are typically of the form:
68 // func (z *Dec) Op(x, y *Dec) *Dec
70 // and implement operations z = x Op y with the result as receiver; if it
71 // is one of the operands it may be overwritten (and its memory reused).
72 // To enable chaining of operations, the result is also returned. Methods
73 // returning a result other than *Dec take one of the operands as the receiver.
75 // A "bare" Quo method (quotient / division operation) is not provided, as the
76 // result is not always a finite decimal and thus in general cannot be
77 // represented as a Dec.
78 // Instead, in the common case when rounding is (potentially) necessary,
79 // QuoRound should be used with a Scale and a Rounder.
80 // QuoExact or QuoRound with RoundExact can be used in the special cases when it
81 // is known that the result is always a finite decimal.
88 // Scale represents the type used for the scale of a Dec.
91 const scaleSize = 4 // bytes in a Scale value
93 // Scaler represents a method for obtaining the scale to use for the result of
94 // an operation on x and y.
95 type scaler interface {
96 Scale(x *Dec, y *Dec) Scale
99 var bigInt = [...]*big.Int{
100 big.NewInt(0), big.NewInt(1), big.NewInt(2), big.NewInt(3), big.NewInt(4),
101 big.NewInt(5), big.NewInt(6), big.NewInt(7), big.NewInt(8), big.NewInt(9),
105 var exp10cache [64]big.Int = func() [64]big.Int {
106 e10, e10i := [64]big.Int{}, bigInt[1]
109 e10i = new(big.Int).Mul(e10i, bigInt[10])
114 // NewDec allocates and returns a new Dec set to the given int64 unscaled value
116 func NewDec(unscaled int64, scale Scale) *Dec {
117 return new(Dec).SetUnscaled(unscaled).SetScale(scale)
120 // NewDecBig allocates and returns a new Dec set to the given *big.Int unscaled
122 func NewDecBig(unscaled *big.Int, scale Scale) *Dec {
123 return new(Dec).SetUnscaledBig(unscaled).SetScale(scale)
126 // Scale returns the scale of x.
127 func (x *Dec) Scale() Scale {
131 // Unscaled returns the unscaled value of x for u and true for ok when the
132 // unscaled value can be represented as int64; otherwise it returns an undefined
133 // int64 value for u and false for ok. Use x.UnscaledBig().Int64() to avoid
134 // checking the validity of the value when the check is known to be redundant.
135 func (x *Dec) Unscaled() (u int64, ok bool) {
136 u = x.unscaled.Int64()
138 ok = i.SetInt64(u).Cmp(&x.unscaled) == 0
142 // UnscaledBig returns the unscaled value of x as *big.Int.
143 func (x *Dec) UnscaledBig() *big.Int {
147 // SetScale sets the scale of z, with the unscaled value unchanged, and returns
149 // The mathematical value of the Dec changes as if it was multiplied by
150 // 10**(oldscale-scale).
151 func (z *Dec) SetScale(scale Scale) *Dec {
156 // SetUnscaled sets the unscaled value of z, with the scale unchanged, and
158 func (z *Dec) SetUnscaled(unscaled int64) *Dec {
159 z.unscaled.SetInt64(unscaled)
163 // SetUnscaledBig sets the unscaled value of z, with the scale unchanged, and
165 func (z *Dec) SetUnscaledBig(unscaled *big.Int) *Dec {
166 z.unscaled.Set(unscaled)
170 // Set sets z to the value of x and returns z.
171 // It does nothing if z == x.
172 func (z *Dec) Set(x *Dec) *Dec {
174 z.SetUnscaledBig(x.UnscaledBig())
175 z.SetScale(x.Scale())
186 func (x *Dec) Sign() int {
187 return x.UnscaledBig().Sign()
190 // Neg sets z to -x and returns z.
191 func (z *Dec) Neg(x *Dec) *Dec {
192 z.SetScale(x.Scale())
193 z.UnscaledBig().Neg(x.UnscaledBig())
197 // Cmp compares x and y and returns:
203 func (x *Dec) Cmp(y *Dec) int {
204 xx, yy := upscale(x, y)
205 return xx.UnscaledBig().Cmp(yy.UnscaledBig())
208 // Abs sets z to |x| (the absolute value of x) and returns z.
209 func (z *Dec) Abs(x *Dec) *Dec {
210 z.SetScale(x.Scale())
211 z.UnscaledBig().Abs(x.UnscaledBig())
215 // Add sets z to the sum x+y and returns z.
216 // The scale of z is the greater of the scales of x and y.
217 func (z *Dec) Add(x, y *Dec) *Dec {
218 xx, yy := upscale(x, y)
219 z.SetScale(xx.Scale())
220 z.UnscaledBig().Add(xx.UnscaledBig(), yy.UnscaledBig())
224 // Sub sets z to the difference x-y and returns z.
225 // The scale of z is the greater of the scales of x and y.
226 func (z *Dec) Sub(x, y *Dec) *Dec {
227 xx, yy := upscale(x, y)
228 z.SetScale(xx.Scale())
229 z.UnscaledBig().Sub(xx.UnscaledBig(), yy.UnscaledBig())
233 // Mul sets z to the product x*y and returns z.
234 // The scale of z is the sum of the scales of x and y.
235 func (z *Dec) Mul(x, y *Dec) *Dec {
236 z.SetScale(x.Scale() + y.Scale())
237 z.UnscaledBig().Mul(x.UnscaledBig(), y.UnscaledBig())
241 // Round sets z to the value of x rounded to Scale s using Rounder r, and
243 func (z *Dec) Round(x *Dec, s Scale, r Rounder) *Dec {
244 return z.QuoRound(x, NewDec(1, 0), s, r)
247 // QuoRound sets z to the quotient x/y, rounded using the given Rounder to the
250 // If the rounder is RoundExact but the result can not be expressed exactly at
251 // the specified scale, QuoRound returns nil, and the value of z is undefined.
253 // There is no corresponding Div method; the equivalent can be achieved through
254 // the choice of Rounder used.
256 func (z *Dec) QuoRound(x, y *Dec, s Scale, r Rounder) *Dec {
257 return z.quo(x, y, sclr{s}, r)
260 func (z *Dec) quo(x, y *Dec, s scaler, r Rounder) *Dec {
263 if r.UseRemainder() {
264 zz, rA, rB := new(Dec).quoRem(x, y, scl, true, new(big.Int), new(big.Int))
265 zzz = r.Round(new(Dec), zz, rA, rB)
267 zz, _, _ := new(Dec).quoRem(x, y, scl, false, nil, nil)
268 zzz = r.Round(new(Dec), zz, nil, nil)
276 // QuoExact sets z to the quotient x/y and returns z when x/y is a finite
277 // decimal. Otherwise it returns nil and the value of z is undefined.
279 // The scale of a non-nil result is "x.Scale() - y.Scale()" or greater; it is
280 // calculated so that the remainder will be zero whenever x/y is a finite
282 func (z *Dec) QuoExact(x, y *Dec) *Dec {
283 return z.quo(x, y, scaleQuoExact{}, RoundExact)
286 // quoRem sets z to the quotient x/y with the scale s, and if useRem is true,
287 // it sets remNum and remDen to the numerator and denominator of the remainder.
288 // It returns z, remNum and remDen.
290 // The remainder is normalized to the range -1 < r < 1 to simplify rounding;
291 // that is, the results satisfy the following equation:
293 // x / y = z + (remNum/remDen) * 10**(-z.Scale())
295 // See Rounder for more details about rounding.
297 func (z *Dec) quoRem(x, y *Dec, s Scale, useRem bool,
298 remNum, remDen *big.Int) (*Dec, *big.Int, *big.Int) {
299 // difference (required adjustment) compared to "canonical" result scale
300 shift := s - (x.Scale() - y.Scale())
301 // pointers to adjusted unscaled dividend and divisor
305 // increased scale: decimal-shift dividend left
306 ix = new(big.Int).Mul(x.UnscaledBig(), exp10(shift))
309 // decreased scale: decimal-shift divisor left
311 iy = new(big.Int).Mul(y.UnscaledBig(), exp10(-shift))
316 // save a copy of iy in case it to be overwritten with the result
318 if iy == z.UnscaledBig() {
319 iy2 = new(big.Int).Set(iy)
326 _, intr := z.UnscaledBig().QuoRem(ix, iy, new(big.Int))
331 z.UnscaledBig().Quo(ix, iy)
333 return z, remNum, remDen
336 type sclr struct{ s Scale }
338 func (s sclr) Scale(x, y *Dec) Scale {
342 type scaleQuoExact struct{}
344 func (sqe scaleQuoExact) Scale(x, y *Dec) Scale {
345 rem := new(big.Rat).SetFrac(x.UnscaledBig(), y.UnscaledBig())
346 f2, f5 := factor2(rem.Denom()), factor(rem.Denom(), bigInt[5])
353 return x.Scale() - y.Scale() + f10
356 func factor(n *big.Int, p *big.Int) int {
357 // could be improved for large factors
360 dd, dm := new(big.Int).DivMod(d, p, new(big.Int))
371 func factor2(n *big.Int) int {
372 // could be improved for large factors
374 for ; n.Bit(f) == 0; f++ {
379 func upscale(a, b *Dec) (*Dec, *Dec) {
380 if a.Scale() == b.Scale() {
383 if a.Scale() > b.Scale() {
384 bb := b.rescale(a.Scale())
387 aa := a.rescale(b.Scale())
391 func exp10(x Scale) *big.Int {
392 if int(x) < len(exp10cache) {
393 return &exp10cache[int(x)]
395 return new(big.Int).Exp(bigInt[10], big.NewInt(int64(x)), nil)
398 func (x *Dec) rescale(newScale Scale) *Dec {
399 shift := newScale - x.Scale()
403 return NewDecBig(new(big.Int).Quo(x.UnscaledBig(), e), newScale)
406 return NewDecBig(new(big.Int).Mul(x.UnscaledBig(), e), newScale)
411 var zeros = []byte("00000000000000000000000000000000" +
412 "00000000000000000000000000000000")
413 var lzeros = Scale(len(zeros))
415 func appendZeros(s []byte, n Scale) []byte {
416 for i := Scale(0); i < n; i += lzeros {
418 s = append(s, zeros...)
420 s = append(s, zeros[0:n-i]...)
426 func (x *Dec) String() string {
431 s := []byte(x.UnscaledBig().String())
433 if scale != 0 && x.unscaled.Sign() != 0 {
434 s = appendZeros(s, -scale)
438 negbit := Scale(-((x.Sign() - 1) / 2))
440 lens := Scale(len(s))
441 if lens-negbit <= scale {
442 ss := make([]byte, 0, scale+2)
446 ss = append(ss, '0', '.')
447 ss = appendZeros(ss, scale-lens+negbit)
448 ss = append(ss, s[negbit:]...)
452 ss := make([]byte, 0, lens+1)
453 ss = append(ss, s[:lens-scale]...)
455 ss = append(ss, s[lens-scale:]...)
459 // Format is a support routine for fmt.Formatter. It accepts the decimal
460 // formats 'd' and 'f', and handles both equivalently.
461 // Width, precision, flags and bases 2, 8, 16 are not supported.
462 func (x *Dec) Format(s fmt.State, ch rune) {
463 if ch != 'd' && ch != 'f' && ch != 'v' && ch != 's' {
464 fmt.Fprintf(s, "%%!%c(dec.Dec=%s)", ch, x.String())
467 fmt.Fprintf(s, x.String())
470 func (z *Dec) scan(r io.RuneScanner) (*Dec, error) {
471 unscaled := make([]byte, 0, 256) // collects chars of unscaled as bytes
472 dp, dg := -1, -1 // indexes of decimal point, first digit
475 ch, _, err := r.ReadRune()
483 case ch == '+' || ch == '-':
484 if len(unscaled) > 0 || dp >= 0 { // must be first character
494 continue // don't add to unscaled
495 case ch >= '0' && ch <= '9':
503 unscaled = append(unscaled, byte(ch))
506 return nil, fmt.Errorf("no digits read")
509 z.SetScale(Scale(len(unscaled) - dp))
513 _, ok := z.UnscaledBig().SetString(string(unscaled), 10)
515 return nil, fmt.Errorf("invalid decimal: %s", string(unscaled))
520 // SetString sets z to the value of s, interpreted as a decimal (base 10),
521 // and returns z and a boolean indicating success. The scale of z is the
522 // number of digits after the decimal point (including any trailing 0s),
523 // or 0 if there is no decimal point. If SetString fails, the value of z
524 // is undefined but the returned value is nil.
525 func (z *Dec) SetString(s string) (*Dec, bool) {
526 r := strings.NewReader(s)
531 _, _, err = r.ReadRune()
535 // err == io.EOF => scan consumed all of s
539 // Scan is a support routine for fmt.Scanner; it sets z to the value of
540 // the scanned number. It accepts the decimal formats 'd' and 'f', and
541 // handles both equivalently. Bases 2, 8, 16 are not supported.
542 // The scale of z is the number of digits after the decimal point
543 // (including any trailing 0s), or 0 if there is no decimal point.
544 func (z *Dec) Scan(s fmt.ScanState, ch rune) error {
545 if ch != 'd' && ch != 'f' && ch != 's' && ch != 'v' {
546 return fmt.Errorf("Dec.Scan: invalid verb '%c'", ch)
553 // Gob encoding version
554 const decGobVersion byte = 1
556 func scaleBytes(s Scale) []byte {
557 buf := make([]byte, scaleSize)
559 for j := 0; j < scaleSize; j++ {
567 func scale(b []byte) (s Scale) {
568 for j := 0; j < scaleSize; j++ {
575 // GobEncode implements the gob.GobEncoder interface.
576 func (x *Dec) GobEncode() ([]byte, error) {
577 buf, err := x.UnscaledBig().GobEncode()
581 buf = append(append(buf, scaleBytes(x.Scale())...), decGobVersion)
585 // GobDecode implements the gob.GobDecoder interface.
586 func (z *Dec) GobDecode(buf []byte) error {
588 return fmt.Errorf("Dec.GobDecode: no data")
591 if b != decGobVersion {
592 return fmt.Errorf("Dec.GobDecode: encoding version %d not supported", b)
594 l := len(buf) - scaleSize - 1
595 err := z.UnscaledBig().GobDecode(buf[:l])
599 z.SetScale(scale(buf[l : l+scaleSize]))
603 // MarshalText implements the encoding.TextMarshaler interface.
604 func (x *Dec) MarshalText() ([]byte, error) {
605 return []byte(x.String()), nil
608 // UnmarshalText implements the encoding.TextUnmarshaler interface.
609 func (z *Dec) UnmarshalText(data []byte) error {
610 _, ok := z.SetString(string(data))
612 return fmt.Errorf("invalid inf.Dec")