decimal

package
v18.0.0-rc0 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2024 License: Apache-2.0, BSD-3-Clause Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Dec32Traits  dec32Traits
	Dec64Traits  dec64Traits
	Dec128Traits dec128Traits
	Dec256Traits dec256Traits
)

Functions

func MaxPrecision

func MaxPrecision[T DecimalTypes]() int

Types

type Decimal128

type Decimal128 = decimal128.Num

func Decimal128FromFloat

func Decimal128FromFloat(v float64, prec, scale int32) (Decimal128, error)

func Decimal128FromString

func Decimal128FromString(v string, prec, scale int32) (n Decimal128, err error)

type Decimal256

type Decimal256 = decimal256.Num

func Decimal256FromFloat

func Decimal256FromFloat(v float64, prec, scale int32) (Decimal256, error)

func Decimal256FromString

func Decimal256FromString(v string, prec, scale int32) (n Decimal256, err error)

type Decimal32

type Decimal32 int32

func Decimal32FromFloat

func Decimal32FromFloat[F float32 | float64](v F, prec, scale int32) (Decimal32, error)

func Decimal32FromString

func Decimal32FromString(v string, prec, scale int32) (n Decimal32, err error)

func (Decimal32) Abs

func (n Decimal32) Abs() Decimal32

func (Decimal32) Add

func (d Decimal32) Add(rhs Decimal32) Decimal32

func (Decimal32) Cmp

func (n Decimal32) Cmp(other Decimal32) int

func (Decimal32) Div

func (d Decimal32) Div(rhs Decimal32) (res, rem Decimal32)

func (Decimal32) FitsInPrecision

func (n Decimal32) FitsInPrecision(prec int32) bool

func (Decimal32) IncreaseScaleBy

func (n Decimal32) IncreaseScaleBy(increase int32) Decimal32

func (Decimal32) Mul

func (d Decimal32) Mul(rhs Decimal32) Decimal32

func (Decimal32) Negate

func (d Decimal32) Negate() Decimal32

func (Decimal32) Pow

func (d Decimal32) Pow(rhs Decimal32) Decimal32

func (Decimal32) ReduceScaleBy

func (n Decimal32) ReduceScaleBy(reduce int32, round bool) Decimal32

func (Decimal32) Rescale

func (n Decimal32) Rescale(originalScale, newScale int32) (out Decimal32, err error)

func (Decimal32) Sign

func (d Decimal32) Sign() int

func (Decimal32) Sub

func (d Decimal32) Sub(rhs Decimal32) Decimal32

func (Decimal32) ToBigFloat

func (n Decimal32) ToBigFloat(scale int32) *big.Float

func (Decimal32) ToFloat32

func (n Decimal32) ToFloat32(scale int32) float32

func (Decimal32) ToFloat64

func (n Decimal32) ToFloat64(scale int32) float64

func (Decimal32) ToString

func (n Decimal32) ToString(scale int32) string

type Decimal64

type Decimal64 int64

func Decimal64FromFloat

func Decimal64FromFloat[F float32 | float64](v F, prec, scale int32) (Decimal64, error)

func Decimal64FromString

func Decimal64FromString(v string, prec, scale int32) (n Decimal64, err error)

func (Decimal64) Abs

func (n Decimal64) Abs() Decimal64

func (Decimal64) Add

func (d Decimal64) Add(rhs Decimal64) Decimal64

func (Decimal64) Cmp

func (n Decimal64) Cmp(other Decimal64) int

func (Decimal64) Div

func (d Decimal64) Div(rhs Decimal64) (res, rem Decimal64)

func (Decimal64) FitsInPrecision

func (n Decimal64) FitsInPrecision(prec int32) bool

func (Decimal64) IncreaseScaleBy

func (n Decimal64) IncreaseScaleBy(increase int32) Decimal64

func (Decimal64) Mul

func (d Decimal64) Mul(rhs Decimal64) Decimal64

func (Decimal64) Negate

func (d Decimal64) Negate() Decimal64

func (Decimal64) Pow

func (d Decimal64) Pow(rhs Decimal64) Decimal64

func (Decimal64) ReduceScaleBy

func (n Decimal64) ReduceScaleBy(reduce int32, round bool) Decimal64

func (Decimal64) Rescale

func (n Decimal64) Rescale(originalScale, newScale int32) (out Decimal64, err error)

func (Decimal64) Sign

func (d Decimal64) Sign() int

func (Decimal64) Sub

func (d Decimal64) Sub(rhs Decimal64) Decimal64

func (Decimal64) ToBigFloat

func (n Decimal64) ToBigFloat(scale int32) *big.Float

func (Decimal64) ToFloat32

func (n Decimal64) ToFloat32(scale int32) float32

func (Decimal64) ToFloat64

func (n Decimal64) ToFloat64(scale int32) float64

func (Decimal64) ToString

func (n Decimal64) ToString(scale int32) string

type DecimalTypes

type DecimalTypes interface {
	Decimal32 | Decimal64 | Decimal128 | Decimal256
}

DecimalTypes is a generic constraint representing the implemented decimal types in this package, and a single point of update for future additions. Everything else is constrained by this.

type Num

type Num[T DecimalTypes] interface {
	Negate() T
	Add(T) T
	Sub(T) T
	Mul(T) T
	Div(T) (res, rem T)
	Pow(T) T

	FitsInPrecision(int32) bool
	Abs() T
	Sign() int
	Rescale(int32, int32) (T, error)
	Cmp(T) int

	IncreaseScaleBy(int32) T
	ReduceScaleBy(int32, bool) T

	ToFloat32(int32) float32
	ToFloat64(int32) float64
	ToBigFloat(int32) *big.Float

	ToString(int32) string
}

Num is an interface that is useful for building generic types for all decimal type implementations. It presents all the methods and interfaces necessary to operate on the decimal objects without having to care about the bit width.

type Traits

type Traits[T DecimalTypes] interface {
	BytesRequired(int) int
	FromString(string, int32, int32) (T, error)
	FromFloat64(float64, int32, int32) (T, error)
}

Traits is a convenience for building generic objects for operating on Decimal values to get around the limitations of Go generics. By providing this interface a generic object can handle producing the proper types to generate new decimal values.

Jump to

Keyboard shortcuts

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