big

package
v0.21.0 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2020 License: BSD-3-Clause Imports: 6 Imported by: 4

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

Constants

This section is empty.

Variables

View Source
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.

View Source
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.

View Source
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.

View Source
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

type Float struct {
	big.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

func CreateFloat(v float64) Float

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

func MustParseFloat(s string) (f *Float)

MustParseFloat convert the string `s` into Float or panic.

func NewFloat

func NewFloat(v float64) *Float

NewFloat create and initialize new Float with default bit precision, and rounding mode.

func ParseFloat

func ParseFloat(s string) (f *Float, err error)

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 SubFloat

func SubFloat(f, g *Float) *Float

SubFloat return the result of subtraction `f-g` as new Float.

func (*Float) Add

func (f *Float) Add(g interface{}) *Float

Add sets f to `f+g` and return the f as the result.

func (*Float) Clone

func (f *Float) Clone() *Float

Clone the instance to new Float.

func (*Float) Int64

func (f *Float) Int64() int64

Int64 return the integer resulting from truncating x towards zero.

func (*Float) IsEqual

func (f *Float) IsEqual(g interface{}) bool

IsEqual will return true if `f == g`.

func (*Float) IsGreater

func (f *Float) IsGreater(g interface{}) bool

IsGreater will return true if `f > g`.

func (*Float) IsGreaterOrEqual

func (f *Float) IsGreaterOrEqual(g interface{}) bool

IsGreaterOrEqual will return true if `f >= g`.

func (*Float) IsLess

func (f *Float) IsLess(g interface{}) bool

IsLess will return true if `f < g`.

func (*Float) IsLessOrEqual

func (f *Float) IsLessOrEqual(g interface{}) bool

IsLessOrEqual will return true if `f <= g`.

func (*Float) IsZero

func (f *Float) IsZero() bool

IsZero will return true if `f == 0`.

func (*Float) MarshalJSON

func (f *Float) MarshalJSON() ([]byte, error)

MarshalJSON implement the json.Marshaler interface and return the output of String method.

func (*Float) Mul

func (f *Float) Mul(g interface{}) *Float

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

func (f *Float) ParseFloat(s string) (err error)

Parse the string into Float value.

func (*Float) Quo

func (f *Float) Quo(g interface{}) *Float

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

func (f *Float) String() 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) Sub

func (f *Float) Sub(g interface{}) *Float

Sub sets f to rounded difference `f-g` and return f.

func (*Float) UnmarshalJSON

func (f *Float) UnmarshalJSON(in []byte) (err error)

UnmarshalJSON convert the JSON byte value into Float.

type Rat

type Rat struct {
	big.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) Abs added in v0.16.0

func (r *Rat) Abs() *Rat

Abs sets r to |r| (the absolute value of r) and return it.

func (*Rat) Add

func (r *Rat) Add(g interface{}) *Rat

Add sets r to `r+g` and return the r as the result.

func (*Rat) Int64

func (r *Rat) Int64() int64

Int64 return the integer resulting from truncating r towards zero.

func (*Rat) IsEqual

func (r *Rat) IsEqual(g interface{}) bool

IsEqual will return true if `r == g`.

func (*Rat) IsGreater

func (r *Rat) IsGreater(g interface{}) bool

IsGreater will return true if `r > g`.

func (*Rat) IsGreaterOrEqual

func (r *Rat) IsGreaterOrEqual(g interface{}) bool

IsGreaterOrEqual will return true if `r >= g`.

func (*Rat) IsGreaterThanZero

func (r *Rat) IsGreaterThanZero() bool

IsGreaterThanZero will return true if `r > 0`.

func (*Rat) IsLess

func (r *Rat) IsLess(g interface{}) bool

IsLess will return true if `r < g`.

func (*Rat) IsLessOrEqual

func (r *Rat) IsLessOrEqual(g interface{}) bool

IsLessOrEqual will return true if `r <= g`.

func (*Rat) IsLessThanZero

func (r *Rat) IsLessThanZero() bool

IsLessThanZero return true if `r < 0`.

func (*Rat) IsZero

func (r *Rat) IsZero() bool

IsZero will return true if `r == 0`.

func (*Rat) MarshalJSON

func (r *Rat) MarshalJSON() ([]byte, error)

MarshalJSON implement the json.Marshaler interface and return the output of String method.

func (*Rat) Mul

func (r *Rat) Mul(g interface{}) *Rat

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

func (r *Rat) Quo(g interface{}) *Rat

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) RoundToNearestAway added in v0.20.1

func (r *Rat) RoundToNearestAway(prec int) *Rat

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.

func (*Rat) RoundToZero added in v0.20.1

func (r *Rat) RoundToZero(prec int) *Rat

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

func (r *Rat) Scan(src interface{}) error

Scan implement the database's sql.Scan interface.

func (*Rat) String

func (r *Rat) String() 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) Sub

func (r *Rat) Sub(g interface{}) *Rat

Sub sets r to rounded difference `r-g` and return r.

func (*Rat) UnmarshalJSON

func (r *Rat) UnmarshalJSON(in []byte) (err error)

UnmarshalJSON convert the JSON byte value into Rat.

Jump to

Keyboard shortcuts

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