d64

package
v1.15.0 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2025 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
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.

View Source
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.

View Source
var DefaultScanContext = DefaultFormatContext
View Source
var E = newFromParts(0, -15, 2_718281828459045)

E represents the transcendental number e (lim[n→∞](1+1/n)ⁿ).

View Source
var ErrNaN error = Error("sNaN64")
View Source
var Inf = newDec(inf)

Inf is ∞ represented as a Decimal.

View Source
var Max = newFromParts(0, expMax, maxSig)

Max is the highest finite number representable as a Decimal. It has the value 9.999999999999999E+384.

View Source
var Min = newFromParts(0, -398, 1)

Min is the closest positive number to zero. It has the value 1E-398.

View Source
var NegInf = newDec(neg | inf)

NegInf is -∞ represented as a Decimal.

View Source
var NegMax = newFromParts(1, expMax, maxSig)

NegMax is the lowest finite number representable as a Decimal. It has the value -9.999999999999999E+384.

View Source
var NegMin = newFromParts(1, -398, 1)

NegMin is the closest negative number to zero. It has the value -1E-398.

View Source
var NegOne = newFromParts(1, -15, decimalBase)

NegOne is -1 represented as a Decimal.

View Source
var NegZero = newFromParts(1, 0, 0)

NegZero is -0 represented as a Decimal. Note that Zero != NegZero, but Zero.Equal(NegZero) returns true.

View Source
var One = newFromParts(0, -15, decimalBase)

One is 1 represented as a Decimal.

View Source
var Pi = newFromParts(0, -15, 3_141592653589793)

Pi represents the transcendental number π.

View Source
var QNaN = newDec(0x7c << 56)

QNaN is a quiet NaN represented as a Decimal.

View Source
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.

View Source
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) Add

func (ctx Context) Add(d, e Decimal) Decimal

Add computes d + e

func (Context) FMA

func (ctx Context) FMA(d, e, f Decimal) Decimal

FMA computes d*e + f

func (Context) Mul

func (ctx Context) Mul(d, e Decimal) Decimal

Mul computes d * e.

func (Context) MustParse

func (ctx Context) MustParse(s string) Decimal

MustParse parses a string as a Decimal and returns the value or panics if the string doesn't represent a valid Decimal.

func (Context) Parse

func (ctx Context) Parse(s string) (Decimal, error)

Parse parses a string representation of a number as a Decimal.

func (Context) Quo

func (ctx Context) Quo(d, e Decimal) Decimal

Quo computes d / e. Rounding rules are applied as per the context.

func (Context) Round

func (ctx Context) Round(d, e Decimal) Decimal

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) Scan

func (ctx Context) Scan(d *Decimal, state fmt.ScanState, verb rune) error

Scan scans a string into a Decimal, applying context rounding.

func (Context) Sub

func (ctx Context) Sub(d, e Decimal) Decimal

Add computes d + e

func (Context) ToIntegral

func (ctx Context) ToIntegral(d Decimal) Decimal

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) Format

func (c Contextual) Format(s fmt.State, verb rune)

func (Contextual) String

func (c Contextual) String() string

func (Contextual) Text

func (c Contextual) Text(verb rune, width, prec int) 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

func MustParse(s string) Decimal

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 NewFromFloat64(f float64) Decimal

func NewFromInt64

func NewFromInt64(i int64) Decimal

NewFromInt64 returns a new Decimal with the given value.

func Parse

func Parse(s string) (Decimal, error)

Parse parses a string representation of a number as a Decimal. It uses DefaultScanContext.

func (Decimal) Abs

func (d Decimal) Abs() Decimal

Abs computes ||d||.

func (Decimal) Add

func (d Decimal) Add(e Decimal) Decimal

Add computes d + e. It uses DefaultContext to call Context.Add.

func (Decimal) Append

func (d Decimal) Append(buf []byte, format byte, prec int) []byte

Append appends the text representation of d to buf.

func (Decimal) Class

func (d Decimal) Class() string

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

func (d Decimal) Cmp(e Decimal) int

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

func (d Decimal) CmpDec(e Decimal) Decimal

CmpDec is equivalent to Cmp but with a Decimal result. If d or e is NaN, it returns a corresponding NaN result.

func (Decimal) CopySign

func (d Decimal) CopySign(e Decimal) Decimal

CopySign copies d, but with the sign taken from e.

func (Decimal) Equal

func (d Decimal) Equal(e Decimal) bool

Equal indicates whether two numbers are equal. It is equivalent to d.Cmp(e) == 0.

func (Decimal) FMA

func (d Decimal) FMA(e, f Decimal) Decimal

FMA computes d × e + f. It uses DefaultContext to call Context.FMA.

func (Decimal) Float64

func (d Decimal) Float64() float64

Float64 returns a float64 representation of d.

func (Decimal) Format

func (d Decimal) Format(s fmt.State, verb rune)

