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 ¶
- type Context
- func (c *Context) Abs(z, x *decimal.Decimal) *decimal.Decimal
- func (c *Context) Add(z, x, y *decimal.Decimal) (r *decimal.Decimal)
- func (c *Context) Err() (err error)
- func (c *Context) FMA(z, x, y, u *decimal.Decimal) (r *decimal.Decimal)
- func (c *Context) Mode() decimal.RoundingMode
- func (c *Context) Mul(z, x, y *decimal.Decimal) (r *decimal.Decimal)
- func (c *Context) Neg(z, x *decimal.Decimal) *decimal.Decimal
- func (c *Context) New() *decimal.Decimal
- func (c *Context) NewFloat(x *big.Float) *decimal.Decimal
- func (c *Context) NewFloat64(x float64) *decimal.Decimal
- func (c *Context) NewInt(x *big.Int) *decimal.Decimal
- func (c *Context) NewInt64(x int64) *decimal.Decimal
- func (c *Context) NewRat(x *big.Rat) *decimal.Decimal
- func (c *Context) NewString(s string) (d *decimal.Decimal, success bool)
- func (c *Context) NewUint64(x uint64) *decimal.Decimal
- func (c *Context) ParseDecimal(s string, base int) (f *decimal.Decimal, b int, err error)
- func (c *Context) Prec() uint
- func (c *Context) Quo(z, x, y *decimal.Decimal) (r *decimal.Decimal)
- func (c *Context) Round(z, x *decimal.Decimal) *decimal.Decimal
- func (c *Context) SetMode(mode decimal.RoundingMode) *Context
- func (c *Context) SetPrec(prec uint) *Context
- func (c *Context) Sqrt(z, x *decimal.Decimal) (r *decimal.Decimal)
- func (c *Context) Sub(z, x, y *decimal.Decimal) (r *decimal.Decimal)
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 ¶
Abs sets z to the (possibly rounded) value |x| (the absolute value of x) and returns z.
func (*Context) Err ¶
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 ¶
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) Neg ¶
Neg sets z to the (possibly rounded) value of x with its sign negated, and returns z.
func (*Context) New ¶
New returns a new decimal.Decimal with value 0, precision and rounding mode set to c's precision and rounding mode.
func (*Context) NewFloat ¶
NewFloat returns a new *decimal.Decimal set to the (possibly rounded) value of x.
func (*Context) NewFloat64 ¶
NewFloat64 returns a new *decimal.Decimal set to the (possibly rounded) value of x.
func (*Context) NewInt ¶
NewInt returns a new *decimal.Decimal set to the (possibly rounded) value of x.
func (*Context) NewInt64 ¶
NewInt64 returns a new *decimal.Decimal set to the (possibly rounded) value of x.
func (*Context) NewRat ¶
NewRat returns a new *decimal.Decimal set to the (possibly rounded) value of x.
func (*Context) NewString ¶
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 ¶
NewUint64 returns a new *decimal.Decimal set to the (possibly rounded) value of x.
func (*Context) ParseDecimal ¶
ParseDecimal is like d.Parse(s, base) with d set to the given precision and rounding mode.
func (*Context) Prec ¶
Prec returns the mantissa precision of x in decimal digits. The result may be 0 for |x| == 0 and |x| == Inf.
func (*Context) Round ¶
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 ¶
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.