fixedpoint

package module
v0.0.0-...-1b46583 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2023 License: MIT Imports: 11 Imported by: 0

README

Go Reference Report card

fixedpoint

A shopspring/decimal wrapper library for fixed point arithmetic operations in Cleverse projects.

Installation

go get github.com/Cleverse/go-utilities/fixedpoint

Documentation

Overview

Package fixedpoint provides a shopspring/decimal wrapper library for fixed point arithmetic operations.

Index

Constants

This section is empty.

Variables

View Source
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
)
View Source
var (
	Min = NewFromFloat64(1e-36)           // smallest possible FixedPoint (1e-36)
	Max = NewFromFloat64(math.MaxFloat64) // largest possible FixedPoint (1.7976931348623157e+308)
)

Functions

func SetDivPrecision

func SetDivPrecision(precision int32) (old int32)

SetDivPrecision set div operation precision (default is 36), returns old div precission.

func SetPrecision

func SetPrecision(precision int32) (old int32)

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 Empty

func Empty() FixedPoint

Empty alias for Zero. returns a new FixedPoint with value 0.

func MustSafeFromString

func MustSafeFromString(s string) FixedPoint

MustSafeNewFromString safe to use when input can be empty string. Will panic if error

func New

func New() FixedPoint

New returns a new null FixedPoint.

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 Zero

func Zero() FixedPoint

Zero returns a new FixedPoint with value 0.

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

func (f FixedPoint) IsOverPrecision(precision int) bool

IsOverPrecision return true if first significant digit is lower than given precision digit

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.

func (FixedPoint) Value

func (f FixedPoint) Value() (driver.Value, error)

Value implements the driver.Valuer interface for database serialization.

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.

func (FixedPointArray) Value

func (f FixedPointArray) Value() (driver.Value, error)

Value implements the driver.Valuer interface for database serialization.

Jump to

Keyboard shortcuts

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