Documentation ¶
Overview ¶
Package math provides helper functions for doing mathematical calculations and parsing for the ecocredit module.
Index ¶
- Constants
- Variables
- func MatchEq(dec Dec) gomock.Matcher
- type Dec
- func Add(x Dec, y Dec) (Dec, error)
- func NewDecFinite(coeff int64, exp int32) Dec
- func NewDecFromInt64(x int64) Dec
- func NewDecFromString(s string) (Dec, error)
- func NewNonNegativeDecFromString(s string) (Dec, error)
- func NewNonNegativeFixedDecFromString(s string, max uint32) (Dec, error)
- func NewPositiveDecFromString(s string) (Dec, error)
- func NewPositiveFixedDecFromString(s string, max uint32) (Dec, error)
- func SafeAddBalance(x Dec, y Dec) (Dec, error)
- func SafeSubBalance(x Dec, y Dec) (Dec, error)
- func SubNonNegative(x Dec, y Dec) (Dec, error)
- func (x Dec) Add(y Dec) (Dec, error)
- func (x Dec) BigInt() (*big.Int, error)
- func (x Dec) Cmp(y Dec) int
- func (x Dec) Equal(y Dec) bool
- func (x Dec) Int64() (int64, error)
- func (x Dec) IsFinite() bool
- func (x Dec) IsNegative() bool
- func (x Dec) IsPositive() bool
- func (x Dec) IsZero() bool
- func (x Dec) Mul(y Dec) (Dec, error)
- func (x Dec) MulExact(y Dec) (Dec, error)
- func (x Dec) NumDecimalPlaces() uint32
- func (x Dec) Quo(y Dec) (Dec, error)
- func (x Dec) QuoExact(y Dec) (Dec, error)
- func (x Dec) QuoInteger(y Dec) (Dec, error)
- func (x Dec) Reduce() (Dec, int)
- func (x Dec) Rem(y Dec) (Dec, error)
- func (x Dec) SdkIntTrim() sdkmath.Int
- func (x Dec) String() string
- func (x Dec) Sub(y Dec) (Dec, error)
Constants ¶
const ( GreaterThan = 1 LessThan = -1 EqualTo = 0 )
constants for more convenient intent behind dec.Cmp values.
Variables ¶
var ( ErrInvalidDecString = errors.Register(mathCodespace, 1, "invalid decimal string") ErrUnexpectedRounding = errors.Register(mathCodespace, 2, "unexpected rounding") ErrNonIntegeral = errors.Register(mathCodespace, 3, "value is non-integral") ErrInfiniteString = errors.Register(mathCodespace, 4, "value is infinite") )
Functions ¶
Types ¶
type Dec ¶
type Dec struct {
// contains filtered or unexported fields
}
Dec is a wrapper struct around apd.Decimal that does no mutation of apd.Decimal's when performing arithmetic, instead creating a new apd.Decimal for every operation ensuring usage is safe.
Using apd.Decimal directly can be unsafe because apd operations mutate the underlying Decimal, but when copying the big.Int structure can be shared between Decimal instances causing corruption. This was originally discovered in regen0-network/mainnet#15.
func NewDecFinite ¶
NewDecFinite returns a decimal with a value of coeff * 10^exp.
func NewDecFromInt64 ¶
func NewDecFromString ¶
func SafeAddBalance ¶
SafeAddBalance adds the value of x+y and returns the result with arbitrary precision. Returns with ErrInvalidRequest error if either x or y is negative.
func SafeSubBalance ¶
SafeSubBalance subtracts the value of y from x and returns the result with arbitrary precision. Returns with ErrInsufficientFunds error if the result is negative.
func SubNonNegative ¶
SubNonNegative subtracts the value of y from x and returns the result with arbitrary precision. Returns an error if the result is negative.
func (Dec) Add ¶
Add returns a new Dec with value `x+y` without mutating any argument and error if there is an overflow.
func (Dec) BigInt ¶
BigInt converts x to a *big.Int or returns an error if x cannot fit precisely into an *big.Int.
func (Dec) Cmp ¶
Cmp compares x and y and returns: -1 if x < y 0 if x == y +1 if x > y undefined if d or x are NaN
func (Dec) Int64 ¶
Int64 converts x to an int64 or returns an error if x cannot fit precisely into an int64.
func (Dec) IsNegative ¶
IsNegative returns true if the decimal is negative.
func (Dec) IsPositive ¶
IsPositive returns true if the decimal is positive.
func (Dec) Mul ¶
Mul returns a new Dec with value `x*y` (formatted as decimal128, with 34 digit precision) without mutating any argument and error if there is an overflow.
func (Dec) MulExact ¶
MulExact returns a new dec with value x * y. The product must not round or ErrUnexpectedRounding will be returned.
func (Dec) NumDecimalPlaces ¶
NumDecimalPlaces returns the number of decimal places in x.
func (Dec) Quo ¶
Quo returns a new Dec with value `x/y` (formatted as decimal128, 34 digit precision) without mutating any argument and error if there is an overflow.
func (Dec) QuoExact ¶
QuoExact is a version of Quo that returns ErrUnexpectedRounding if any rounding occurred.
func (Dec) QuoInteger ¶
QuoInteger returns a new integral Dec with value `x/y` (formatted as decimal128, with 34 digit precision) without mutating any argument and error if there is an overflow.
func (Dec) Reduce ¶
Reduce returns a copy of x with all trailing zeros removed and the number of trailing zeros removed.
func (Dec) Rem ¶
Rem returns the integral remainder from `x/y` (formatted as decimal128, with 34 digit precision) without mutating any argument and error if the integer part of x/y cannot fit in 34 digit precision
func (Dec) SdkIntTrim ¶
SdkIntTrim rounds decimal number to the integer towards zero and converts it to `sdkmath.Int`. Panics if x is bigger the SDK Int max value