context

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: May 29, 2020 License: BSD-2-Clause Imports: 3 Imported by: 0

Documentation

Overview

Package context provides IEEE-754 style contexts for Decimals.

All factory functions of the form

func (c *Context) NewT(x T) *decimal.Decimal

create a new decimal.Decimal set to the value of x, and rounded using c's precision and rounding mode.

Operators that set a receiver z to function of other decimal arguments like:

func (c *Context) UnaryOp(z, x *decimal.Decimal) *decimal.Decimal
func (c *Context) BinaryOp(z, x, y *decimal.Decimal) *decimal.Decimal

set z to the result of z.Op(args), rounded using the c's precision and rounding mode and return z.

A Context catches NaN errors: if an operation generates a NaN, the operation will silently succeed with an undefined result. Further operations with the context will be no-ops (they simply return the receiver z) until (*Context).Err is called to check for errors.

Although it does not exactly provide IEEE-754 NaNs, it provides a form of support for quiet NaNs.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Context

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

A Context is a wrapper around Decimals that facilitates management of rounding modes, precision and error handling.

func New

func New(prec uint, mode decimal.RoundingMode) *Context

New creates a new context with the given precision and rounding mode. If prec is 0, it will be set to decimal.DefaultRoundingMode.

func (*Context) Abs

func (c *Context) Abs(z, x *decimal.Decimal) *decimal.Decimal

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

func (*Context) Add

func (c *Context) Add(z, x, y *decimal.Decimal) (r *decimal.Decimal)

Add sets z to the rounded sum x+y and returns z.

func (*Context) Err

func (c *Context) Err() (err error)

Err returns the first error encountered since the last call to Err and clears the error state.

Example
c := New(0, decimal.ToNearestEven)

x := c.NewInt64(-1)
z := c.Sqrt(c.New(), x) // silently fails

zero := c.New()
// no-op, c.Err will still report "square root of negative operand" but the
// value of z is undefined.
c.Quo(z, zero, zero)

// calling c.Err clears the error state.
fmt.Println(c.Err())

x.SetInt64(4)
c.Sqrt(z, x)
fmt.Printf("√(%g) = %g", x, z)

//
Output:

square root of negative operand
√(4) = 2

func (*Context) FMA

func (c *Context) FMA(z, x, y, u *decimal.Decimal) (r *decimal.Decimal)

FMA sets z to x * y + u, computed with only one rounding. That is, FMA performs the fused multiply-add of x, y, and u.

func (*Context) Mode

func (c *Context) Mode() decimal.RoundingMode

Mode returns the rounding mode of c.

func (*Context) Mul

func (c *Context) Mul(z, x, y *decimal.Decimal) (r *decimal.Decimal)

Mul sets z to the rounded product x×y and returns z.

func (*Context) Neg

func (c *Context) Neg(z, x *decimal.Decimal) *decimal.Decimal

Neg sets z to the (possibly rounded) value of x with its sign negated, and returns z.

func (*Context) New

func (c *Context) New() *decimal.Decimal

New returns a new decimal.Decimal with value 0, precision and rounding mode set to c's precision and rounding mode.

func (*Context) NewFloat

func (c *Context) NewFloat(x *big.Float) *decimal.Decimal

NewFloat returns a new *decimal.Decimal set to the (possibly rounded) value of x.

func (*Context) NewFloat64

func (c *Context) NewFloat64(x float64) *decimal.Decimal

NewFloat64 returns a new *decimal.Decimal set to the (possibly rounded) value of x.

func (*Context) NewInt

func (c *Context) NewInt(x *big.Int) *decimal.Decimal

NewInt returns a new *decimal.Decimal set to the (possibly rounded) value of x.

func (*Context) NewInt64

func (c *Context) NewInt64(x int64) *decimal.Decimal

NewInt64 returns a new *decimal.Decimal set to the (possibly rounded) value of x.

func (*Context) NewRat

func (c *Context) NewRat(x *big.Rat) *decimal.Decimal

NewRat returns a new *decimal.Decimal set to the (possibly rounded) value of x.

func (*Context) NewString

func (c *Context) NewString(s string) (d *decimal.Decimal, success bool)

NewFromString returns a new Decimal with the value of s and a boolean indicating success. s must be a floating-point number of the same format as accepted by (*decimal.Decimal).Parse, with base argument 0. The entire string (not just a prefix) must be valid for success. If the operation failed, the value of d is undefined but the returned value is nil. d's precision and rounding mode are set to c's precision and rounding mode.

func (*Context) NewUint64

func (c *Context) NewUint64(x uint64) *decimal.Decimal

NewUint64 returns a new *decimal.Decimal set to the (possibly rounded) value of x.

func (*Context) ParseDecimal

func (c *Context) ParseDecimal(s string, base int) (f *decimal.Decimal, b int, err error)

ParseDecimal is like d.Parse(s, base) with d set to the given precision and rounding mode.

func (*Context) Prec

func (c *Context) Prec() uint

Prec returns the mantissa precision of x in decimal digits. The result may be 0 for |x| == 0 and |x| == Inf.

func (*Context) Quo

func (c *Context) Quo(z, x, y *decimal.Decimal) (r *decimal.Decimal)

Quo sets z to the rounded quotient x/y and returns z.

func (*Context) Round

func (c *Context) Round(z, x *decimal.Decimal) *decimal.Decimal

Round sets z's to the value of x and returns z rounded using c's precision and rounding mode.

func (*Context) SetMode

func (c *Context) SetMode(mode decimal.RoundingMode) *Context

SetMode sets c's rounding mode to mode and returns c.

func (*Context) SetPrec

func (c *Context) SetPrec(prec uint) *Context

SetPrec sets c's precision to prec and returns c.

If prec > MaxPrec, it is set to MaxPrec. If prec == 0, it is set to decimal.DefaultDecimalPrec.

func (*Context) Sqrt

func (c *Context) Sqrt(z, x *decimal.Decimal) (r *decimal.Decimal)

Sqrt sets z to the rounded square root of x, and returns z.

func (*Context) Sub

func (c *Context) Sub(z, x, y *decimal.Decimal) (r *decimal.Decimal)

Sub sets z to the rounded difference x+y and returns z.

Jump to

Keyboard shortcuts

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