dec

package
v0.0.0-...-a4b336c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 24, 2016 License: BSD-2-Clause Imports: 4 Imported by: 0

Documentation

Overview

Package dec implements multi-precision decimal arithmetic. It supports the numeric type Dec for signed decimals. It is based on and complements the multi-precision integer implementation (Int) in the Go library (math/big).

Methods are typically of the form:

func (z *Dec) Op(x, y *Dec) *Dec

and implement operations z = x Op y with the result as receiver; if it is one of the operands it may be overwritten (and its memory reused). To enable chaining of operations, the result is also returned. Methods returning a result other than *Dec take one of the operands as the receiver.

Quotient (division) operation uses Scalers and Rounders to specify the desired behavior. See Quo, Scaler, and Rounder for details.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Dec

type Dec struct {
	// contains filtered or unexported fields
}

A Dec represents a signed multi-precision decimal. It is stored as a combination of a multi-precision big.Int unscaled value and a fixed-precision scale of type Scale.

The mathematical value of a Dec equals:

unscaled * 10**(-scale)

Note that different Dec representations may have equal mathematical values.

unscaled  scale  String()
-------------------------
       0      0    "0"
       0      2    "0.00"
       0     -2    "0"
       1      0    "1"
     100      2    "1.00"
      10      0   "10"
       1     -1   "10"

The zero value for a Dec represents the value 0 with scale 0.

func NewDec

func NewDec(unscaled *big.Int, scale Scale) *Dec

NewDec allocates and returns a new Dec set to the given unscaled value and scale.

func NewDecInt64

func NewDecInt64(x int64) *Dec

NewDecInt64 allocates and returns a new Dec set to the given int64 value with scale 0.

func (*Dec) Abs

func (z *Dec) Abs(x *Dec) *Dec

Abs sets z to |x| (the absolute value of x) and returns z.

func (*Dec) Add

func (z *Dec) Add(x, y *Dec) *Dec

Add sets z to the sum x+y and returns z. The scale of z is the greater of the scales of x and y.

func (*Dec) Cmp

func (x *Dec) Cmp(y *Dec) int

Cmp compares x and y and returns:

-1 if x <  y
 0 if x == y
+1 if x >  y

func (*Dec) Format

func (x *Dec) Format(s fmt.State, ch rune)

Format is a support routine for fmt.Formatter. It accepts the decimal formats 'd' and 'f', and handles both equivalently. Width, precision, flags and bases 2, 8, 16 are not supported.

func (*Dec) GobDecode

func (z *Dec) GobDecode(buf []byte) error

GobDecode implements the gob.GobDecoder interface.

func (*Dec) GobEncode

func (x *Dec) GobEncode() ([]byte, error)

GobEncode implements the gob.GobEncoder interface.

func (*Dec) Mul

func (z *Dec) Mul(x, y *Dec) *Dec

Mul sets z to the product x*y and returns z. The scale of z is the sum of the scales of x and y.

func (*Dec) Neg

func (z *Dec) Neg(x *Dec) *Dec

Neg sets z to -x and returns z.

func (*Dec) Quo

func (z *Dec) Quo(x, y *Dec, scaler Scaler, rounder Rounder) *Dec

Quo sets z to the quotient x/y, with the scale obtained from the given Scaler, rounded using the given Rounder. If the result from the rounder is nil, Quo also returns nil, and the value of z is undefined.

There is no corresponding Div method; the equivalent can be achieved through the choice of Rounder used.

See Rounder for details on the various ways for rounding.

func (*Dec) QuoExact

func (z *Dec) QuoExact(x, y *Dec) *Dec

QuoExact(x, y) is a shorthand for Quo(x, y, ScaleQuoExact, RoundExact). If x/y can be expressed as a Dec without rounding, QuoExact sets z to the quotient x/y and returns z. Otherwise, it returns nil and the value of z is undefined.

func (*Dec) Round

func (z *Dec) Round(x *Dec, s Scale, r Rounder) *Dec

Round sets z to the value of x rounded to Scale s using Rounder r, and returns z.

func (*Dec) Scale

func (x *Dec) Scale() Scale

Scale returns the scale of x.

func (*Dec) Scan

func (z *Dec) Scan(s fmt.ScanState, ch rune) error

Scan is a support routine for fmt.Scanner; it sets z to the value of the scanned number. It accepts the decimal formats 'd' and 'f', and handles both equivalently. Bases 2, 8, 16 are not supported. The scale of z is the number of digits after the decimal point (including any trailing 0s), or 0 if there is no decimal point.

