Documentation
¶
Overview ¶
Package bigint provides an immutable-style arbitrary-precision integer type built on top of math/big.Int.
BigInt is useful when application code needs deterministic integer arithmetic, string-safe JSON encoding, SQL scanning, and binary marshaling while keeping the usual API surface free from mutable *big.Int receivers.
Value Semantics ¶
Arithmetic and inspection methods have value receivers and return new BigInt values without mutating the receiver. Examples include Add, Sub, Mul, Quo, Mod, Power, Sqrt, Cmp, Sign, String, MarshalJSON, and MarshalBinary.
Methods with pointer receivers mutate the receiver and require external synchronization whenever the same *BigInt may be accessed by more than one goroutine. These include Scan, UnmarshalJSON, UnmarshalBinary, and Unmarshal.
Concurrency ¶
A BigInt value created by a constructor or arithmetic method is safe for concurrent read access provided that no goroutine reassigns the variable holding it. Accessors that expose math/big values, such as BigInt, return defensive copies so callers can mutate the returned *big.Int without changing the original value.
Zero Value ¶
The zero value of BigInt is uninitialized. Use Zero or NewFromInt(0) when a real numeric zero is required.
Index ¶
- Variables
- func UnquoteIfQuoted(value any) (string, error)
- type BigInt
- func Max(a, b BigInt) BigInt
- func Min(a, b BigInt) BigInt
- func MustNewFromString(s string) BigInt
- func NewFromBigInt(i *big.Int) BigInt
- func NewFromInt(i int) BigInt
- func NewFromInt64(i int64) BigInt
- func NewFromString(s string) (BigInt, bool)
- func NewFromUint(i uint) BigInt
- func NewFromUint64(i uint64) BigInt
- func (b BigInt) Abs() BigInt
- func (b BigInt) Add(b2 BigInt) BigInt
- func (b BigInt) AddInt64(rhs int64) BigInt
- func (b BigInt) AddUint64(rhs uint64) BigInt
- func (b BigInt) BigInt() *big.Int
- func (b BigInt) BitLen() int
- func (b BigInt) Cmp(b2 BigInt) int
- func (b BigInt) Equal(b2 BigInt) bool
- func (b BigInt) GT(b2 BigInt) bool
- func (b BigInt) GTE(b2 BigInt) bool
- func (b BigInt) GetInt64() int64
- func (b BigInt) IsNegative() bool
- func (b BigInt) IsNil() bool
- func (b BigInt) IsPositive() bool
- func (b BigInt) IsZero() bool
- func (b BigInt) LT(b2 BigInt) bool
- func (b BigInt) LTE(b2 BigInt) bool
- func (b BigInt) Marshal() ([]byte, error)
- func (b BigInt) MarshalBinary() (data []byte, err error)
- func (b BigInt) MarshalJSON() ([]byte, error)
- func (b BigInt) MarshalTo(data []byte) (n int, err error)
- func (b BigInt) MarshalYAML() (any, error)
- func (b BigInt) Mod(b2 BigInt) BigInt
- func (b BigInt) Mul(b2 BigInt) BigInt
- func (b BigInt) Neg() BigInt
- func (b BigInt) Power(power int64) BigInt
- func (b BigInt) Quo(b2 BigInt, roundingMode RoundingMode) BigInt
- func (b BigInt) QuoDown(b2 BigInt) BigInt
- func (b *BigInt) Scan(value any) error
- func (b BigInt) ShiftLeft(n uint) BigInt
- func (b BigInt) ShiftRight(n uint) BigInt
- func (b BigInt) Sign() int
- func (b BigInt) Size() int
- func (b BigInt) Sqrt() BigInt
- func (b BigInt) String() string
- func (b BigInt) Sub(b2 BigInt) BigInt
- func (b *BigInt) Unmarshal(data []byte) error
- func (b *BigInt) UnmarshalBinary(data []byte) error
- func (b *BigInt) UnmarshalJSON(bz []byte) error
- func (b BigInt) Value() (driver.Value, error)
- type RoundingMode
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( Zero = NewFromInt(0) One = NewFromInt(1) Ten = NewFromInt(10) )
Functions ¶
func UnquoteIfQuoted ¶
Types ¶
type BigInt ¶
type BigInt struct {
// contains filtered or unexported fields
}
BigInt is a wrapper around big.Int that provides some convenience methods
Note: BigInt is immutable, so all methods return a new BigInt
Example (Arithmetic) ¶
package main
import (
"fmt"
"github.com/exc-works/bigint"
)
func main() {
a := bigint.MustNewFromString("12345678901234567890")
b := bigint.NewFromInt64(10)
sum := a.Add(b)
quotient := sum.Quo(bigint.NewFromInt(3), bigint.RoundDown)
fmt.Println(a)
fmt.Println(sum)
fmt.Println(quotient)
}
Output: 12345678901234567890 12345678901234567900 4115226300411522633
Example (Rounding) ¶
package main
import (
"fmt"
"github.com/exc-works/bigint"
)
func main() {
x := bigint.NewFromInt(-5)
y := bigint.NewFromInt(2)
fmt.Println(x.Quo(y, bigint.RoundDown))
fmt.Println(x.Quo(y, bigint.RoundUp))
fmt.Println(x.Quo(y, bigint.RoundCeiling))
}
Output: -2 -3 -2
func MustNewFromString ¶
func NewFromBigInt ¶
func NewFromInt ¶
func NewFromInt64 ¶
func NewFromString ¶
NewFromString returns a BigInt from a string.
If the string starts with 0x or 0X, it is interpreted as a hex string. If the string starts with 0b or 0B, it is interpreted as a binary string. Otherwise, it is interpreted as a decimal string.
func NewFromUint ¶
func NewFromUint64 ¶
func (BigInt) GetInt64 ¶
GetInt64 returns the int64 representation of x. If x cannot be represented in an int64, the result is undefined.
func (BigInt) IsNegative ¶
func (BigInt) IsPositive ¶
func (BigInt) MarshalBinary ¶
MarshalBinary implements the encoding.BinaryMarshaler interface
func (BigInt) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface
Example ¶
package main
import (
"encoding/json"
"fmt"
"github.com/exc-works/bigint"
)
func main() {
type Balance struct {
Amount bigint.BigInt `json:"amount"`
}
b := Balance{Amount: bigint.MustNewFromString("9007199254740993")}
data, _ := json.Marshal(b)
fmt.Println(string(data))
}
Output: {"amount":"9007199254740993"}
func (BigInt) MarshalYAML ¶
MarshalYAML implements the yaml.Marshaler interface
func (BigInt) Quo ¶
func (b BigInt) Quo(b2 BigInt, roundingMode RoundingMode) BigInt
Quo returns the quotient of b and b2
Rounding mode is only support for math.RoundDown, math.RoundUp, math.RoundCeiling and math.RoundUnnecessary
func (BigInt) ShiftRight ¶
ShiftRight returns b shifted right by n bits
func (*BigInt) UnmarshalBinary ¶
UnmarshalBinary implements the encoding.BinaryUnmarshaler interface
func (*BigInt) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface
type RoundingMode ¶
type RoundingMode int
const ( // RoundDown rounds towards zero. RoundDown RoundingMode = iota // RoundUp rounds away from zero. RoundUp // RoundCeiling rounds towards positive infinity. RoundCeiling // RoundHalfUp rounds to nearest; ties round up. RoundHalfUp // RoundHalfDown rounds to nearest; ties round down. RoundHalfDown // RoundHalfEven rounds to nearest; ties to even. RoundHalfEven // RoundUnnecessary asserts no rounding is required. RoundUnnecessary )