simplebig is a wrapper around math/big
built-in package and main goal is convient to use math/big
.
trying to solve these with simple wrapper around math/big
:
- no need to pass first parameter in most functions:
numb := simplebig.NewInt(12).Add(simplebig.NewInt(13))
fmt.Println(numb) // 25
- return new value for most methods instead of modify original value, it's thread-safe:
numb := simplebig.NewInt(12)
reuslt := numb.Sub(simplebig.NewInt(13))
fmt.Println(numb, result) // 12 -1
-
sql.Scanner
andsql/driver.Valuer
interfaces are implemented,Int
andFloat
can be used directly for databases. -
some helpers for convient like
Pow
or converting functions betweensimplebig.Float
andsimplebig.Int
are added. -
method chaining:
x := simplebig.NewInt(2)
result := x.Add(simplebig.NewInt(13)).Sub(simplebig.NewInt(5)).Mul(simplebig.NewInt(2)).Pow(simplebig.NewInt(3))
fmt.Println(result, x) // 8000 2
- it's only a wrapper, so it does whatever
math/big
do and it's accurate asmath/big
is accurate.
x := simplebig.NewInt(2).Add(simplebig.NewInt(-5)).Mul(simplebig.NewInt(-1))
// get copy of underlying big.Int
x.BigInt()
fmt.Println(x, x.BigInt())
// safe to use in gorm models
type User struct {
Name string
Balance simepleint.Int
}
simplebig.Int
implements two interface to compatibile types with using in databases, but this changes breaks one
feature and only this one from math/big
. math/big.Int
implements fmt.Scanner
but
simplebig.Int
implements sql.Scanner
. due to conflict in names of these two
interfaces, I decided to choose one of them which is database compatibility. because there is
a workaround for fmt.Scanner
but there is no workaround for database compatibility.
- implement wrapper for ModInverse and ModSqrt on
simplebig.Int
- implement wrapper for
math/big.Float
- implement wrapper for
math/big.Rat
MIT