Documentation ¶
Overview ¶
Package fixedpoint provides a shopspring/decimal wrapper library for fixed point arithmetic operations.
Index ¶
- Variables
- func SetDivPrecision(precision int32) (old int32)
- func SetPrecision(precision int32) (old int32)
- type FixedPoint
- func Empty() FixedPoint
- func MustSafeFromString(s string) FixedPoint
- func New() FixedPoint
- func NewFromBigFloat(bf *big.Float) FixedPoint
- func NewFromBigInt(bi *big.Int) FixedPoint
- func NewFromBigIntExp(bi *big.Int, exp int32) FixedPoint
- func NewFromDecimal(d decimal.Decimal) FixedPoint
- func NewFromFloat32(f32 float32) FixedPoint
- func NewFromFloat64(f64 float64) FixedPoint
- func NewFromInt32(i32 int32) FixedPoint
- func NewFromInt64(i64 int64) FixedPoint
- func NewFromNumeric(numeric pgtype.Numeric) (FixedPoint, error)
- func NewFromString(s string) (FixedPoint, error)
- func NewFromUint256(u *uint256.Int) FixedPoint
- func PowerOfTen[T constraints.Signed](n T) FixedPoint
- func SafeNewFromString(s string) (FixedPoint, error)
- func Zero() FixedPoint
- func (f FixedPoint) Abs() FixedPoint
- func (f FixedPoint) Add(a FixedPoint) FixedPoint
- func (f FixedPoint) BigFloat() *big.Float
- func (f FixedPoint) BigInt() *big.Int
- func (f FixedPoint) Cmp(a FixedPoint) int
- func (f FixedPoint) Copy() FixedPoint
- func (f FixedPoint) Decimal() decimal.Decimal
- func (f FixedPoint) Div(a FixedPoint) FixedPoint
- func (f FixedPoint) Equal(a FixedPoint) bool
- func (f FixedPoint) Float64() (value float64, exact bool)
- func (f FixedPoint) InexactFloat64() float64
- func (f FixedPoint) IsInteger() bool
- func (f FixedPoint) IsNegative() bool
- func (f FixedPoint) IsOverPrecision(precision int) bool
- func (f FixedPoint) IsPositive() bool
- func (f FixedPoint) IsValid() bool
- func (f FixedPoint) IsZero() bool
- func (f FixedPoint) MarshalBinary() ([]byte, error)
- func (f FixedPoint) MarshalJSON() ([]byte, error)
- func (f FixedPoint) MarshalMsgpack() ([]byte, error)
- func (f *FixedPoint) MarshalText() ([]byte, error)
- func (f FixedPoint) Mod(a FixedPoint) FixedPoint
- func (f FixedPoint) Mul(a FixedPoint) FixedPoint
- func (f FixedPoint) Neg() FixedPoint
- func (f FixedPoint) NullDecimal() decimal.NullDecimal
- func (f FixedPoint) NumDigits() int
- func (f FixedPoint) Numeric() (pgtype.Numeric, error)
- func (f FixedPoint) PowInt(a FixedPoint) FixedPoint
- func (f *FixedPoint) Scan(value interface{}) error
- func (f FixedPoint) Sign() int
- func (f FixedPoint) String() string
- func (f FixedPoint) StringBankWithPrecision(precision int32) string
- func (f FixedPoint) StringFixed() string
- func (f FixedPoint) StringFixedBank() string
- func (f FixedPoint) StringWithPrecision(precision int32) string
- func (f FixedPoint) Sub(a FixedPoint) FixedPoint
- func (f FixedPoint) Uint256() *uint256.Int
- func (f *FixedPoint) UnmarshalBinary(data []byte) error
- func (f *FixedPoint) UnmarshalJSON(decimalBytes []byte) error
- func (f *FixedPoint) UnmarshalMsgpack(decimalBytes []byte) error
- func (f *FixedPoint) UnmarshalText(text []byte) error
- func (f FixedPoint) Value() (driver.Value, error)
- type FixedPointArray
Constants ¶
This section is empty.
Variables ¶
var ( // Precision is fixedpoint output precision Precision int32 = defualtPrecision // DivPrecision is div operation precision. // default is 36, which is 2 * Precision DivPrecision int32 = 2 * defualtPrecision )
var ( Min = NewFromFloat64(1e-36) // smallest possible FixedPoint (1e-36) Max = NewFromFloat64(math.MaxFloat64) // largest possible FixedPoint (1.7976931348623157e+308) )
Functions ¶
func SetDivPrecision ¶
SetDivPrecision set div operation precision (default is 36), returns old div precission.
func SetPrecision ¶
SetPrecision set fixedpoint output precision (default is 18), returns old precission. div precision will be set to 2 * precision.
Types ¶
type FixedPoint ¶
type FixedPoint struct {
// contains filtered or unexported fields
}
func MustSafeFromString ¶
func MustSafeFromString(s string) FixedPoint
MustSafeNewFromString safe to use when input can be empty string. Will panic if error
func NewFromBigFloat ¶
func NewFromBigFloat(bf *big.Float) FixedPoint
NewFromBigFloat returns a new FixedPoint from a big.Float.
func NewFromBigInt ¶
func NewFromBigInt(bi *big.Int) FixedPoint
NewFromBigInt returns a new FixedPoint from a big.Int.
func NewFromBigIntExp ¶
func NewFromBigIntExp(bi *big.Int, exp int32) FixedPoint
NewFromBigIntExp returns a new FixedPoint from a big.Int with an exponent.
func NewFromDecimal ¶
func NewFromDecimal(d decimal.Decimal) FixedPoint
NewFromDecimal returns a new FixedPoint from a decimal.Decimal.
func NewFromFloat32 ¶
func NewFromFloat32(f32 float32) FixedPoint
NewFromFloat32 returns a new FixedPoint from a float32.
func NewFromFloat64 ¶
func NewFromFloat64(f64 float64) FixedPoint
NewFromFloat64 returns a new FixedPoint from a float64.
func NewFromInt32 ¶
func NewFromInt32(i32 int32) FixedPoint
NewFromInt32 returns a new FixedPoint from an int32.
func NewFromInt64 ¶
func NewFromInt64(i64 int64) FixedPoint
NewFromInt64 returns a new FixedPoint from an int64.
func NewFromNumeric ¶
func NewFromNumeric(numeric pgtype.Numeric) (FixedPoint, error)
NewFromNumeric returns a new FixedPoint from a pgtype.Numeric.
func NewFromString ¶
func NewFromString(s string) (FixedPoint, error)
NewFromString safely converts a string to a fixedpoint.FixedPoint.
func NewFromUint256 ¶
func NewFromUint256(u *uint256.Int) FixedPoint
NewFromUint256 returns a new FixedPoint from a uint256.Int.
func PowerOfTen ¶
func PowerOfTen[T constraints.Signed](n T) FixedPoint
PowerOfTen optimized arithmetic performance for 10^n.
func SafeNewFromString ¶
func SafeNewFromString(s string) (FixedPoint, error)
SafeNewFromString safe to use when input can be empty string
func (FixedPoint) Abs ¶
func (f FixedPoint) Abs() FixedPoint
func (FixedPoint) Add ¶
func (f FixedPoint) Add(a FixedPoint) FixedPoint
func (FixedPoint) BigFloat ¶
func (f FixedPoint) BigFloat() *big.Float
BigFloat returns a big.Float representation of the FixedPoint.
func (FixedPoint) BigInt ¶
func (f FixedPoint) BigInt() *big.Int
BigInt returns a big.Int representation of the FixedPoint.
func (FixedPoint) Cmp ¶
func (f FixedPoint) Cmp(a FixedPoint) int
Cmp compares the numbers represented by d and d2 and returns:
-1 if d < d2 0 if d == d2 +1 if d > d2
func (FixedPoint) Copy ¶
func (f FixedPoint) Copy() FixedPoint
Copy returns a copy of the FixedPoint with the same value and exponent, but a different pointer to value.
func (FixedPoint) Decimal ¶
func (f FixedPoint) Decimal() decimal.Decimal
Decimal returns the decimal.Decimal representation of the FixedPoint.
func (FixedPoint) Div ¶
func (f FixedPoint) Div(a FixedPoint) FixedPoint
func (FixedPoint) Equal ¶
func (f FixedPoint) Equal(a FixedPoint) bool
func (FixedPoint) Float64 ¶
func (f FixedPoint) Float64() (value float64, exact bool)
Float64 returns a float64 representation of the FixedPoint and a bool indicating whether f represents d exactly.
func (FixedPoint) InexactFloat64 ¶
func (f FixedPoint) InexactFloat64() float64
InexactFloat64 returns a float64 representation of the FixedPoint.
func (FixedPoint) IsInteger ¶
func (f FixedPoint) IsInteger() bool
IsInteger returns true if the FixedPoint is an integer.
Panics if FixedPoint is not valid.
func (FixedPoint) IsNegative ¶
func (f FixedPoint) IsNegative() bool
IsNegative return
true if d < 0 false if d == 0 false if d > 0
func (FixedPoint) IsOverPrecision ¶
func (f FixedPoint) IsOverPrecision(precision int) bool
IsOverPrecision return true if first significant digit is lower than given precision digit
func (FixedPoint) IsPositive ¶
func (f FixedPoint) IsPositive() bool
IsPositive return
true if d > 0 false if d == 0 false if d < 0
func (FixedPoint) IsValid ¶
func (f FixedPoint) IsValid() bool
IsValid returns true if the FixedPoint is valid and can be used.
func (FixedPoint) IsZero ¶
func (f FixedPoint) IsZero() bool
IsZero returns true if the FixedPoint is zero.
Panics if FixedPoint is not valid.
func (FixedPoint) MarshalBinary ¶
func (f FixedPoint) MarshalBinary() ([]byte, error)
MarshalBinary implements the encoding.TextMarshaler interface for text serialization.
func (FixedPoint) MarshalJSON ¶
func (f FixedPoint) MarshalJSON() ([]byte, error)
MarshalJSON implements the json.Marshaler interface for json serialization.
func (FixedPoint) MarshalMsgpack ¶
func (f FixedPoint) MarshalMsgpack() ([]byte, error)
func (*FixedPoint) MarshalText ¶
func (f *FixedPoint) MarshalText() ([]byte, error)
MarshalText implements the encoding.TextMarshaler interface for text serialization.
func (FixedPoint) Mod ¶
func (f FixedPoint) Mod(a FixedPoint) FixedPoint
func (FixedPoint) Mul ¶
func (f FixedPoint) Mul(a FixedPoint) FixedPoint
func (FixedPoint) Neg ¶
func (f FixedPoint) Neg() FixedPoint
func (FixedPoint) NullDecimal ¶
func (f FixedPoint) NullDecimal() decimal.NullDecimal
NullDecimal returns the decimal.NullDecimal representation of the FixedPoint.
func (FixedPoint) NumDigits ¶
func (f FixedPoint) NumDigits() int
NumDigits returns the number of digits of the decimal coefficient (d.Value).
Panics if FixedPoint is not valid.
func (FixedPoint) Numeric ¶
func (f FixedPoint) Numeric() (pgtype.Numeric, error)
Numeric returns a pgtype.Numeric representation of the FixedPoint.
func (FixedPoint) PowInt ¶
func (f FixedPoint) PowInt(a FixedPoint) FixedPoint
PowerInt returns f^a, where a is an Integer only. Panics if a is float/decimal.
Warning: Power with negative exponent is not work normally. E.g. 10^-17, 10^-19, 10^-21 will return 0.
Why we not support PowDecimal. ref: https://github.com/shopspring/decimal/issues/201
func (*FixedPoint) Scan ¶
func (f *FixedPoint) Scan(value interface{}) error
Scan implements the sql.Scanner interface for database deserialization.
func (FixedPoint) Sign ¶
func (f FixedPoint) Sign() int
Sign returns:
-1 if d < 0 0 if d == 0 +1 if d > 0
func (FixedPoint) String ¶
func (f FixedPoint) String() string
String returns the string representation of the decimal with the fixed point.
Example:
d := New(-12345, -3) println(d.String())
Output:
-12.345
func (FixedPoint) StringBankWithPrecision ¶
func (f FixedPoint) StringBankWithPrecision(precision int32) string
StringBankWithPrecision returns a banker rounded fixed-point string with given precision digits after
func (FixedPoint) StringFixed ¶
func (f FixedPoint) StringFixed() string
StringFixed returns a rounded fixed-point string with places digits after the decimal point.
Example:
NewFromFloat64(5.45).StringFixed() // output: "5.450000000000000000" NewFromFloat64(5.5555555555555555555).StringFixed() // output: "5.555555555555555556"
func (FixedPoint) StringFixedBank ¶
func (f FixedPoint) StringFixedBank() string
StringFixedBank returns a banker rounded fixed-point string with places digits after the decimal point.
Example:
NewFromFloat64(5.45).StringFixed() // output: "5.450000000000000000" NewFromFloat64(5.5555555555555555555).StringFixed() // output: "5.555555555555555555"
func (FixedPoint) StringWithPrecision ¶
func (f FixedPoint) StringWithPrecision(precision int32) string
StringWithPrecision returns a rounded fixed-point string with given precision digits after
func (FixedPoint) Sub ¶
func (f FixedPoint) Sub(a FixedPoint) FixedPoint
func (FixedPoint) Uint256 ¶
func (f FixedPoint) Uint256() *uint256.Int
Uint256 returns a uint256.Int representation of the FixedPoint.
func (*FixedPoint) UnmarshalBinary ¶
func (f *FixedPoint) UnmarshalBinary(data []byte) error
UnmarshalBinary implements the encoding.TextUnmarshaler interface for text deserialization.
func (*FixedPoint) UnmarshalJSON ¶
func (f *FixedPoint) UnmarshalJSON(decimalBytes []byte) error
UnmarshalJSON implements the json.Unmarshaler interface for json deserialization.
func (*FixedPoint) UnmarshalMsgpack ¶
func (f *FixedPoint) UnmarshalMsgpack(decimalBytes []byte) error
func (*FixedPoint) UnmarshalText ¶
func (f *FixedPoint) UnmarshalText(text []byte) error
UnmarshalText implements the encoding.TextUnmarshaler interface for text deserialization.
type FixedPointArray ¶
type FixedPointArray []FixedPoint
FixedPointArray is a sql/driver compatible type for storing a slice of FixedPoint in postgres.
func (*FixedPointArray) Scan ¶
func (fs *FixedPointArray) Scan(value interface{}) error
Scan implements the sql.Scanner interface for database deserialization.