bunbig

package module
v0.0.0-...-49fdf94 Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2023 License: BSD-2-Clause Imports: 5 Imported by: 0

README

bunbig is a wrapper around math/big package to let us use big.int type in bun

Disclaimer: math/big does not implement database/sql scan/value methods and it can't be used in bun. This package uses math/big in its heart and extends its usefulness even into postgres.

Types

  • Int
    • Int wraps around big.Int and supports its base functionalities
  • Float
    • Float is a bun counterpart of big.Float

Example use


	type TableWithBigint struct {
		ID        uint64
		Name      string
		Deposit   *bunbig.Int
		Residue *bunbig.Float
	}

Mathematical Operations:

This package supports basic mathematical operations such as addition, subtraction, division, negation etc.

Example :

	x := bunbig.FromInt64(100)
	y , err := bunbig.FromString("9999999999999999999999999999999999999999")

	if err!=nil {
		panic(err)
	}

	y.Add(x) // 9999999999999999999999999999999999999999 + 100
	y.Sub(x) // 9999999999999999999999999999999999999999 - 100
	y.Neg() //  -9999999999999999999999999999999999999999
	// on the fly operation
	c:= bunbig.FromInt64(100).Mul(y) // 100 * 100 = 10000
	c.Div(x) // 10000/100 = 100
	c.Neg().Abs() // |-10000| = 10000

For extracting math/big's Int you can simply do as follows:

    d:= bunbig.ToMathBig(x)

Now you can do your calculations and convert it back to bunbig with:

   x = bunbig.FromBigint(d)

comparisons:

let we have x , y as two bigint.Bigint numbers in buntypes.

   x:= bunbig.FromInt64(100)
   y:= bunbig.FromInt64(90)

For comparing the above numbers, we can do as follow:

   cmp:=x.Cmp(y)

cmp has the following methods:

    // equal
	Eq() bool
	// greater than
	Gt() bool
	// lower than
	Lt() bool
	// Greater or equal
	Geq() bool
	// Lower or equal
	Leq() bool

Thus, we have the following results:

  x.Cmp(y).Eq() // 100 == 90 : false
  x.Cmp(y).Geq() // 100 >= 90 : true
  x.Cmp(y).Gt() // 100 > 90 : true
  x.Cmp(y).Lt() // 100 < 90 : false
  x.Cmp(y).Leq() // 100 <= 90 : false

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cmp

type Cmp interface {
	// equal
	Eq() bool
	// greater than
	Gt() bool
	// lower than
	Lt() bool
	// Greater or equal
	Geq() bool
	// Lower or equal
	Leq() bool
}

type Float

type Float big.Float

func NewFloat

func NewFloat() *Float

func (*Float) Abs

func (f *Float) Abs() *Float

func (*Float) Add

func (f *Float) Add(target *Float) *Float

func (*Float) Cmp

func (f *Float) Cmp(target *Float) Cmp

func (*Float) Div

func (f *Float) Div(target *Float) *Float

func (*Float) FromMathFloat

func (f *Float) FromMathFloat(fl *big.Float) *Float

func (*Float) FromString

func (f *Float) FromString(inp string) (*Float, error)

func (*Float) Mul

func (f *Float) Mul(target *Float) *Float

func (*Float) Neg

func (f *Float) Neg() *Float

func (*Float) Scan

func (f *Float) Scan(value interface{}) error

func (*Float) String

func (f *Float) String() string

func (*Float) Sub

func (f *Float) Sub(target *Float) *Float

func (*Float) ToFloat64

func (f *Float) ToFloat64() (float64, int8)

func (*Float) Value

func (f *Float) Value() (driver.Value, error)

type Int

type Int big.Int

func FromInt64

func FromInt64(x int64) *Int

func FromMathBig

func FromMathBig(x *big.Int) *Int

same as NewBigint()

func NewInt

func NewInt() *Int

func (*Int) Abs

func (b *Int) Abs() *Int

func (*Int) Add

func (b *Int) Add(x *Int) *Int

func (*Int) Cmp

func (b *Int) Cmp(target *Int) Cmp

func (*Int) Div

func (b *Int) Div(x *Int) *Int

func (*Int) FromString

func (i *Int) FromString(x string) (*Int, error)

func (*Int) Mul

func (b *Int) Mul(x *Int) *Int

func (*Int) Neg

func (b *Int) Neg() *Int

func (*Int) Scan

func (b *Int) Scan(value interface{}) error

func (*Int) String

func (b *Int) String() string

func (*Int) Sub

func (b *Int) Sub(x *Int) *Int

func (*Int) ToInt64

func (b *Int) ToInt64() int64

func (*Int) ToMathBig

func (b *Int) ToMathBig() *big.Int

func (*Int) ToUInt64

func (b *Int) ToUInt64() uint64

func (*Int) UnmarshalYAML

func (b *Int) UnmarshalYAML(value *yaml.Node) error

@todo , this part needs to be fixed

func (*Int) Value

func (b *Int) Value() (driver.Value, error)

Jump to

Keyboard shortcuts

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