math

package
v2.3.1 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2023 License: Apache-2.0 Imports: 7 Imported by: 14

Documentation

Overview

Package math provides helper functions for doing mathematical calculations and parsing for the ecocredit module.

Index

Constants

View Source
const (
	GreaterThan = 1
	LessThan    = -1
	EqualTo     = 0
)

constants for more convenient intent behind dec.Cmp values.

Variables

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

func MatchEq

func MatchEq(dec Dec) gomock.Matcher

MatchEq is a gomock.Matcher which compares a decimal value to an expected value in gomock calls.

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 Add

func Add(x Dec, y Dec) (Dec, error)

Add adds x and y

func NewDecFinite

func NewDecFinite(coeff int64, exp int32) Dec

NewDecFinite returns a decimal with a value of coeff * 10^exp.

func NewDecFromInt64

func NewDecFromInt64(x int64) Dec

func NewDecFromString

func NewDecFromString(s string) (Dec, error)

func NewNonNegativeDecFromString

func NewNonNegativeDecFromString(s string) (Dec, error)

func NewNonNegativeFixedDecFromString

func NewNonNegativeFixedDecFromString(s string, max uint32) (Dec, error)

func NewPositiveDecFromString

func NewPositiveDecFromString(s string) (Dec, error)

func NewPositiveFixedDecFromString

func NewPositiveFixedDecFromString(s string, max uint32) (Dec, error)

func SafeAddBalance

func SafeAddBalance(x Dec, y Dec) (Dec, error)

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

func SafeSubBalance(x Dec, y Dec) (Dec, error)

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

func SubNonNegative(x Dec, y Dec) (Dec, error)

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

func (x Dec) Add(y Dec) (Dec, error)

Add returns a new Dec with value `x+y` without mutating any argument and error if there is an overflow.

func (Dec) BigInt

func (x Dec) BigInt() (*big.Int, error)

BigInt converts x to a *big.Int or returns an error if x cannot fit precisely into an *big.Int.

func (Dec) Cmp

func (x Dec) Cmp(y Dec) int

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

func (x Dec) Equal(y Dec) bool

func (Dec) Int64

func (x Dec) Int64() (int64, error)

Int64 converts x to an int64 or returns an error if x cannot fit precisely into an int64.

func (Dec) IsFinite

func (x Dec) IsFinite() bool

IsFinite returns true if the decimal is finite.

func (Dec) IsNegative

func (x Dec) IsNegative() bool

IsNegative returns true if the decimal is negative.

func (Dec) IsPositive

func (x Dec) IsPositive() bool

IsPositive returns true if the decimal is positive.

func (Dec) IsZero

func (x Dec) IsZero() bool

IsZero returns true if the decimal is zero.

func (Dec) Mul

func (x Dec) Mul(y Dec) (Dec, error)

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

func (x Dec) MulExact(y Dec) (Dec, error)

MulExact returns a new dec with value x * y. The product must not round or ErrUnexpectedRounding will be returned.

func (Dec) NumDecimalPlaces

func (x Dec) NumDecimalPlaces() uint32

NumDecimalPlaces returns the number of decimal places in x.

func (Dec) Quo

func (x Dec) Quo(y Dec) (Dec, error)

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

func (x Dec) QuoExact(y Dec) (Dec, error)

QuoExact is a version of Quo that returns ErrUnexpectedRounding if any rounding occurred.

func (Dec) QuoInteger

func (x Dec) QuoInteger(y Dec) (Dec, error)

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

func (x Dec) Reduce() (Dec, int)

Reduce returns a copy of x with all trailing zeros removed and the number of trailing zeros removed.

func (Dec) Rem

func (x Dec) Rem(y Dec) (Dec, error)

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

func (x Dec) SdkIntTrim() sdkmath.Int

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

func (Dec) String

func (x Dec) String() string

func (Dec) Sub

func (x Dec) Sub(y Dec) (Dec, error)

Sub returns a new Dec with value `x-y` without mutating any argument and error if there is an overflow.

Jump to

Keyboard shortcuts

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