Documentation
¶
Index ¶
- Variables
- type Context
- func (ctx Context) Add(d, e Decimal) Decimal
- func (ctx Context) FMA(d, e, f Decimal) Decimal
- func (ctx Context) Mul(d, e Decimal) Decimal
- func (ctx Context) MustParse(s string) Decimal
- func (ctx Context) Parse(s string) (Decimal, error)
- func (ctx Context) Quo(d, e Decimal) Decimal
- func (ctx Context) Round(d, e Decimal) Decimal
- func (ctx Context) Scan(d *Decimal, state fmt.ScanState, verb rune) error
- func (ctx Context) Sub(d, e Decimal) Decimal
- func (ctx Context) ToIntegral(d Decimal) Decimal
- func (ctx Context) With(d Decimal) Contextual
- type Contextual
- type Decimal
- func (d Decimal) Abs() Decimal
- func (d Decimal) Add(e Decimal) Decimal
- func (d Decimal) Append(buf []byte, format byte, prec int) []byte
- func (d Decimal) Class() string
- func (d Decimal) Cmp(e Decimal) int
- func (d Decimal) CmpDec(e Decimal) Decimal
- func (d Decimal) CopySign(e Decimal) Decimal
- func (d Decimal) Equal(e Decimal) bool
- func (d Decimal) FMA(e, f Decimal) Decimal
- func (d Decimal) Float64() float64
- func (d Decimal) Format(s fmt.State, verb rune)
- func (d *Decimal) GobDecode(buf []byte) error
- func (d Decimal) GobEncode() ([]byte, error)
- func (d Decimal) Int64() int64
- func (d Decimal) Int64x() (i int64, exact bool)
- func (d Decimal) IsInf() bool
- func (d Decimal) IsInt() bool
- func (d Decimal) IsNaN() bool
- func (d Decimal) IsQNaN() bool
- func (d Decimal) IsSNaN() bool
- func (d Decimal) IsSubnormal() bool
- func (d Decimal) IsZero() bool
- func (d Decimal) Logb() Decimal
- func (d Decimal) MarshalBinary() ([]byte, error)
- func (d Decimal) MarshalJSON() ([]byte, error)
- func (d Decimal) MarshalText() ([]byte, error)
- func (d Decimal) Max(e Decimal) Decimal
- func (d Decimal) MaxMag(e Decimal) Decimal
- func (d Decimal) Min(e Decimal) Decimal
- func (d Decimal) MinMag(e Decimal) Decimal
- func (d Decimal) Mul(e Decimal) Decimal
- func (d Decimal) Neg() Decimal
- func (d Decimal) NextMinus() Decimal
- func (d Decimal) NextPlus() Decimal
- func (d Decimal) Quo(e Decimal) Decimal
- func (d Decimal) Round(e Decimal) Decimal
- func (d Decimal) ScaleB(e Decimal) Decimal
- func (d Decimal) ScaleBInt(i int) Decimal
- func (d *Decimal) Scan(state fmt.ScanState, verb rune) error
- func (d Decimal) Sign() int
- func (d Decimal) Signbit() bool
- func (d Decimal) Sqrt() Decimal
- func (d Decimal) String() string
- func (d Decimal) Sub(e Decimal) Decimal
- func (d Decimal) Text(format byte, prec int) string
- func (d Decimal) ToIntegral() Decimal
- func (d *Decimal) UnmarshalBinary(data []byte) error
- func (d *Decimal) UnmarshalJSON(data []byte) error
- func (d *Decimal) UnmarshalText(text []byte) error
- type Error
- type Rounding
Constants ¶
This section is empty.
Variables ¶
var DefaultContext = Context{Rounding: HalfUp}
DefaultContext is the context that arithmetic functions will use in order to do calculations. Setting this context to a different value will globally affect all Decimal methods whose behavior depends on context. Note that all such methods are also available as direct methods of Context. It uses HalfUp rounding.
var DefaultFormatContext = Context{Rounding: HalfEven}
DefaultFormatContext is the default context use for formatting Decimal. Unlike DefaultContext, it uses HalfEven rounding to conform to standard Go formatting for float types.
var DefaultScanContext = DefaultFormatContext
var E = newFromParts(0, -15, 2_718281828459045)
E represents the transcendental number e (lim[n→∞](1+1/n)ⁿ).
var ErrNaN error = Error("sNaN64")
var Inf = newDec(inf)
Inf is ∞ represented as a Decimal.
var Max = newFromParts(0, expMax, maxSig)
Max is the highest finite number representable as a Decimal. It has the value 9.999999999999999E+384.
var Min = newFromParts(0, -398, 1)
Min is the closest positive number to zero. It has the value 1E-398.
var NegInf = newDec(neg | inf)
NegInf is -∞ represented as a Decimal.
var NegMax = newFromParts(1, expMax, maxSig)
NegMax is the lowest finite number representable as a Decimal. It has the value -9.999999999999999E+384.
var NegMin = newFromParts(1, -398, 1)
NegMin is the closest negative number to zero. It has the value -1E-398.
var NegOne = newFromParts(1, -15, decimalBase)
NegOne is -1 represented as a Decimal.
var NegZero = newFromParts(1, 0, 0)
NegZero is -0 represented as a Decimal. Note that Zero != NegZero, but Zero.Equal(NegZero) returns true.
var One = newFromParts(0, -15, decimalBase)
One is 1 represented as a Decimal.
var Pi = newFromParts(0, -15, 3_141592653589793)
Pi represents the transcendental number π.
var QNaN = newDec(0x7c << 56)
QNaN is a quiet NaN represented as a Decimal.
var SNaN = newDec(0x7e << 56)
SNaN is a signalling NaN represented as a Decimal. Note that the decimal never signals on NaNs but some operations treat sNaN differently to NaN.
var Zero = newFromParts(0, 0, 0)
Zero is 0 represented as a Decimal.
Functions ¶
This section is empty.
Types ¶
type Context ¶
type Context struct {
// Rounding sets the rounding behaviour of arithmetic operations.
Rounding Rounding
}
Context may be used to tune the behaviour of arithmetic operations.
func (Context) MustParse ¶
MustParse parses a string as a Decimal and returns the value or panics if the string doesn't represent a valid Decimal.
func (Context) Round ¶
Round rounds a number to a given power of ten value. The e argument should be a power of ten, such as 1, 10, 100, 1000, etc.
func (Context) ToIntegral ¶
ToIntegral rounds d to a nearby integer.
func (Context) With ¶
func (ctx Context) With(d Decimal) Contextual
type Contextual ¶
type Contextual struct {
// contains filtered or unexported fields
}
Contextual binds a Decimal to a Context for greater control of formatting. It implements fmt.Stringer and fmt.Formatter on behalf of the number, using the context to control formatting.
func (Contextual) String ¶
func (c Contextual) String() string
type Decimal ¶
type Decimal struct {
// contains filtered or unexported fields
}
Decimal represents an IEEE 754 64-bit floating point decimal number. It uses the binary representation method. Decimal is intentionally a struct to ensure users don't accidentally cast it to uint64.
func MustParse ¶
MustParse parses a string as a Decimal and returns the value or panics if the string doesn't represent a valid Decimal. It uses DefaultScanContext.
func NewFromFloat64 ¶ added in v1.13.0
func NewFromInt64 ¶
NewFromInt64 returns a new Decimal with the given value.
func Parse ¶
Parse parses a string representation of a number as a Decimal. It uses DefaultScanContext.
func (Decimal) Add ¶
Add computes d + e. It uses DefaultContext to call Context.Add.
func (Decimal) Class ¶
Class returns a string representing the number's 'type' that the decimal is. It can be one of the following:
- "+Normal"
- "-Normal"
- "+Subnormal"
- "-Subnormal"
- "+Zero"
- "-Zero"
- "+Infinity"
- "-Infinity"
- "NaN"
- "sNaN"
func (Decimal) Cmp ¶
Cmp returns:
-2 if d or e is NaN -1 if d < e 0 if d == e (incl. -0 == 0, -Inf == -Inf, and +Inf == +Inf) +1 if d > e
func (Decimal) CmpDec ¶
CmpDec is equivalent to Cmp but with a Decimal result. If d or e is NaN, it returns a corresponding NaN result.
func (Decimal) Equal ¶
Equal indicates whether two numbers are equal. It is equivalent to d.Cmp(e) == 0.
func (Decimal) FMA ¶
FMA computes d × e + f. It uses DefaultContext to call Context.FMA.
func (Decimal) Int64 ¶
Int64 returns an int64 representation of d, clamped to [math.MinInt64, math.MaxInt64].
func (Decimal) Int64x ¶
Int64x returns an int64 representation of d, clamped to [math.MinInt64, math.MaxInt64]. The second return value, exact, indicates whether NewFromInt64(i) == d.
func (Decimal) IsSubnormal ¶
IsSubnormal indicates whether d is a subnormal.
func (Decimal) MarshalBinary ¶
MarshalBinary implements the encoding.BinaryMarshaler interface.
func (Decimal) MarshalJSON ¶
MarshalText implements the encoding.TextMarshaler interface.
func (Decimal) MarshalText ¶
MarshalText implements the encoding.TextMarshaler interface.
func (Decimal) Mul ¶
Mul computes d × e. It uses DefaultContext to call Context.Mul.
func (Decimal) Quo ¶
Quo computes d ÷ e. It uses DefaultContext to call Context.Quo.
func (Decimal) Round ¶
Round rounds a number to a given power-of-10 value. The e argument should be a power of ten, such as 1, 10, 100, 1000, etc. It uses DefaultContext to call Context.Round.
func (*Decimal) Scan ¶
Scan implements fmt.Scanner. It uses DefaultScanContext.
func (Decimal) Sub ¶
Sub returns d - e. It uses DefaultContext to call Context.Sub.
func (Decimal) Text ¶
Text converts the floating-point number x to a string according to the given format and precision prec.
func (Decimal) ToIntegral ¶
ToIntegral rounds d to a nearby integer. It uses DefaultContext to call Context.ToIntegral.
func (*Decimal) UnmarshalBinary ¶
UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
func (*Decimal) UnmarshalJSON ¶
UnmarshalText implements the encoding.TextUnmarshaler interface.
func (*Decimal) UnmarshalText ¶
UnmarshalText implements the encoding.TextUnmarshaler interface.
type Rounding ¶
type Rounding int8
Rounding defines how arithmetic operations round numbers in certain operations.
const ( // HalfUp rounds to the nearest number, rounding away from zero if the // number is exactly halfway between two possible roundings. HalfUp Rounding = iota // HalfEven rounds to the nearest number, rounding to the nearest even // number if the number is exactly halfway between two possible roundings. HalfEven // Down rounds towards zero. Down )