Format implements fmt.Formatter.

func (*Decimal) GobDecode

func (d *Decimal) GobDecode(buf []byte) error

GobDecode implements encoding.GobDecoder.

func (Decimal) GobEncode

func (d Decimal) GobEncode() ([]byte, error)

GobEncode implements encoding.GobEncoder.

func (Decimal) Int64

func (d Decimal) Int64() int64

Int64 returns an int64 representation of d, clamped to [math.MinInt64, math.MaxInt64].

func (Decimal) Int64x

func (d Decimal) Int64x() (i int64, exact bool)

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) IsInf

func (d Decimal) IsInf() bool

IsInf indicates whether d is ±∞.

func (Decimal) IsInt

func (d Decimal) IsInt() bool

IsInt indicates whether d is an integer.

func (Decimal) IsNaN

func (d Decimal) IsNaN() bool

IsNaN indicates whether d is not a number.

func (Decimal) IsQNaN

func (d Decimal) IsQNaN() bool

IsQNaN indicates whether d is a quiet NaN.

func (Decimal) IsSNaN

func (d Decimal) IsSNaN() bool

IsSNaN indicates whether d is a signalling NaN.

func (Decimal) IsSubnormal

func (d Decimal) IsSubnormal() bool

IsSubnormal indicates whether d is a subnormal.

func (Decimal) IsZero

func (d Decimal) IsZero() bool

IsZero returns true if the Decimal encodes a zero value.

func (Decimal) Logb

func (d Decimal) Logb() Decimal

Logb return the integral log10 of d.

func (Decimal) MarshalBinary

func (d Decimal) MarshalBinary() ([]byte, error)

MarshalBinary implements the encoding.BinaryMarshaler interface.

func (Decimal) MarshalJSON

func (d Decimal) MarshalJSON() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface.

func (Decimal) MarshalText

func (d Decimal) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface.

func (Decimal) Max

func (d Decimal) Max(e Decimal) Decimal

Max returns the lower of d and e.

func (Decimal) MaxMag

func (d Decimal) MaxMag(e Decimal) Decimal

MaxMag returns the lower of d and e.

func (Decimal) Min

func (d Decimal) Min(e Decimal) Decimal

Min returns the lower of d and e.

func (Decimal) MinMag

func (d Decimal) MinMag(e Decimal) Decimal

MinMag returns the lower of d and e.

func (Decimal) Mul

func (d Decimal) Mul(e Decimal) Decimal

Mul computes d × e. It uses DefaultContext to call Context.Mul.

func (Decimal) Neg

func (d Decimal) Neg() Decimal

Neg computes -d.

func (Decimal) NextMinus

func (d Decimal) NextMinus() Decimal

NextMinus returns the next value above d.

func (Decimal) NextPlus

func (d Decimal) NextPlus() Decimal

NextPlus returns the next value above d.

func (Decimal) Quo

func (d Decimal) Quo(e Decimal) Decimal

Quo computes d ÷ e. It uses DefaultContext to call Context.Quo.

func (Decimal) Round

func (d Decimal) Round(e Decimal) Decimal

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) ScaleB

func (d Decimal) ScaleB(e Decimal) Decimal

func (Decimal) ScaleBInt

func (d Decimal) ScaleBInt(i int) Decimal

func (*Decimal) Scan

func (d *Decimal) Scan(state fmt.ScanState, verb rune) error

Scan implements fmt.Scanner. It uses DefaultScanContext.

func (Decimal) Sign

func (d Decimal) Sign() int

Sign returns -1/0/1 if d is </=/> 0, respectively.

func (Decimal) Signbit

func (d Decimal) Signbit() bool

Signbit indicates whether d is negative or -0.

func (Decimal) Sqrt

func (d Decimal) Sqrt() Decimal

Sqrt computes √d.

func (Decimal) String

func (d Decimal) String() string

String returns a string representation of d.

func (Decimal) Sub

func (d Decimal) Sub(e Decimal) Decimal

Sub returns d - e. It uses DefaultContext to call Context.Sub.

func (Decimal) Text

func (d Decimal) Text(format byte, prec int) string

Text converts the floating-point number x to a string according to the given format and precision prec.

func (Decimal) ToIntegral

func (d Decimal) ToIntegral() Decimal

ToIntegral rounds d to a nearby integer. It uses DefaultContext to call Context.ToIntegral.

func (*Decimal) UnmarshalBinary

func (d *Decimal) UnmarshalBinary(data []byte) error

UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.

func (*Decimal) UnmarshalJSON

func (d *Decimal) UnmarshalJSON(data []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface.

func (*Decimal) UnmarshalText

func (d *Decimal) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface.

type Error

type Error string

func (Error) Error

func (e Error) Error() string

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
)

func (Rounding) String

func (r Rounding) String() string

Jump to

Keyboard shortcuts

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