math

package
v0.48.86 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2023 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

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

Index

Constants

This section is empty.

Variables

This section is empty.

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)

NewDecFromString returns a new Dec from a string It only support finite numbers, not NaN, +Inf, -Inf

func NewNonNegativeDecFromString

func NewNonNegativeDecFromString(s string) (Dec, error)

func NewPositiveDecFromString

func NewPositiveDecFromString(s string) (Dec, error)

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

func (Dec) Int64

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

func (Dec) IsEqual

func (x Dec) IsEqual(y Dec) bool

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