decimal128

package
v17.0.0-...-32885c9 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2024 License: Apache-2.0, BSD-2-Clause, BSD-3-Clause, + 7 more Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MaxPrecision = 38
	MaxScale     = 38
)

Variables

View Source
var (
	MaxDecimal128 = New(542101086242752217, 687399551400673280-1)
)

Functions

This section is empty.

Types

type Num

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

Num represents a signed 128-bit integer in two's complement. Calculations wrap around and overflow is ignored.

For a discussion of the algorithms, look at Knuth's volume 2, Semi-numerical Algorithms section 4.3.1.

Adapted from the Apache ORC C++ implementation

func FromBigInt

func FromBigInt(v *big.Int) (n Num)

FromBigInt will convert a big.Int to a Num, if the value in v has a BitLen > 128, this will panic.

func FromFloat32

func FromFloat32(v float32, prec, scale int32) (Num, error)

FromFloat32 returns a new decimal128.Num constructed from the given float32 value using the provided precision and scale. Will return an error if the value cannot be accurately represented with the desired precision and scale.

func FromFloat64

func FromFloat64(v float64, prec, scale int32) (Num, error)

FromFloat64 returns a new decimal128.Num constructed from the given float64 value using the provided precision and scale. Will return an error if the value cannot be accurately represented with the desired precision and scale.

func FromI64

func FromI64(v int64) Num

FromI64 returns a new signed 128-bit integer value from the provided int64 one.

func FromString

func FromString(v string, prec, scale int32) (n Num, err error)

func FromU64

func FromU64(v uint64) Num

FromU64 returns a new signed 128-bit integer value from the provided uint64 one.

func GetHalfScaleMultiplier

func GetHalfScaleMultiplier(pow int) Num

func GetMaxValue

func GetMaxValue(prec int32) Num

func GetScaleMultiplier

func GetScaleMultiplier(pow int) Num

func Max

func Max(first Num, rest ...Num) Num

Max returns the largest Decimal128 that was passed in the arguments

func Min

func Min(first Num, rest ...Num) Num

Min returns the smallest Decimal128 that was passed in the arguments

func New

func New(hi int64, lo uint64) Num

New returns a new signed 128-bit integer value.

func (Num) Abs

func (n Num) Abs() Num

Abs returns a new decimal128.Num that contains the absolute value of n

func (Num) Add

func (n Num) Add(rhs Num) Num

func (Num) BigInt

func (n Num) BigInt() *big.Int

while the code would be simpler to just do lsh/rsh and add it turns out from benchmarking that calling SetBits passing in the words and negating ends up being >2x faster

func (Num) Cmp

func (n Num) Cmp(other Num) int

Cmp compares the numbers represented by n and other and returns:

+1 if n > other
 0 if n == other
-1 if n < other

func (Num) Div

func (n Num) Div(rhs Num) (res, rem Num)

func (Num) FitsInPrecision

func (n Num) FitsInPrecision(prec int32) bool

FitsInPrecision returns true or false if the value currently held by n would fit within precision (0 < prec <= 38) without losing any data.

func (Num) Greater

func (n Num) Greater(other Num) bool

Greater returns true if the value represented by n is > other

func (Num) GreaterEqual

func (n Num) GreaterEqual(other Num) bool

GreaterEqual returns true if the value represented by n is >= other

func (Num) HighBits

func (n Num) HighBits() int64

HighBits returns the high bits of the two's complement representation of the number.

func (Num) IncreaseScaleBy

func (n Num) IncreaseScaleBy(increase int32) Num

IncreaseScaleBy returns a new decimal128.Num with the value scaled up by the desired amount. Must be 0 <= increase <= 38. Any data loss from scaling is ignored. If you wish to prevent data loss, use Rescale which will return an error if data loss is detected.

func (Num) Less

func (n Num) Less(other Num) bool

Less returns true if the value represented by n is < other

func (Num) LessEqual

func (n Num) LessEqual(other Num) bool

LessEqual returns true if the value represented by n is <= other

func (Num) LowBits

func (n Num) LowBits() uint64

LowBits returns the low bits of the two's complement representation of the number.

func (Num) Mul

func (n Num) Mul(rhs Num) Num

func (Num) Negate

func (n Num) Negate() Num

Negate returns a copy of this Decimal128 value but with the sign negated

func (Num) Pow

func (n Num) Pow(rhs Num) Num

func (Num) ReduceScaleBy

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

ReduceScaleBy returns a new decimal128.Num with the value scaled down by the desired amount and, if 'round' is true, the value will be rounded accordingly. Assumes 0 <= reduce <= 38. Any data loss from scaling is ignored. If you wish to prevent data loss, use Rescale which will return an error if data loss is detected.

func (Num) Rescale

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

Rescale returns a new decimal128.Num with the value updated assuming the current value is scaled to originalScale with the new value scaled to newScale. If rescaling this way would cause data loss, an error is returned instead.

func (Num) Sign

func (n Num) Sign() int

Sign returns:

-1 if x < 0

0 if x == 0

+1 if x > 0

func (Num) Sub

func (n Num) Sub(rhs Num) Num

func (Num) ToFloat32

func (n Num) ToFloat32(scale int32) float32

ToFloat32 returns a float32 value representative of this decimal128.Num, but with the given scale.

func (Num) ToFloat64

func (n Num) ToFloat64(scale int32) float64

ToFloat64 returns a float64 value representative of this decimal128.Num, but with the given scale.

func (Num) ToString

func (n Num) ToString(scale int32) string

Jump to

Keyboard shortcuts

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