Documentation ¶
Overview ¶
Package math provides helper functions for doing mathematical calculations and parsing for the ecocredit module.
Index ¶
- Variables
- type Dec
- func Add(x Dec, y Dec) (Dec, error)
- 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) Cmp(y Dec) int
- func (x Dec) Equal(y Dec) bool
- func (x Dec) Int64() (int64, error)
- 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) NumDecimalPlaces() uint32
- func (x Dec) Quo(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) String() string
- func (x Dec) Sub(y Dec) (Dec, error)
Constants ¶
This section is empty.
Variables ¶
var ErrInvalidDecString = errors.Register(mathCodespace, 1, "invalid decimal string")
Functions ¶
This section is empty.
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 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) 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) IsNegative ¶
func (Dec) IsPositive ¶
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) 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) 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 removes trailing zeros from x and returns x and the number of zeros removed.