apd: github.com/cockroachdb/apd Index | Examples | Files

package apd

import "github.com/cockroachdb/apd"

Package apd implements arbitrary-precision decimals.

apd implements much of the decimal specification from the General Decimal Arithmetic (http://speleotrove.com/decimal/) description. This is the same specification implemented by python’s decimal module (https://docs.python.org/2/library/decimal.html) and GCC’s decimal extension.

Features

Panic-free operation. The math/big types don’t return errors, and instead panic under some conditions that are documented. This requires users to validate the inputs before using them. Meanwhile, we’d like our decimal operations to have more failure modes and more input requirements than the math/big types, so using that API would be difficult. apd instead returns errors when needed.

Support for standard functions. sqrt, ln, pow, etc.

Accurate and configurable precision. Operations will use enough internal precision to produce a correct result at the requested precision. Precision is set by a "context" structure that accompanies the function arguments, as discussed in the next section.

Good performance. Operations will either be fast enough or will produce an error if they will be slow. This prevents edge-case operations from consuming lots of CPU or memory.

Condition flags and traps. All operations will report whether their result is exact, is rounded, is over- or under-flowed, is subnormal (https://en.wikipedia.org/wiki/Denormal_number), or is some other condition. apd supports traps which will trigger an error on any of these conditions. This makes it possible to guarantee exactness in computations, if needed.

Usage

apd has two main types. The first is Decimal which holds the values of decimals. It is simple and uses a big.Int with an exponent to describe values. Most operations on Decimals can’t produce errors as they work directly on the underlying big.Int. Notably, however, there are no arithmetic operations on Decimals.

The second main type is Context, which is where all arithmetic operations are defined. A Context describes the precision, range, and some other restrictions during operations. These operations can all produce failures, and so return errors.

Context operations, in addition to errors, return a Condition, which is a bitfield of flags that occurred during an operation. These include overflow, underflow, inexact, rounded, and others. The Traps field of a Context can be set which will produce an error if the corresponding flag occurs. An example of this is given below.

Index

Examples

Package Files

condition.go const.go context.go decimal.go doc.go error.go form_string.go loop.go round.go table.go

Constants

const (

    // MaxExponent is the highest exponent supported. Exponents near this range will
    // perform very slowly (many seconds per operation).
    MaxExponent = 100000
    // MinExponent is the lowest exponent supported with the same limitations as
    // MaxExponent.
    MinExponent = -MaxExponent
)
const (
    // RoundDown rounds toward 0; truncate.
    RoundDown = "down"
    // RoundHalfUp rounds up if the digits are >= 0.5.
    RoundHalfUp = "half_up"
    // RoundHalfEven rounds up if the digits are > 0.5. If the digits are equal
    // to 0.5, it rounds up if the previous digit is odd, always producing an
    // even digit.
    RoundHalfEven = "half_even"
    // RoundCeiling towards +Inf: rounds up if digits are > 0 and the number
    // is positive.
    RoundCeiling = "ceiling"
    // RoundFloor towards -Inf: rounds up if digits are > 0 and the number
    // is negative.
    RoundFloor = "floor"
    // RoundHalfDown rounds up if the digits are > 0.5.
    RoundHalfDown = "half_down"
    // RoundUp rounds away from 0.
    RoundUp = "up"
    // Round05Up rounds zero or five away from 0; same as round-up, except that
    // rounding up only occurs if the digit to be rounded up is 0 or 5.
    Round05Up = "05up"
)
const (
    // DefaultTraps is the default trap set used by BaseContext.
    DefaultTraps = SystemOverflow |
        SystemUnderflow |
        Overflow |
        Underflow |
        Subnormal |
        DivisionUndefined |
        DivisionByZero |
        DivisionImpossible |
        InvalidOperation
)

Variables

var BaseContext = Context{

    Precision: 0,

    MaxExponent: MaxExponent,
    MinExponent: MinExponent,

    Traps: DefaultTraps,
}

BaseContext is a useful default Context. Should not be mutated.

var (
    // Roundings defines the set of Rounders used by Context. Users may add their
    // own, but modification of this map is not safe during any other parallel
    // Context operations.
    Roundings = map[string]Rounder{
        RoundDown:     roundDown,
        RoundHalfUp:   roundHalfUp,
        RoundHalfEven: roundHalfEven,
        RoundCeiling:  roundCeiling,
        RoundFloor:    roundFloor,
        RoundHalfDown: roundHalfDown,
        RoundUp:       roundUp,
        Round05Up:     round05Up,
    }
)

func NumDigits Uses

func NumDigits(b *big.Int) int64

NumDigits returns the number of decimal digits of b.

type Condition Uses

type Condition uint32

Condition holds condition flags.

const (
    // SystemOverflow is raised when an exponent is greater than MaxExponent.
    SystemOverflow Condition = 1 << iota
    // SystemUnderflow is raised when an exponent is less than MinExponent.
    SystemUnderflow
    // Overflow is raised when the exponent of a result is too large to be
    // represented.
    Overflow
    // Underflow is raised when a result is both subnormal and inexact.
    Underflow
    // Inexact is raised when a result is not exact (one or more non-zero
    // coefficient digits were discarded during rounding).
    Inexact
    // Subnormal is raised when a result is subnormal (its adjusted exponent is
    // less than Emin), before any rounding.
    Subnormal
    // Rounded is raised when a result has been rounded (that is, some zero or
    // non-zero coefficient digits were discarded).
    Rounded
    // DivisionUndefined is raised when both division operands are 0.
    DivisionUndefined
    // DivisionByZero is raised when a non-zero dividend is divided by zero.
    DivisionByZero
    // DivisionImpossible is raised when integer division cannot be exactly
    // represented with the given precision.
    DivisionImpossible
    // InvalidOperation is raised when a result would be undefined or impossible.
    InvalidOperation
    // Clamped is raised when the exponent of a result has been altered or
    // constrained in order to fit the constraints of the Decimal representation.
    Clamped
)

func (Condition) Any Uses

func (r Condition) Any() bool

Any returns true if any flag is true.

func (Condition) Clamped Uses

func (r Condition) Clamped() bool

Clamped returns true if the Clamped flag is set.

func (Condition) DivisionByZero Uses

func (r Condition) DivisionByZero() bool

DivisionByZero returns true if the DivisionByZero flag is set.

func (Condition) DivisionImpossible Uses

func (r Condition) DivisionImpossible() bool

DivisionImpossible returns true if the DivisionImpossible flag is set.

func (Condition) DivisionUndefined Uses

func (r Condition) DivisionUndefined() bool

DivisionUndefined returns true if the DivisionUndefined flag is set.

func (Condition) GoError Uses

func (r Condition) GoError(traps Condition) (Condition, error)

GoError converts r to an error based on the given traps and returns r. Traps are the conditions which will trigger an error result if the corresponding Flag condition occurred.

func (Condition) Inexact Uses

func (r Condition) Inexact() bool

Inexact returns true if the Inexact flag is set.

func (Condition) InvalidOperation Uses

func (r Condition) InvalidOperation() bool

InvalidOperation returns true if the InvalidOperation flag is set.

func (Condition) Overflow Uses

func (r Condition) Overflow() bool

Overflow returns true if the Overflow flag is set.

func (Condition) Rounded Uses

func (r Condition) Rounded() bool

Rounded returns true if the Rounded flag is set.

func (Condition) String Uses

func (r Condition) String() string

func (Condition) Subnormal Uses

func (r Condition) Subnormal() bool

Subnormal returns true if the Subnormal flag is set.

func (Condition) SystemOverflow Uses

func (r Condition) SystemOverflow() bool

SystemOverflow returns true if the SystemOverflow flag is set.

func (Condition) SystemUnderflow Uses

func (r Condition) SystemUnderflow() bool

SystemUnderflow returns true if the SystemUnderflow flag is set.

func (Condition) Underflow Uses

func (r Condition) Underflow() bool

Underflow returns true if the Underflow flag is set.

type Context Uses

type Context struct {
    // Precision is the number of places to round during rounding; this is
    // effectively the total number of digits (before and after the decimal
    // point).
    Precision uint32
    // MaxExponent specifies the largest effective exponent. The
    // effective exponent is the value of the Decimal in scientific notation. That
    // is, for 10e2, the effective exponent is 3 (1.0e3). Zero (0) is not a special
    // value; it does not disable this check.
    MaxExponent int32
    // MinExponent is similar to MaxExponent, but for the smallest effective
    // exponent.
    MinExponent int32
    // Traps are the conditions which will trigger an error result if the
    // corresponding Flag condition occurred.
    Traps Condition
    // Rounding specifies the Rounder to use during rounding. RoundHalfUp is used if
    // empty or not present in Roundings.
    Rounding string
}

Context maintains options for Decimal operations. It can safely be used concurrently, but not modified concurrently. Arguments for any method can safely be used as both result and operand.

ExampleInexact demonstrates how to detect inexact operations.

Code:

d := apd.New(27, 0)
three := apd.New(3, 0)
c := apd.BaseContext.WithPrecision(5)
for {
    res, err := c.Quo(d, d, three)
    fmt.Printf("d: %7s, inexact: %5v, err: %v\n", d, res.Inexact(), err)
    if err != nil {
        return
    }
    if res.Inexact() {
        return
    }
}

Output:

d:       9, inexact: false, err: <nil>
d:       3, inexact: false, err: <nil>
d:       1, inexact: false, err: <nil>
d: 0.33333, inexact:  true, err: <nil>

ExampleOverflow demonstrates how to detect or error on overflow.

Code:

// Create a context that will overflow at 1e3.
c := apd.Context{
    MaxExponent: 2,
    Traps:       apd.Overflow,
}
one := apd.New(1, 0)
d := apd.New(997, 0)
for {
    res, err := c.Add(d, d, one)
    fmt.Printf("d: %8s, overflow: %5v, err: %v\n", d, res.Overflow(), err)
    if err != nil {
        return
    }
}

Output:

d:      998, overflow: false, err: <nil>
d:      999, overflow: false, err: <nil>
d: Infinity, overflow:  true, err: overflow

func (*Context) Abs Uses

func (c *Context) Abs(d, x *Decimal) (Condition, error)

Abs sets d to |x| (the absolute value of x).

func (*Context) Add Uses

func (c *Context) Add(d, x, y *Decimal) (Condition, error)

Add sets d to the sum x+y.

func (*Context) Cbrt Uses

func (c *Context) Cbrt(d, x *Decimal) (Condition, error)

Cbrt sets d to the cube root of x.

func (*Context) Ceil Uses

func (c *Context) Ceil(d, x *Decimal) (Condition, error)

Ceil sets d to the smallest integer >= x.

func (*Context) Cmp Uses

func (c *Context) Cmp(d, x, y *Decimal) (Condition, error)

Cmp compares x and y and sets d to:

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

This comparison respects the normal rules of special values (like NaN), and does not compare them.

func (*Context) Exp Uses

func (c *Context) Exp(d, x *Decimal) (Condition, error)

Exp sets d = e**x.

func (*Context) Floor Uses

func (c *Context) Floor(d, x *Decimal) (Condition, error)

Floor sets d to the largest integer <= x.

func (*Context) Ln Uses

func (c *Context) Ln(d, x *Decimal) (Condition, error)

Ln sets d to the natural log of x.

func (*Context) Log10 Uses

func (c *Context) Log10(d, x *Decimal) (Condition, error)

Log10 sets d to the base 10 log of x.

func (*Context) Mul Uses

func (c *Context) Mul(d, x, y *Decimal) (Condition, error)

Mul sets d to the product x*y.

func (*Context) Neg Uses

func (c *Context) Neg(d, x *Decimal) (Condition, error)

Neg sets d to -x.

func (*Context) NewFromString Uses

func (c *Context) NewFromString(s string) (*Decimal, Condition, error)

NewFromString creates a new decimal from s. The returned Decimal has its exponents restricted by the context and its value rounded if it contains more digits than the context's precision.

func (*Context) Pow Uses

func (c *Context) Pow(d, x, y *Decimal) (Condition, error)

Pow sets d = x**y.

func (*Context) Quantize Uses

func (c *Context) Quantize(d, x *Decimal, exp int32) (Condition, error)

Quantize adjusts and rounds x as necessary so it is represented with exponent exp and stores the result in d.

Code:

input, _, _ := apd.NewFromString("123.45")
output := new(apd.Decimal)
c := apd.BaseContext.WithPrecision(10)
for i := int32(-3); i <= 3; i++ {
    res, _ := c.Quantize(output, input, i)
    fmt.Printf("%2v: %s", i, output)
    if res != 0 {
        fmt.Printf(" (%s)", res)
    }
    fmt.Println()
}

Output:

-3: 123.450
-2: 123.45
-1: 123.5 (inexact, rounded)
 0: 123 (inexact, rounded)
 1: 1.2E+2 (inexact, rounded)
 2: 1E+2 (inexact, rounded)
 3: 0E+3 (inexact, rounded)

func (*Context) Quo Uses

func (c *Context) Quo(d, x, y *Decimal) (Condition, error)

Quo sets d to the quotient x/y for y != 0. c.Precision must be > 0. If an exact division is required, use a context with high precision and verify it was exact by checking the Inexact flag on the return Condition.

func (*Context) QuoInteger Uses

func (c *Context) QuoInteger(d, x, y *Decimal) (Condition, error)

QuoInteger sets d to the integer part of the quotient x/y. If the result cannot fit in d.Precision digits, an error is returned.

func (*Context) Reduce Uses

func (c *Context) Reduce(d, x *Decimal) (int, Condition, error)

Reduce sets d to x with all trailing zeros removed and returns the number of zeros removed.

func (*Context) Rem Uses

func (c *Context) Rem(d, x, y *Decimal) (Condition, error)

Rem sets d to the remainder part of the quotient x/y. If the integer part cannot fit in d.Precision digits, an error is returned.

func (*Context) Round Uses

func (c *Context) Round(d, x *Decimal) (Condition, error)

Round sets d to rounded x, rounded to the precision specified by c. If c has zero precision, no rounding will occur. If c has no Rounding specified, RoundHalfUp is used.

func (*Context) SetString Uses

func (c *Context) SetString(d *Decimal, s string) (*Decimal, Condition, error)

SetString sets d to s and returns d. The returned Decimal has its exponents restricted by the context and its value rounded if it contains more digits than the context's precision.

func (*Context) Sqrt Uses

func (c *Context) Sqrt(d, x *Decimal) (Condition, error)

Sqrt sets d to the square root of x. Sqrt uses the Babylonian method for computing the square root, which uses O(log p) steps for p digits of precision.

func (*Context) Sub Uses

func (c *Context) Sub(d, x, y *Decimal) (Condition, error)

Sub sets d to the difference x-y.

func (*Context) ToIntegral Uses

func (c *Context) ToIntegral(d, x *Decimal) (Condition, error)

ToIntegral sets d to integral value of x. Inexact and Rounded flags are ignored and removed.

func (*Context) ToIntegralX Uses

func (c *Context) ToIntegralX(d, x *Decimal) (Condition, error)

ToIntegralX sets d to integral value of x.

ExampleToIntegralX demonstrates how to use ToIntegralX to check if a number is an integer or not. Note the variations between integer (which allows zeros after the decimal point) and strict (which does not). See the documentation on Inexact and Rounded.

Code:

inputs := []string{
    "123.4",
    "123.0",
    "123",
    "12E1",
    "120E-1",
    "120E-2",
}
for _, input := range inputs {
    d, _, _ := apd.NewFromString(input)
    res, _ := apd.BaseContext.ToIntegralX(d, d)
    integer := !res.Inexact()
    strict := !res.Rounded()
    fmt.Printf("input: % 6s, ToIntegralX: % 3s, integer: % 5t, strict: % 5t, res:", input, d, integer, strict)
    if res != 0 {
        fmt.Printf(" %s", res)
    }
    fmt.Println()
}

Output:

input:  123.4, ToIntegralX: 123, integer: false, strict: false, res: inexact, rounded
input:  123.0, ToIntegralX: 123, integer:  true, strict: false, res: rounded
input:    123, ToIntegralX: 123, integer:  true, strict:  true, res:
input:   12E1, ToIntegralX: 120, integer:  true, strict:  true, res:
input: 120E-1, ToIntegralX:  12, integer:  true, strict: false, res: rounded
input: 120E-2, ToIntegralX:   1, integer: false, strict: false, res: inexact, rounded

func (*Context) WithPrecision Uses

func (c *Context) WithPrecision(p uint32) *Context

WithPrecision returns a copy of c but with the specified precision.

type Decimal Uses

type Decimal struct {
    Form     Form
    Negative bool
    Exponent int32
    Coeff    big.Int
}

Decimal is an arbitrary-precision decimal. Its value is:

Negative × Coeff × 10**Exponent

Coeff must be positive. If it is negative results may be incorrect and apd may panic.

func New Uses

func New(coeff int64, exponent int32) *Decimal

New creates a new decimal with the given coefficient and exponent.

func NewFromString Uses

func NewFromString(s string) (*Decimal, Condition, error)

NewFromString creates a new decimal from s. It has no restrictions on exponents or precision.

func NewWithBigInt Uses

func NewWithBigInt(coeff *big.Int, exponent int32) *Decimal

NewWithBigInt creates a new decimal with the given coefficient and exponent.

func (*Decimal) Abs Uses

func (d *Decimal) Abs(x *Decimal) *Decimal

Abs sets d to |x| and returns d.

func (*Decimal) Cmp Uses

func (d *Decimal) Cmp(x *Decimal) int

Cmp compares d and x and returns:

-1 if d <  x
 0 if d == x
+1 if d >  x
undefined if d or x are NaN

func (*Decimal) CmpTotal Uses

func (d *Decimal) CmpTotal(x *Decimal) int

CmpTotal compares d and x and returns:

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

This comparison uses a total ordering that is defined to compare all finite and non-finite (special) values.

For example, the following values are ordered from lowest to highest:

-NaN
-NaNSignaling
-Infinity
-127
-1.00
-1
-0.000
-0
0
1.2300
1.23
1E+9
Infinity
NaNSignaling
NaN

func (*Decimal) Float64 Uses

func (d *Decimal) Float64() (float64, error)

Float64 returns the float64 representation of x. This conversion may lose data (see strconv.ParseFloat for caveats).

func (*Decimal) Int64 Uses

func (d *Decimal) Int64() (int64, error)

Int64 returns the int64 representation of x. If x cannot be represented in an int64, an error is returned.

func (*Decimal) IsZero Uses

func (d *Decimal) IsZero() bool

IsZero returns true if d == 0 or -0.

func (*Decimal) Modf Uses

func (d *Decimal) Modf(integ, frac *Decimal)

Modf sets integ to the integral part of d and frac to the fractional part such that d = integ+frac. If d is negative, both integ or frac will be either 0 or negative. integ.Exponent will be >= 0; frac.Exponent will be <= 0.

func (*Decimal) Neg Uses

func (d *Decimal) Neg(x *Decimal) *Decimal

Neg sets d to -x and returns d.

func (*Decimal) NumDigits Uses

func (d *Decimal) NumDigits() int64

NumDigits returns the number of decimal digits of d.Coeff.

func (*Decimal) Reduce Uses

func (d *Decimal) Reduce(x *Decimal) (*Decimal, int)

Reduce sets d to x with all trailing zeros removed and returns d and the number of zeros removed.

func (*Decimal) Set Uses

func (d *Decimal) Set(x *Decimal) *Decimal

Set sets d's fields to the values of x and returns d.

func (*Decimal) SetCoefficient Uses

func (d *Decimal) SetCoefficient(x int64) *Decimal

SetCoefficient sets d's coefficient and negative value to x, its Form to Finite, and returns d. The exponent is not changed.

func (*Decimal) SetExponent Uses

func (d *Decimal) SetExponent(x int32) *Decimal

SetExponent sets d's Exponent value to x and returns d.

func (*Decimal) SetFloat64 Uses

func (d *Decimal) SetFloat64(f float64) (*Decimal, error)

SetFloat64 sets d's Coefficient and Exponent to x and returns d. d will hold the exact value of f.

func (*Decimal) SetInt64 Uses

func (d *Decimal) SetInt64(x int64) *Decimal

SetInt64 sets d to x and returns d.

func (*Decimal) SetString Uses

func (d *Decimal) SetString(s string) (*Decimal, Condition, error)

SetString sets d to s and returns d. It has no restrictions on exponents or precision.

func (*Decimal) Sign Uses

func (d *Decimal) Sign() int

Sign returns, if d is Finite:

-1 if d <  0
 0 if d == 0 or -0
+1 if d >  0

Otherwise (if d is Infinite or NaN):

-1 if d.Negative == true
+1 if d.Negative == false

func (*Decimal) String Uses

func (d *Decimal) String() string

String is a wrapper of ToSci.

func (*Decimal) ToSci Uses

func (d *Decimal) ToSci() string

ToSci returns d in scientific notation if an exponent is needed.

func (*Decimal) ToStandard Uses

func (d *Decimal) ToStandard() string

ToStandard converts d to a standard notation string (i.e., no exponent part). This can result in long strings given large exponents.

type ErrDecimal Uses

type ErrDecimal struct {
    Ctx *Context
    // Flags are the accumulated flags from operations.
    Flags Condition
    // contains filtered or unexported fields
}

ErrDecimal performs operations on decimals and collects errors during operations. If an error is already set, the operation is skipped. Designed to be used for many operations in a row, with a single error check at the end.

Code:

c := apd.BaseContext.WithPrecision(5)
ed := apd.MakeErrDecimal(c)
d := apd.New(10, 0)
fmt.Printf("%s, err: %v\n", d, ed.Err())
ed.Add(d, d, apd.New(2, 1)) // add 20
fmt.Printf("%s, err: %v\n", d, ed.Err())
ed.Quo(d, d, apd.New(0, 0)) // divide by zero
fmt.Printf("%s, err: %v\n", d, ed.Err())
ed.Sub(d, d, apd.New(1, 0)) // attempt to subtract 1
// The subtraction doesn't occur and doesn't change the error.
fmt.Printf("%s, err: %v\n", d, ed.Err())

Output:

10, err: <nil>
30, err: <nil>
Infinity, err: division by zero
Infinity, err: division by zero

func MakeErrDecimal Uses

func MakeErrDecimal(c *Context) ErrDecimal

MakeErrDecimal creates a ErrDecimal with given context.

func (*ErrDecimal) Abs Uses

func (e *ErrDecimal) Abs(d, x *Decimal) *Decimal

Abs performs e.Ctx.Abs(d, x) and returns d.

func (*ErrDecimal) Add Uses

func (e *ErrDecimal) Add(d, x, y *Decimal) *Decimal

Add performs e.Ctx.Add(d, x, y) and returns d.

func (*ErrDecimal) Ceil Uses

func (e *ErrDecimal) Ceil(d, x *Decimal) *Decimal

Ceil performs e.Ctx.Ceil(d, x) and returns d.

func (*ErrDecimal) Err Uses

func (e *ErrDecimal) Err() error

Err returns the first error encountered or the context's trap error if present.

func (*ErrDecimal) Exp Uses

func (e *ErrDecimal) Exp(d, x *Decimal) *Decimal

Exp performs e.Ctx.Exp(d, x) and returns d.

func (*ErrDecimal) Floor Uses

func (e *ErrDecimal) Floor(d, x *Decimal) *Decimal

Floor performs e.Ctx.Floor(d, x) and returns d.

func (*ErrDecimal) Int64 Uses

func (e *ErrDecimal) Int64(d *Decimal) int64

Int64 returns 0 if err is set. Otherwise returns d.Int64().

func (*ErrDecimal) Ln Uses

func (e *ErrDecimal) Ln(d, x *Decimal) *Decimal

Ln performs e.Ctx.Ln(d, x) and returns d.

func (*ErrDecimal) Log10 Uses

func (e *ErrDecimal) Log10(d, x *Decimal) *Decimal

Log10 performs d.Log10(x) and returns d.

func (*ErrDecimal) Mul Uses

func (e *ErrDecimal) Mul(d, x, y *Decimal) *Decimal

Mul performs e.Ctx.Mul(d, x, y) and returns d.

func (*ErrDecimal) Neg Uses

func (e *ErrDecimal) Neg(d, x *Decimal) *Decimal

Neg performs e.Ctx.Neg(d, x) and returns d.

func (*ErrDecimal) Pow Uses

func (e *ErrDecimal) Pow(d, x, y *Decimal) *Decimal

Pow performs e.Ctx.Pow(d, x, y) and returns d.

func (*ErrDecimal) Quantize Uses

func (e *ErrDecimal) Quantize(d, v *Decimal, exp int32) *Decimal

Quantize performs e.Ctx.Quantize(d, v, exp) and returns d.

func (*ErrDecimal) Quo Uses

func (e *ErrDecimal) Quo(d, x, y *Decimal) *Decimal

Quo performs e.Ctx.Quo(d, x, y) and returns d.

func (*ErrDecimal) QuoInteger Uses

func (e *ErrDecimal) QuoInteger(d, x, y *Decimal) *Decimal

QuoInteger performs e.Ctx.QuoInteger(d, x, y) and returns d.

func (*ErrDecimal) Reduce Uses

func (e *ErrDecimal) Reduce(d, x *Decimal) (int, *Decimal)

Reduce performs e.Ctx.Reduce(d, x) and returns the number of zeros removed and d.

func (*ErrDecimal) Rem Uses

func (e *ErrDecimal) Rem(d, x, y *Decimal) *Decimal

Rem performs e.Ctx.Rem(d, x, y) and returns d.

func (*ErrDecimal) Round Uses

func (e *ErrDecimal) Round(d, x *Decimal) *Decimal

Round performs e.Ctx.Round(d, x) and returns d.

func (*ErrDecimal) Sqrt Uses

func (e *ErrDecimal) Sqrt(d, x *Decimal) *Decimal

Sqrt performs e.Ctx.Sqrt(d, x) and returns d.

func (*ErrDecimal) Sub Uses

func (e *ErrDecimal) Sub(d, x, y *Decimal) *Decimal

Sub performs e.Ctx.Sub(d, x, y) and returns d.

func (*ErrDecimal) ToIntegral Uses

func (e *ErrDecimal) ToIntegral(d, x *Decimal) *Decimal

ToIntegral performs e.Ctx.ToIntegral(d, x) and returns d.

func (*ErrDecimal) ToIntegralX Uses

func (e *ErrDecimal) ToIntegralX(d, x *Decimal) *Decimal

ToIntegralX performs e.Ctx.ToIntegralX(d, x) and returns d.

type Form Uses

type Form int
const (
    Finite Form = iota
    Infinite
    // NaNSignaling will always raise the InvalidOperation condition during
    // an operation.
    NaNSignaling
    NaN
)

func (Form) String Uses

func (i Form) String() string

type Rounder Uses

type Rounder func(result *big.Int, neg bool, half int) bool

Rounder defines a function that returns true if 1 should be added to the absolute value of a number being rounded. result is the result to which the 1 would be added. neg is true if the number is negative. half is -1 if the discarded digits are < 0.5, 0 if = 0.5, or 1 if > 0.5.

func (Rounder) Round Uses

func (r Rounder) Round(c *Context, d, x *Decimal) Condition

Round sets d to rounded x.

Package apd imports 6 packages (graph) and is imported by 8 packages. Updated 2017-06-13. Refresh now. Tools for package owners.