func (*Dec) Set

func (z *Dec) Set(x *Dec) *Dec

Set sets z to the value of x and returns z. It does nothing if z == x.

func (*Dec) SetScale

func (x *Dec) SetScale(scale Scale) *Dec

SetScale sets the scale of x, with the unscaled value unchanged. The mathematical value of the Dec changes as if it was multiplied by 10**(oldscale-scale).

func (*Dec) SetString

func (z *Dec) SetString(s string) (*Dec, bool)

SetString sets z to the value of s, interpreted as a decimal (base 10), and returns z and a boolean indicating success. The scale of z is the number of digits after the decimal point (including any trailing 0s), or 0 if there is no decimal point. If SetString fails, the value of z is undefined but the returned value is nil.

func (*Dec) SetUnscaled

func (x *Dec) SetUnscaled(unscaled *big.Int) *Dec

SetScale sets the unscaled value of x, with the scale unchanged.

func (*Dec) Sign

func (x *Dec) Sign() int

Sign returns:

-1 if x <  0
 0 if x == 0
+1 if x >  0

func (*Dec) String

func (x *Dec) String() string

func (*Dec) Sub

func (z *Dec) Sub(x, y *Dec) *Dec

Sub sets z to the difference x-y and returns z. The scale of z is the greater of the scales of x and y.

func (*Dec) Unscaled

func (x *Dec) Unscaled() *big.Int

Unscaled returns the unscaled value of x.

type Rounder

type Rounder interface {

	// When UseRemainder() returns true, the Round() method is passed the
	// remainder of the division, expressed as the numerator and denominator of
	// a rational.
	UseRemainder() bool

	// Round sets the rounded value of a quotient to z, and returns z.
	// quo is rounded down (truncated towards zero) to the scale obtained from
	// the Scaler in Quo().
	//
	// When the remainder is not used, remNum and remDen are nil.
	// When used, the remainder is normalized between -1 and 1; that is:
	//
	//  -|remDen| < remNum < |remDen|
	//
	// remDen has the same sign as y, and remNum is zero or has the same sign
	// as x.
	Round(z, quo *Dec, remNum, remDen *big.Int) *Dec
}

Rounder represents a method for rounding the (possibly infinite decimal) result of a division to a finite Dec. It is used by Dec.Round() and Dec.Quo().

var RoundCeil Rounder = roundCeil

RoundCeil rounds towards positive infinity; that is, returns the smallest Dec not smaller than the result represented by quo and rem.

The following table shows examples of the results for Quo(x, y, Scale(scale), RoundCeil).

    x      y    scale   result
------------------------------
  -1.8    10        1     -0.1
  -1.5    10        1     -0.1
  -1.2    10        1     -0.1
  -1.0    10        1     -0.1
  -0.8    10        1     -0.0
  -0.5    10        1     -0.0
  -0.2    10        1     -0.0
   0.0    10        1      0.0
   0.2    10        1      0.1
   0.5    10        1      0.1
   0.8    10        1      0.1
   1.0    10        1      0.1
   1.2    10        1      0.2
   1.5    10        1      0.2
   1.8    10        1      0.2
var RoundDown Rounder = roundDown

RoundDown rounds towards 0; that is, returns the Dec with the greatest absolute value not exceeding that of the result represented by quo and rem.

The following table shows examples of the results for Quo(x, y, Scale(scale), RoundDown).

    x      y    scale   result
------------------------------
  -1.8    10        1     -0.1
  -1.5    10        1     -0.1
  -1.2    10        1     -0.1
  -1.0    10        1     -0.1
  -0.8    10        1     -0.0
  -0.5    10        1     -0.0
  -0.2    10        1     -0.0
   0.0    10        1      0.0
   0.2    10        1      0.0
   0.5    10        1      0.0
   0.8    10        1      0.0
   1.0    10        1      0.1
   1.2    10        1      0.1
   1.5    10        1      0.1
   1.8    10        1      0.1
var RoundExact Rounder = roundExact

RoundExact returns quo if rem is zero, or nil otherwise. It is intended to be used with ScaleQuoExact when it is guaranteed that the result can be obtained without rounding. QuoExact is a shorthand for such a quotient operation.

var RoundFloor Rounder = roundFloor

RoundFloor rounds towards negative infinity; that is, returns the greatest Dec not exceeding the result represented by quo and rem.

The following table shows examples of the results for Quo(x, y, Scale(scale), RoundFloor).

    x      y    scale   result
