bn

package
v0.0.0-...-6378e9d Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2024 License: AGPL-3.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
const MaxDecPointPrecision = math.MaxUint8

Variables

This section is empty.

Functions

This section is empty.

Types

type DecFixedPointNumber

type DecFixedPointNumber struct {
	// contains filtered or unexported fields
}

DecFixedPointNumber represents a fixed-point decimal number with fixed precision.

Internally, the number is stored as a *big.Int, scaled by 10^prec.

func DecFixedPoint

func DecFixedPoint(x any, n uint8) *DecFixedPointNumber

DecFixedPoint returns the DecFixedPointNumber representation of x.

The argument x can be one of the following types: - IntNumber - FloatNumber - DecFixedPointNumber - DecFloatPointNumber - big.Int - big.Float - int, int8, int16, int32, int64 - uint, uint8, uint16, uint32, uint64 - float32, float64 - string - a string accepted by big.Float.SetString, otherwise it returns nil

If the input value is not one of the supported types, nil is returned.

func DecFixedPointFromRawBigInt

func DecFixedPointFromRawBigInt(x *big.Int, n uint8) *DecFixedPointNumber

DecFixedPointFromRawBigInt returns the DecFixedPointNumber of x assuming it is already scaled by 10^prec.

func (*DecFixedPointNumber) Abs

Abs returns the absolute number of x.

func (*DecFixedPointNumber) Add

Add adds y to the number and returns the result.

Before the addition, the precision of x and y is increased to the larger of the two precisions. The precision of the result is set back to the precision of x.

func (*DecFixedPointNumber) BigFloat

func (x *DecFixedPointNumber) BigFloat() *big.Float

BigFloat returns the *big.Float representation of the DecFixedPointNumber.

func (*DecFixedPointNumber) BigInt

func (x *DecFixedPointNumber) BigInt() *big.Int

BigInt returns the *big.Int representation of the DecFixedPointNumber.

func (*DecFixedPointNumber) Cmp

Cmp compares x and y and returns:

-1 if x <  y
 0 if x == y
+1 if x >  y

func (*DecFixedPointNumber) DecFloatPoint

func (x *DecFixedPointNumber) DecFloatPoint() *DecFloatPointNumber

DecFloatPoint returns the DecFloatPointNumber representation of the DecFixedPointNumber.

func (*DecFixedPointNumber) Div

Div divides the number by y and returns the result.

Division by zero panics.

Before the division, the precision of x and y is increased to the precision of the larger of the two values plus decGuardDigits. The precision of the result is set back to the precision of x.

To use a different precision, use DivPrec.

func (*DecFixedPointNumber) DivPrec

DivPrec divides the number by y and returns the result.

Division by zero panics.

Before the division, the precision of x and y is increased to the given precision. The precision of the result is set back to the precision of x.

func (*DecFixedPointNumber) Float

func (x *DecFixedPointNumber) Float() *FloatNumber

Float returns the Float representation of the DecFixedPointNumber.

func (*DecFixedPointNumber) Int

func (x *DecFixedPointNumber) Int() *IntNumber

Int returns the Int representation of the DecFixedPointNumber.

func (*DecFixedPointNumber) Inv

Inv returns the inverse value of the number of x.

If x is zero, Inv panics.

func (*DecFixedPointNumber) MarshalBinary

func (x *DecFixedPointNumber) MarshalBinary() (data []byte, err error)

MarshalBinary implements the encoding.BinaryMarshaler interface.

func (*DecFixedPointNumber) Mul

Mul multiplies the number by y and returns the result.

Before the multiplication, the precision of x and y is increased to the sum of the precisions of x and y. The precision of the result is set back to the precision of x.

func (*DecFixedPointNumber) Neg

Neg returns the negative number of x.

func (*DecFixedPointNumber) Prec

func (x *DecFixedPointNumber) Prec() uint8

Prec returns the precision of the DecFixedPointNumber.

Prec is the number of decimal digits in the fractional part.

func (*DecFixedPointNumber) RawBigInt

func (x *DecFixedPointNumber) RawBigInt() *big.Int

RawBigInt returns the internal *big.Int representation of the DecFixedPointNumber without scaling.

func (*DecFixedPointNumber) SetPrec

func (x *DecFixedPointNumber) SetPrec(prec uint8) *DecFixedPointNumber

SetPrec returns a new DecFixedPointNumber with the given precision.

Prec is the number of decimal digits in the fractional part.

If precision is decreased, the number is rounded.

func (*DecFixedPointNumber) Sign

func (x *DecFixedPointNumber) Sign() int

Sign returns:

-1 if x <  0
 0 if x == 0
+1 if x >  0

func (*DecFixedPointNumber) String

func (x *DecFixedPointNumber) String() string

String returns the 10-base string representation of the DecFixedPointNumber.

func (*DecFixedPointNumber) Sub

Sub subtracts y from the number and returns the result.

Before the subtraction, the precision of x and y is increased to the larger of the two precisions. The precision of the result is set back to the precision of x.

func (*DecFixedPointNumber) Text

func (x *DecFixedPointNumber) Text(format byte, prec int) string

Text returns the string representation of the DecFixedPointNumber. The format and prec arguments are the same as in big.Float.Text.

For any format other than 'f' and prec of -1, the result may be rounded.

func (*DecFixedPointNumber) UnmarshalBinary

func (x *DecFixedPointNumber) UnmarshalBinary(data []byte) error

UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.

type DecFloatPointNumber

type DecFloatPointNumber struct {
	// contains filtered or unexported fields
}

DecFloatPointNumber represents a decimal floating-point number.

Unlike the DecFixedPointNumber, the precision of the DecFloatPointNumber is adjusted dynamically to fit the value.

func DecFloatPoint

func DecFloatPoint(x any) *DecFloatPointNumber

DecFloatPoint returns the DecFloatPointNumber representation of x.

The argument x can be one of the following types: - IntNumber - FloatNumber - DecFixedPointNumber - DecFloatPointNumber - big.Int - big.Float - int, int8, int16, int32, int64 - uint, uint8, uint16, uint32, uint64 - float32, float64 - string - a string accepted by big.Float.SetString, otherwise it returns nil

If the input value is not one of the supported types, nil is returned.

func (*DecFloatPointNumber) Abs

Abs returns the absolute number of x.

func (*DecFloatPointNumber) Add

Add adds y to the number and returns the result.

func (*DecFloatPointNumber) BigFloat

func (x *DecFloatPointNumber) BigFloat() *big.Float

BigFloat returns the *big.Float representation of the DecFloatPointNumber.

func (*DecFloatPointNumber) BigInt

func (x *DecFloatPointNumber) BigInt() *big.Int

BigInt returns the *big.Int representation of the DecFloatPointNumber.

func (*DecFloatPointNumber) Cmp

Cmp compares the number to y and returns:

-1 if x <  0
 0 if x == 0
+1 if x >  0

func (*DecFloatPointNumber) DecFixedPoint

func (x *DecFloatPointNumber) DecFixedPoint(n uint8) *DecFixedPointNumber

DecFixedPoint returns the DecFixedPointNumber representation of the Float.

func (*DecFloatPointNumber) Div

Div divides the number by y and returns the result.

Division by zero panics.

During division, the precision is increased by divPrecisionIncrease and then lowered to the smallest possible that still fits the result.

func (*DecFloatPointNumber) DivPrec

DivPrec divides the number by y and returns the result.

Division by zero panics.

During division, the precision is set to prec and then lowered to the smallest possible that still fits the result.

func (*DecFloatPointNumber) Float

func (x *DecFloatPointNumber) Float() *FloatNumber

Float returns the Float representation of the DecFloatPointNumber.

func (*DecFloatPointNumber) Int

func (x *DecFloatPointNumber) Int() *IntNumber

Int returns the Int representation of the DecFloatPointNumber.

func (*DecFloatPointNumber) Inv

Inv returns the inverse value of the number of x.

If x is zero, Inv panics.

During inversion, the precision is increased by divPrecisionIncrease and then lowered to the smallest possible that still fits the result.

func (*DecFloatPointNumber) MarshalBinary

func (x *DecFloatPointNumber) MarshalBinary() (data []byte, err error)

MarshalBinary implements the encoding.BinaryMarshaler interface.

func (*DecFloatPointNumber) Mul

Mul multiplies the number by y and returns the result.

func (*DecFloatPointNumber) Neg

Neg returns the negative number of x.

func (*DecFloatPointNumber) Prec

func (x *DecFloatPointNumber) Prec() uint8

Prec returns the precision of the DecFloatPointNumber.

Prec is the number of decimal digits in the fractional part.

func (*DecFloatPointNumber) SetPrec

SetPrec returns a new DecFloatPointNumber with the given precision.

Prec is the number of decimal digits in the fractional part.

func (*DecFloatPointNumber) Sign

func (x *DecFloatPointNumber) Sign() int

Sign returns:

-1 if x <  0
 0 if x == 0
+1 if x >  0

func (*DecFloatPointNumber) String

func (x *DecFloatPointNumber) String() string

String returns the 10-base string representation of the DecFloatPointNumber.

func (*DecFloatPointNumber) Sub

Sub subtracts y from the number and returns the result.

func (*DecFloatPointNumber) Text

func (x *DecFloatPointNumber) Text(format byte, prec int) string

Text returns the string representation of the DecFixedPointNumber. The format and prec arguments are the same as in big.Float.Text.

For any format other than 'f' and prec of -1, the result may be rounded.

func (*DecFloatPointNumber) UnmarshalBinary

func (x *DecFloatPointNumber) UnmarshalBinary(data []byte) error

UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.

type FloatNumber

type FloatNumber struct {
	// contains filtered or unexported fields
}

FloatNumber represents a floating-point number.

func Float

func Float(x any) *FloatNumber

Float returns the FloatNumber representation of x.

The argument x can be one of the following types: - IntNumber - FloatNumber - DecFixedPointNumber - DecFloatPointNumber - big.Int - big.Float - int, int8, int16, int32, int64 - uint, uint8, uint16, uint32, uint64 - float32, float64 - string - a string accepted by big.Float.SetString, otherwise it returns nil

If the input value is not one of the supported types, nil is returned.

func (*FloatNumber) Abs

func (x *FloatNumber) Abs() *FloatNumber

Abs returns the absolute number of x.

func (*FloatNumber) Add

func (x *FloatNumber) Add(y *FloatNumber) *FloatNumber

Add adds y to the number and returns the result.

func (*FloatNumber) BigFloat

func (x *FloatNumber) BigFloat() *big.Float

BigFloat returns the *big.Float representation of the Float.

func (*FloatNumber) BigInt

func (x *FloatNumber) BigInt() *big.Int

BigInt returns the *big.Int representation of the Float.

The fractional part is discarded and the number is rounded.

func (*FloatNumber) Cmp

func (x *FloatNumber) Cmp(y *FloatNumber) int

Cmp compares the number to y and returns:

-1 if x <  0
 0 if x == 0
+1 if x >  0

func (*FloatNumber) DecFixedPoint

func (x *FloatNumber) DecFixedPoint(n uint8) *DecFixedPointNumber

DecFixedPoint returns the DecFixedPointNumber representation of the Float.

func (*FloatNumber) Div

func (x *FloatNumber) Div(y *FloatNumber) *FloatNumber

Div divides the number by y and returns the result.

func (*FloatNumber) Int

func (x *FloatNumber) Int() *IntNumber

Int returns the IntNumber representation of the Float.

The fractional part is discarded and the number is rounded.

func (*FloatNumber) Inv

func (x *FloatNumber) Inv() *FloatNumber

Inv returns the inverse number of x.

func (*FloatNumber) IsInf

func (x *FloatNumber) IsInf() bool

IsInf reports whether the number is an infinity.

func (*FloatNumber) Mul

func (x *FloatNumber) Mul(y *FloatNumber) *FloatNumber

Mul multiplies the number by y and returns the result.

func (*FloatNumber) Neg

func (x *FloatNumber) Neg() *FloatNumber

Neg returns the negative number of x.

func (*FloatNumber) Precision

func (x *FloatNumber) Precision() uint

Precision returns the precision of the Float.

It is wrapper around big.Float.Prec.

func (*FloatNumber) SetPrecision

func (x *FloatNumber) SetPrecision(prec uint) *FloatNumber

SetPrecision sets the precision of the Float.

It is wrapper around big.Float.SetPrec.

func (*FloatNumber) Sign

func (x *FloatNumber) Sign() int

Sign returns:

-1 if x <  0
 0 if x == 0
+1 if x >  0

func (*FloatNumber) Sqrt

func (x *FloatNumber) Sqrt() *FloatNumber

Sqrt returns the square root of the number.

func (*FloatNumber) String

func (x *FloatNumber) String() string

String returns the 10-base string representation of the Float.

func (*FloatNumber) Sub

func (x *FloatNumber) Sub(y *FloatNumber) *FloatNumber

Sub subtracts y from the number and returns the result.

func (*FloatNumber) Text

func (x *FloatNumber) Text(format byte, prec int) string

Text returns the string representation of the Float. The format and prec arguments are the same as in big.Float.Text.

type IntNumber

type IntNumber struct {
	// contains filtered or unexported fields
}

IntNumber represents an arbitrary-precision integer.

func Int

func Int(x any) *IntNumber

Int returns the IntNumber representation of x.

The argument x can be one of the following types: - IntNumber - FloatNumber - DecFixedPointNumber - DecFloatPointNumber - big.Int - big.Float - int, int8, int16, int32, int64 - uint, uint8, uint16, uint32, uint64 - float32, float64 - string - a string accepted by big.Int.SetString, otherwise it returns nil - []byte - a byte slice accepted by big.Int.SetBytes, otherwise it returns nil

If the input value is not one of the supported types, nil is returned.

func (*IntNumber) Abs

func (x *IntNumber) Abs() *IntNumber

Abs returns the absolute number of x.

func (*IntNumber) Add

func (x *IntNumber) Add(y *IntNumber) *IntNumber

Add adds y to the number and returns the result.

func (*IntNumber) BigFloat

func (x *IntNumber) BigFloat() *big.Float

BigFloat returns the *big.Float representation of the Int.

func (*IntNumber) BigInt

func (x *IntNumber) BigInt() *big.Int

BigInt returns the *big.Int representation of the Int.

func (*IntNumber) Cmp

func (x *IntNumber) Cmp(y *IntNumber) int

Cmp compares the number to y and returns:

-1 if x <  0
 0 if x == 0
+1 if x >  0

func (*IntNumber) DecFixedPoint

func (x *IntNumber) DecFixedPoint(n uint8) *DecFixedPointNumber

DecFixedPoint returns the DecFixedPoint representation of the Int.

func (*IntNumber) DecFloatPoint

func (x *IntNumber) DecFloatPoint() *DecFloatPointNumber

DecFloatPoint returns the DecFloatPoint representation of the Int.

func (*IntNumber) Div

func (x *IntNumber) Div(y *IntNumber) *IntNumber

Div divides the number by y and returns the result.

func (*IntNumber) DivRoundUp

func (x *IntNumber) DivRoundUp(y *IntNumber) *IntNumber

DivRoundUp divides the number by y and returns the result rounded up.

func (*IntNumber) Float

func (x *IntNumber) Float() *FloatNumber

Float returns the Float representation of the Int.

func (*IntNumber) Lsh

func (x *IntNumber) Lsh(n uint) *IntNumber

Lsh returns the number shifted left by n bits.

func (*IntNumber) Mul

func (x *IntNumber) Mul(y *IntNumber) *IntNumber

Mul multiplies the number by y and returns the result.

func (*IntNumber) Neg

func (x *IntNumber) Neg() *IntNumber

Neg returns the negative number of x.

func (*IntNumber) Pow

func (x *IntNumber) Pow(y *IntNumber) *IntNumber

Pow returns the number raised to the power of y.

func (*IntNumber) Rem

func (x *IntNumber) Rem(y *IntNumber) *IntNumber

Rem returns the remainder of the division of the number by y.

func (*IntNumber) Rsh

func (x *IntNumber) Rsh(n uint) *IntNumber

Rsh returns the number shifted right by n bits.

func (*IntNumber) Sign

func (x *IntNumber) Sign() int

Sign returns:

-1 if i <  0
 0 if i == 0
+1 if i >  0

func (*IntNumber) Sqrt

func (x *IntNumber) Sqrt() *IntNumber

Sqrt returns the square root of the number.

func (*IntNumber) String

func (x *IntNumber) String() string

String returns the 10-base string representation of the Int.

func (*IntNumber) Sub

func (x *IntNumber) Sub(y *IntNumber) *IntNumber

Sub subtracts y from the number and returns the result.

func (*IntNumber) Text

func (x *IntNumber) Text(base int) string

Text returns the string representation of the Int in the given base.

Jump to

Keyboard shortcuts

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