bn

package
v0.16.1 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2023 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 added in v0.12.0

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 added in v0.12.0

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 added in v0.12.0

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 added in v0.12.0

Abs returns the absolute number of x.

func (*DecFixedPointNumber) Add added in v0.12.0

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 added in v0.12.0

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

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

func (*DecFixedPointNumber) BigInt added in v0.12.0

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

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

func (*DecFixedPointNumber) Cmp added in v0.12.0

Cmp compares x and y and returns:

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

func (*DecFixedPointNumber) DecFloatPoint added in v0.16.1

func (x *DecFixedPointNumber) DecFloatPoint() *DecFloatPointNumber

DecFloatPoint returns the DecFloatPointNumber representation of the DecFixedPointNumber.

func (*DecFixedPointNumber) Div added in v0.12.0

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 added in v0.16.1

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 added in v0.12.0

func (x *DecFixedPointNumber) Float() *FloatNumber

Float returns the Float representation of the DecFixedPointNumber.

func (*DecFixedPointNumber) Int added in v0.12.0

func (x *DecFixedPointNumber) Int() *IntNumber

Int returns the Int representation of the DecFixedPointNumber.

func (*DecFixedPointNumber) Inv added in v0.16.1

Inv returns the inverse value of the number of x.

If x is zero, Inv panics.

func (*DecFixedPointNumber) MarshalBinary added in v0.12.0

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

MarshalBinary implements the encoding.BinaryMarshaler interface.

func (*DecFixedPointNumber) Mul added in v0.12.0

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 added in v0.12.0

Neg returns the negative number of x.

func (*DecFixedPointNumber) Prec added in v0.16.1

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 added in v0.12.0

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

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

func (*DecFixedPointNumber) SetPrec added in v0.16.1

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 added in v0.12.0

func (x *DecFixedPointNumber) Sign() int

Sign returns:

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

func (*DecFixedPointNumber) String added in v0.12.0

func (x *DecFixedPointNumber) String() string

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

func (*DecFixedPointNumber) Sub added in v0.12.0

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 added in v0.12.0

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 added in v0.12.0

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

UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.

type DecFloatPointNumber added in v0.16.1

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 added in v0.16.1

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 added in v0.16.1

Abs returns the absolute number of x.

func (*DecFloatPointNumber) Add added in v0.16.1

Add adds y to the number and returns the result.

func (*DecFloatPointNumber) BigFloat added in v0.16.1

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

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

func (*DecFloatPointNumber) BigInt added in v0.16.1

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

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

func (*DecFloatPointNumber) Cmp added in v0.16.1

Cmp compares the number to y and returns:

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

func (*DecFloatPointNumber) DecFixedPoint added in v0.16.1

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

DecFixedPoint returns the DecFixedPointNumber representation of the Float.

func (*DecFloatPointNumber) Div added in v0.16.1

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 added in v0.16.1

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 added in v0.16.1

func (x *DecFloatPointNumber) Float() *FloatNumber

Float returns the Float representation of the DecFloatPointNumber.

func (*DecFloatPointNumber) Int added in v0.16.1

func (x *DecFloatPointNumber) Int() *IntNumber

Int returns the Int representation of the DecFloatPointNumber.

func (*DecFloatPointNumber) Inv added in v0.16.1

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 added in v0.16.1

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

MarshalBinary implements the encoding.BinaryMarshaler interface.

func (*DecFloatPointNumber) Mul added in v0.16.1

Mul multiplies the number by y and returns the result.

func (*DecFloatPointNumber) Neg added in v0.16.1

Neg returns the negative number of x.

func (*DecFloatPointNumber) Prec added in v0.16.1

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 added in v0.16.1

SetPrec returns a new DecFloatPointNumber with the given precision.

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

func (*DecFloatPointNumber) Sign added in v0.16.1

func (x *DecFloatPointNumber) Sign() int

Sign returns:

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

func (*DecFloatPointNumber) String added in v0.16.1

func (x *DecFloatPointNumber) String() string

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

func (*DecFloatPointNumber) Sub added in v0.16.1

Sub subtracts y from the number and returns the result.

func (*DecFloatPointNumber) Text added in v0.16.1

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 added in v0.16.1

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

UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.