------------------------------
  -1.8    10        1     -0.2
  -1.5    10        1     -0.2
  -1.2    10        1     -0.2
  -1.0    10        1     -0.1
  -0.8    10        1     -0.1
  -0.5    10        1     -0.1
  -0.2    10        1     -0.1
   0.0    10        1      0.0
   0.2    10        1      0.0
   0.5    10        1      0.0
   0.8    10        1      0.0
   1.0    10        1      0.1
   1.2    10        1      0.1
   1.5    10        1      0.1
   1.8    10        1      0.1
var RoundHalfDown Rounder = roundHalfDown

RoundHalfDown rounds to the nearest Dec, and when the remainder is 1/2, it rounds to the Dec with the lower absolute value.

The following table shows examples of the results for Quo(x, y, Scale(scale), RoundHalfDown).

    x      y    scale   result
------------------------------
  -1.8    10        1     -0.2
  -1.5    10        1     -0.1
  -1.2    10        1     -0.1
  -1.0    10        1     -0.1
  -0.8    10        1     -0.1
  -0.5    10        1     -0.0
  -0.2    10        1     -0.0
   0.0    10        1      0.0
   0.2    10        1      0.0
   0.5    10        1      0.0
   0.8    10        1      0.1
   1.0    10        1      0.1
   1.2    10        1      0.1
   1.5    10        1      0.1
   1.8    10        1      0.2
var RoundHalfEven Rounder = roundHalfEven

RoundHalfEven rounds to the nearest Dec, and when the remainder is 1/2, it rounds to the Dec with even last digit.

The following table shows examples of the results for Quo(x, y, Scale(scale), RoundHalfEven).

    x      y    scale   result
------------------------------
  -1.8    10        1     -0.2
  -1.5    10        1     -0.2
  -1.2    10        1     -0.1
  -1.0    10        1     -0.1
  -0.8    10        1     -0.1
  -0.5    10        1     -0.0
  -0.2    10        1     -0.0
   0.0    10        1      0.0
   0.2    10        1      0.0
   0.5    10        1      0.0
   0.8    10        1      0.1
   1.0    10        1      0.1
   1.2    10        1      0.1
   1.5    10        1      0.2
   1.8    10        1      0.2
var RoundHalfUp Rounder = roundHalfUp

RoundHalfUp rounds to the nearest Dec, and when the remainder is 1/2, it rounds to the Dec with the greater absolute value.

The following table shows examples of the results for Quo(x, y, Scale(scale), RoundHalfUp).

    x      y    scale   result
------------------------------
  -1.8    10        1     -0.2
  -1.5    10        1     -0.2
  -1.2    10        1     -0.1
  -1.0    10        1     -0.1
  -0.8    10        1     -0.1
  -0.5    10        1     -0.1
  -0.2    10        1     -0.0
   0.0    10        1      0.0
   0.2    10        1      0.0
   0.5    10        1      0.1
   0.8    10        1      0.1
   1.0    10        1      0.1
   1.2    10        1      0.1
   1.5    10        1      0.2
   1.8    10        1      0.2
var RoundUp Rounder = roundUp

RoundUp rounds away from 0; that is, returns the Dec with the smallest absolute value not smaller than that of the result represented by quo and rem.

The following table shows examples of the results for Quo(x, y, Scale(scale), RoundUp).

    x      y    scale   result
------------------------------
  -1.8    10        1     -0.2
  -1.5    10        1     -0.2
  -1.2    10        1     -0.2
  -1.0    10        1     -0.1
  -0.8    10        1     -0.1
  -0.5    10        1     -0.1
  -0.2    10        1     -0.1
   0.0    10        1      0.0
   0.2    10        1      0.1
   0.5    10        1      0.1
   0.8    10        1      0.1
   1.0    10        1      0.1
   1.2    10        1      0.2
   1.5    10        1      0.2
   1.8    10        1      0.2

type Scale

type Scale int32

Scale represents the type used for the scale of a Dec.

func (Scale) Scale

func (s Scale) Scale(x *Dec, y *Dec) Scale

Scale() for a Scale value always returns the Scale value. This allows a Scale value to be used as a Scaler when the desired scale is independent of the values x and y.

type Scaler

type Scaler interface {
	Scale(x *Dec, y *Dec) Scale
}

Scaler represents a method for obtaining the scale to use for the result of an operation on x and y.

var ScaleQuoExact Scaler = scaleQuoExact{}

ScaleQuoExact is the Scaler used by QuoExact. It returns a scale that is greater than or equal to "x.Scale() - y.Scale()"; it is calculated so that the remainder will be zero whenever x/y is a finite decimal.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL