numeric

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2024 License: MIT Imports: 5 Imported by: 0

README

numeric

Ultra-accurate and reliable big number library for Go. Powered with C GNU MPFR library.

Requirements

  1. Install GMP (GNU Multi Precision Arithmetic Library):
sudo apt-get install libgmp3-dev
  1. Install GNU MPFR (GNU Multiple Precision Floating-Point Reliable Library):
sudo apt-get install libmpfr-dev

Examples

package main

import (
	"fmt"

	"github.com/acailuv/numeric"
)

func main() {
	a1 := numeric.New(10001.123)
	a2 := numeric.New(100001.123123)
	a3 := a1.Add(a2)
	fmt.Println(a1, "+", a2, "=", a3)

	s1 := numeric.New(3)
	s2 := numeric.New(4)
	s3 := s1.Subtract(s2)
	fmt.Println(s1, "-", s2, "=", s3)

	m1 := numeric.New(5)
	m2 := numeric.New(6)
	m3 := m1.Multiply(m2)
	fmt.Println(m1, "*", m2, "=", m3)

	d1 := numeric.New(19)
	d2 := numeric.New(6)
	d3 := d1.Divide(d2).Truncate(5)
	fmt.Println(d1, "/", d2, "=", d3.StringDecimalPlaces(3))

	x1 := numeric.New(1)
	x2 := x1.Divide(3)
	fmt.Println(x1, "/", 3, "=", x2)

	gt1 := numeric.New(1)
	gt2 := numeric.New(2)
	fmt.Println(gt1, ">", gt2, "=", gt1.GreaterThan(gt2))

	lt1 := numeric.New(1)
	lt2 := numeric.New(2)
	fmt.Println(lt1, "<", lt2, "=", lt1.LessThan(lt2))

	gte1 := numeric.New(1)
	gte2 := numeric.New(2)
	fmt.Println(gte1, ">=", gte2, "=", gte1.GreaterThanOrEqual(gte2))

	lte1 := numeric.New(1)
	lte2 := numeric.New(2)
	fmt.Println(lte1, "<=", lte2, "=", lte1.LessThanOrEqual(lte2))

	eq1 := numeric.New(1)
	eq2 := numeric.New(1)
	fmt.Println(eq1, "==", eq2, "=", eq1.Equal(eq2))

	taxRate := numeric.New("0.0011")
	feeRate := numeric.New("0.0008")
	buyerBrokerage := numeric.New("0.0285")
	sumTaxFeeRate := taxRate.Add(feeRate)
	taxProportion := taxRate.Divide(sumTaxFeeRate)
	tax := buyerBrokerage.Multiply(taxProportion)

	fmt.Println("Tax:", tax) // 0.0165000000 -- Other libraries would return something like 0.016499999...
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	PrecisionBits       uint64 = 53 // Default MPFR precision
	StringDecimalPlaces uint64 = 10 // Default decimal places for string conversion
)

region Global Variables

Functions

func SetPrecisionBits

func SetPrecisionBits(bits uint64)

Sets the default precision bits when doing arithmetic operations. The upper limit is "virtually" unlimited. However, more precision bits will make your app use more RAM. Cranking this number up to a large number will also make arithmetic operations slower. So bear this in mind. Default value is 53.

func SetStringDecimalPlaces

func SetStringDecimalPlaces(dp uint64)

Sets the decimal places shown when .String() is called. The upper limit is "virtually" unlimited. However, more decimal places will make your number inaccurate. Example: 1.23 will be represented as 1.22999999... if you set a high decimal places, with not enough precision bits. You can overcome this by setting a higher precision bits, but bear in mind the consequences. Default value is 10.

Types

type Numeric

type Numeric struct {
	// contains filtered or unexported fields
}

func New

func New(x any) Numeric

Creates a new numeric value. The type of x has to be int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64 or string.

func NewWithError

func NewWithError(x any) (Numeric, error)

Creates a new numeric value, with error handling. The type of x has to be int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64 or string.

func (Numeric) Abs

func (n Numeric) Abs() Numeric

Makes the number positive. This will modify the original number.

func (Numeric) Add

func (n Numeric) Add(x any) Numeric

Add a number and return the result. This will not modify the original number.

func (Numeric) Ceil

func (n Numeric) Ceil(dp int) Numeric

Ceil the number to the specified decimal places.

func (Numeric) Destroy

func (n Numeric) Destroy()

Clears the memory for the numeric value.

func (Numeric) Divide

func (n Numeric) Divide(x any) Numeric

Divide a number and return the result. This will not modify the original number.

func (Numeric) Equal

func (n Numeric) Equal(x any) bool

Equal returns true if the number is equal to `x`.

func (Numeric) Float32

func (n Numeric) Float32() float32

Returns numeric as float32

func (Numeric) Float64

func (n Numeric) Float64() float64

Returns numeric as float64

func (Numeric) Floor

func (n Numeric) Floor(dp int) Numeric

Floor the number to the specified decimal places.

func (Numeric) GreaterThan

func (n Numeric) GreaterThan(x any) bool

GreaterThan returns true if the number is greater than `x`.

func (Numeric) GreaterThanOrEqual

func (n Numeric) GreaterThanOrEqual(x any) bool

GreaterThanOrEqual returns true if the number is greater than or equal to `x`.

func (Numeric) Int

func (n Numeric) Int() int

Returns numeric as int

func (Numeric) Int16

func (n Numeric) Int16() int16

Returns numeric as int16

func (Numeric) Int32

func (n Numeric) Int32() int32

Returns numeric as int32

func (Numeric) Int64

func (n Numeric) Int64() int64

Returns numeric as int64

func (Numeric) Int8

func (n Numeric) Int8() int8

Returns numeric as int8

func (Numeric) LessThan

func (n Numeric) LessThan(x any) bool

LessThan returns true if the number is less than `x`.

func (Numeric) LessThanOrEqual

func (n Numeric) LessThanOrEqual(x any) bool

LessThanOrEqual returns true if the number is less than or equal to `x`.

func (Numeric) MarshalJSON

func (n Numeric) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (Numeric) Multiply

func (n Numeric) Multiply(x any) Numeric

Multiply a number and return the result. This will not modify the original number.

func (Numeric) Neg

func (n Numeric) Neg() Numeric

Makes the number negative. This will modify the original number.

func (Numeric) Pow

func (n Numeric) Pow(power any) Numeric

Exponent the current number to the power of `x` and return the result. This will not modify the original number.

func (*Numeric) Scan

func (n *Numeric) Scan(value any) error

Scan implements the sql.Scanner interface for database deserialization.

func (Numeric) String

func (n Numeric) String() string

Returns numeric as a string. The default number of decimal places is 10. You can modify the default number of decimal places by using SetStringDecimalPlaces function.

func (Numeric) StringDecimalPlaces

func (n Numeric) StringDecimalPlaces(dp uint64) string

Returns numeric as a string with a specified number of decimal places.

func (Numeric) Subtract

func (n Numeric) Subtract(x any) Numeric

Subtract a number and return the result. This will not modify the original number.

func (Numeric) Truncate

func (n Numeric) Truncate(dp int) Numeric

Truncate the number to the specified decimal places.

func (Numeric) Uint

func (n Numeric) Uint() uint

Returns numeric as uint

func (Numeric) Uint16

func (n Numeric) Uint16() uint16

Returns numeric as uint16

func (Numeric) Uint32

func (n Numeric) Uint32() uint32

Returns numeric as uint32

func (Numeric) Uint64

func (n Numeric) Uint64() uint64

Returns numeric as uint64

func (Numeric) Uint8

func (n Numeric) Uint8() uint8

Returns numeric as uint8

func (*Numeric) UnmarshalJSON

func (n *Numeric) UnmarshalJSON(bytes []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

func (Numeric) Value

func (n Numeric) 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