type FloatNumber added in v0.10.2

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 added in v0.10.2

func (x *FloatNumber) Abs() *FloatNumber

Abs returns the absolute number of x.

func (*FloatNumber) Add added in v0.10.2

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

Add adds y to the number and returns the result.

func (*FloatNumber) BigFloat added in v0.10.2

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

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

func (*FloatNumber) BigInt added in v0.10.2

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 added in v0.10.2

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 added in v0.12.0

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

DecFixedPoint returns the DecFixedPointNumber representation of the Float.

func (*FloatNumber) Div added in v0.10.2

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

Div divides the number by y and returns the result.

func (*FloatNumber) Int added in v0.10.2

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 added in v0.10.2

func (x *FloatNumber) Inv() *FloatNumber

Inv returns the inverse number of x.

func (*FloatNumber) IsInf added in v0.10.2

func (x *FloatNumber) IsInf() bool

IsInf reports whether the number is an infinity.

func (*FloatNumber) Mul added in v0.10.2

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

Mul multiplies the number by y and returns the result.

func (*FloatNumber) Neg added in v0.10.2

func (x *FloatNumber) Neg() *FloatNumber

Neg returns the negative number of x.

func (*FloatNumber) Precision added in v0.16.1

func (x *FloatNumber) Precision() uint

Precision returns the precision of the Float.

It is wrapper around big.Float.Prec.

func (*FloatNumber) SetPrecision added in v0.16.1

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

SetPrecision sets the precision of the Float.

It is wrapper around big.Float.SetPrec.

func (*FloatNumber) Sign added in v0.10.2

func (x *FloatNumber) Sign() int

Sign returns:

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

func (*FloatNumber) Sqrt added in v0.10.2

func (x *FloatNumber) Sqrt() *FloatNumber

Sqrt returns the square root of the number.

func (*FloatNumber) String added in v0.10.2

func (x *FloatNumber) String() string

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

func (*FloatNumber) Sub added in v0.10.2

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

Sub subtracts y from the number and returns the result.

func (*FloatNumber) Text added in v0.10.2

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 added in v0.10.2

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 added in v0.10.2

func (x *IntNumber) Abs() *IntNumber

Abs returns the absolute number of x.

func (*IntNumber) Add added in v0.10.2

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

Add adds y to the number and returns the result.

func (*IntNumber) BigFloat added in v0.10.2

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

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

func (*IntNumber) BigInt added in v0.10.2

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

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

func (*IntNumber) Cmp added in v0.10.2

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 added in v0.12.0

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

DecFixedPoint returns the DecFixedPoint representation of the Int.

func (*IntNumber) DecFloatPoint added in v0.16.1

func (x *IntNumber) DecFloatPoint() *DecFloatPointNumber

DecFloatPoint returns the DecFloatPoint representation of the Int.

func (*IntNumber) Div added in v0.10.2

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

Div divides the number by y and returns the result.

func (*IntNumber) DivRoundUp added in v0.10.2

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

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

func (*IntNumber) Float added in v0.10.2

func (x *IntNumber) Float() *FloatNumber

Float returns the Float representation of the Int.

func (*IntNumber) Lsh added in v0.10.2

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

Lsh returns the number shifted left by n bits.

func (*IntNumber) Mul added in v0.10.2

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

Mul multiplies the number by y and returns the result.

func (*IntNumber) Neg added in v0.10.2

func (x *IntNumber) Neg() *IntNumber

Neg returns the negative number of x.

func (*IntNumber) Pow added in v0.10.2

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

Pow returns the number raised to the power of y.

func (*IntNumber) Rem added in v0.10.2

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

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

func (*IntNumber) Rsh added in v0.10.2

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

Rsh returns the number shifted right by n bits.

func (*IntNumber) Sign added in v0.10.2

func (x *IntNumber) Sign() int

Sign returns:

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

func (*IntNumber) Sqrt added in v0.10.2

func (x *IntNumber) Sqrt() *IntNumber

Sqrt returns the square root of the number.

func (*IntNumber) String added in v0.10.2

func (x *IntNumber) String() string

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

func (*IntNumber) Sub added in v0.10.2

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

Sub subtracts y from the number and returns the result.

func (*IntNumber) Text added in v0.10.2

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