math

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2021 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

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

Index

Constants

This section is empty.

Variables

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

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

Add adds x and y

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

func (Dec) IsNegative

func (x Dec) IsNegative() bool

func (Dec) IsPositive

func (x Dec) IsPositive() bool

func (Dec) IsZero

func (x Dec) IsZero() bool

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) 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) 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 removes trailing zeros from x and returns x and the number of 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) 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