Documentation ¶
Overview ¶
Package big extends the capabilities of standard "math/big" package by adding custom global precision to Float and Rat; and global rounding mode, and bits precision to Float.
Index ¶
- Variables
- type Float
- func AddFloat(f, g interface{}) *Float
- func CreateFloat(v float64) Float
- func MulFloat(f, g interface{}) *Float
- func MustParseFloat(s string) (f *Float)
- func NewFloat(v float64) *Float
- func ParseFloat(s string) (f *Float, err error)
- func QuoFloat(f, g interface{}) *Float
- func SubFloat(f, g *Float) *Float
- func (f *Float) Add(g interface{}) *Float
- func (f *Float) Clone() *Float
- func (f *Float) Int64() int64
- func (f *Float) IsEqual(g interface{}) bool
- func (f *Float) IsGreater(g interface{}) bool
- func (f *Float) IsGreaterOrEqual(g interface{}) bool
- func (f *Float) IsLess(g interface{}) bool
- func (f *Float) IsLessOrEqual(g interface{}) bool
- func (f *Float) IsZero() bool
- func (f *Float) MarshalJSON() ([]byte, error)
- func (f *Float) Mul(g interface{}) *Float
- func (f *Float) ParseFloat(s string) (err error)
- func (f *Float) Quo(g interface{}) *Float
- func (f *Float) String() string
- func (f *Float) Sub(g interface{}) *Float
- func (f *Float) UnmarshalJSON(in []byte) (err error)
- type Rat
- func (r *Rat) Abs() *Rat
- func (r *Rat) Add(g interface{}) *Rat
- func (r *Rat) Humanize(thousandSep, decimalSep string) string
- func (r *Rat) Int64() int64
- func (r *Rat) IsEqual(g interface{}) bool
- func (r *Rat) IsGreater(g interface{}) bool
- func (r *Rat) IsGreaterOrEqual(g interface{}) bool
- func (r *Rat) IsGreaterThanZero() bool
- func (r *Rat) IsLess(g interface{}) bool
- func (r *Rat) IsLessOrEqual(g interface{}) bool
- func (r *Rat) IsLessThanZero() bool
- func (r *Rat) IsZero() bool
- func (r *Rat) MarshalJSON() ([]byte, error)
- func (r *Rat) Mul(g interface{}) *Rat
- func (r *Rat) Quo(g interface{}) *Rat
- func (r *Rat) RoundNearestFraction() *Rat
- func (r *Rat) RoundToNearestAway(prec int) *Rat
- func (r *Rat) RoundToZero(prec int) *Rat
- func (r *Rat) Scan(src interface{}) error
- func (r *Rat) String() string
- func (r *Rat) Sub(g interface{}) *Rat
- func (r *Rat) UnmarshalJSON(in []byte) (err error)
- func (r *Rat) Value() (driver.Value, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var DefaultBitPrecision uint = 128
DefaultBitPrecision define the maximum number of mantissa bits available to represent the value.
In standard library this value is 24 for float32 or 53 for float64.
One should change this value before using the new extended Float in the program.
var DefaultDigitPrecision = 8
DefaultDigitPrecision define the default number of digits after decimal point which affect the return of String() and MarshalJSON() methods.
A zero value of digit precision mean is it will use the default output of 'f' format.
One should change this value before using the new extended Float or Rat in the program.
var DefaultRoundingMode = big.ToNearestAway
DefaultRoundingMode define the default rounding mode for all instance of Float.
One should change this value before using the new extended Float in the program.
var MarshalJSONAsString bool = true
MarshalJSONAsString define the default return behaviour of MarshalJSON(). If its true (the default) the returned JSON format will encapsulated in double quote, as string instead of as numeric.
Functions ¶
This section is empty.
Types ¶
type Float ¶
Float extend the standard big.Float by setting each instance precision to DefaultBitPrecision, rounding mode to DefaultRoundingMode, and using DefaultDigitPrecision value after decimal point when converted to string.
func AddFloat ¶
func AddFloat(f, g interface{}) *Float
AddFloat return the rounded sum `f+g` and return f.
func CreateFloat ¶
Create Float with default bit precision and rounding mode.
func MulFloat ¶
func MulFloat(f, g interface{}) *Float
MulFloat return the result of multiplication `f*g`. It will return nil if `f` or `g` is not convertible to Float.
func MustParseFloat ¶
MustParseFloat convert the string `s` into Float or panic.
func NewFloat ¶
NewFloat create and initialize new Float with default bit precision, and rounding mode.
func ParseFloat ¶
ParseFloat the string s into Float value.
func QuoFloat ¶
func QuoFloat(f, g interface{}) *Float
QuoFloat return the quotient of `f/g` as new Float.
func (*Float) IsGreaterOrEqual ¶
IsGreaterOrEqual will return true if `f >= g`.
func (*Float) IsLessOrEqual ¶
IsLessOrEqual will return true if `f <= g`.
func (*Float) MarshalJSON ¶
MarshalJSON implement the json.Marshaler interface and return the output of String method.
func (*Float) Mul ¶
Mul sets f to product of `f * g` and return the result as f. If g is not convertible to Float it will return nil.
func (*Float) ParseFloat ¶
Parse the string into Float value.
func (*Float) Quo ¶
Quo sets f to quotient of `f/g` and return the result as f. If g is not convertible to Float it will return nil.
func (*Float) String ¶
String format the Float value into string with maximum mantissa is set by digit precision option.
Unlike standard String method, this method will trim trailing zero digit or decimal point at the end of mantissa.
func (*Float) UnmarshalJSON ¶
UnmarshalJSON convert the JSON byte value into Float.
type Rat ¶
Rat extend the standard big.Rat using rounding mode ToZero.
func AddRat ¶
func AddRat(f ...interface{}) *Rat
AddRat return the sum of `f+g+...`. It will return nil if `f` or `g` is not convertable to Rat.
func MulRat ¶
func MulRat(f ...interface{}) *Rat
MulRat return the result of multiplication `f*...`. It will return nil if parameter is empty or `f` is not convertable to Rat.
func MustRat ¶
func MustRat(v interface{}) (r *Rat)
MustRat create and initialize new Rat value from v or panic if v is unknown type that cannot be converted to Rat.
func NewRat ¶
func NewRat(v interface{}) (r *Rat)
NewRat create and initialize new Rat value from v or nil if v is invalid type that cannot be converted to Rat.
func QuoRat ¶
func QuoRat(f ...interface{}) *Rat
QuoRat return the quotient of `f/g/...` as new Rat.
func SubRat ¶
func SubRat(f ...interface{}) *Rat
SubRat return the result of subtraction `f-g-...` as new Rat.
func (*Rat) Humanize ¶ added in v0.23.0
Humanize format the r into string with custom thousand and decimal separator.
Example ¶
var ( thousandSep = "." decimalSep = "," ) fmt.Printf("%s\n", NewRat("0").Humanize(thousandSep, decimalSep)) fmt.Printf("%s\n", NewRat("0.1234").Humanize(thousandSep, decimalSep)) fmt.Printf("%s\n", NewRat("100").Humanize(thousandSep, decimalSep)) fmt.Printf("%s\n", NewRat("100.1234").Humanize(thousandSep, decimalSep)) fmt.Printf("%s\n", NewRat("1000").Humanize(thousandSep, decimalSep)) fmt.Printf("%s\n", NewRat("1000.2").Humanize(thousandSep, decimalSep)) fmt.Printf("%s\n", NewRat("10000.23").Humanize(thousandSep, decimalSep)) fmt.Printf("%s\n", NewRat("100000.234").Humanize(thousandSep, decimalSep))
Output: 0 0,1234 100 100,1234 1.000 1.000,2 10.000,23 100.000,234
func (*Rat) IsGreaterOrEqual ¶
IsGreaterOrEqual will return true if `r >= g`.
func (*Rat) IsGreaterThanZero ¶
IsGreaterThanZero will return true if `r > 0`.
func (*Rat) IsLessOrEqual ¶
IsLessOrEqual will return true if `r <= g`.
func (*Rat) IsLessThanZero ¶
IsLessThanZero return true if `r < 0`.
func (*Rat) MarshalJSON ¶
MarshalJSON implement the json.Marshaler interface and return the output of String method.
func (*Rat) Mul ¶
Mul sets r to product of `r * g` and return the result as r. If g is not convertible to Rat it will return nil.
func (*Rat) Quo ¶
Quo sets r to quotient of `r/g` and return the result as r. If g is not convertible to Rat it will return nil.
func (*Rat) RoundNearestFraction ¶ added in v0.23.0
RoundNearestFraction round the fraction to the nearest non-zero value.
The RoundNearestFraction does not require precision parameter, like in other rounds function, but it figure it out based on the last non-zero value from fraction.
See example for more information.
Example ¶
fmt.Printf("0.000000001: %s\n", NewRat("0").RoundNearestFraction()) // Affected by DefaultDigitPrecision (8) fmt.Printf("0.00545: %s\n", NewRat("0.00545").RoundNearestFraction()) fmt.Printf("0.00555: %s\n", NewRat("0.00555").RoundNearestFraction()) fmt.Printf("0.0545: %s\n", NewRat("0.0545").RoundNearestFraction()) fmt.Printf("0.0555: %s\n", NewRat("0.0555").RoundNearestFraction()) fmt.Printf("0.545: %s\n", NewRat("0.545").RoundNearestFraction()) fmt.Printf("0.555: %s\n", NewRat("0.555").RoundNearestFraction()) fmt.Printf("0.5: %s\n", NewRat("0.5").RoundNearestFraction()) fmt.Printf("-0.5: %s\n", NewRat("-0.5").RoundNearestFraction()) fmt.Printf("-0.555: %s\n", NewRat("-0.555").RoundNearestFraction()) fmt.Printf("-0.545: %s\n", NewRat("-0.545").RoundNearestFraction())
Output: 0.000000001: 0 0.00545: 0.005 0.00555: 0.006 0.0545: 0.05 0.0555: 0.06 0.545: 0.5 0.555: 0.6 0.5: 0.5 -0.5: -0.5 -0.555: -0.6 -0.545: -0.5
func (*Rat) RoundToNearestAway ¶ added in v0.20.1
RoundToNearestAway round r to n digit precision using nearest away mode, where mantissa is accumulated by the last digit after precision. For example, using 2 digit precision, 0.555 would become 0.56.
Example ¶
fmt.Printf("0.5455: %s\n", NewRat("0.5455").RoundToNearestAway(2)) fmt.Printf("0.5555: %s\n", NewRat("0.5555").RoundToNearestAway(2)) fmt.Printf("0.5566: %s\n", NewRat("0.5567").RoundToNearestAway(2)) fmt.Printf("0.5566: %s\n", NewRat("0.5566").RoundToNearestAway(0)) fmt.Printf("0.5: %s\n", NewRat("0.5").RoundToNearestAway(0)) fmt.Printf("-0.5: %s\n", NewRat("-0.5").RoundToNearestAway(0))
Output: 0.5455: 0.55 0.5555: 0.56 0.5566: 0.56 0.5566: 1 0.5: 1 -0.5: -1
func (*Rat) RoundToZero ¶ added in v0.20.1
RoundToZero round r to n digit precision using to zero mode. For example, using 2 digit precision, 0.555 would become 0.55.
func (*Rat) String ¶
String format the Rat value into string with maximum mantissa is set by digit precision option with rounding mode set to zero.
Unlike standard String method, this method will trim trailing zero digit or decimal point at the end of mantissa.
func (*Rat) UnmarshalJSON ¶
UnmarshalJSON convert the JSON byte value into Rat.