xbig

package module
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: May 2, 2025 License: MIT Imports: 5 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

xbig

github.com/CAFxX/xbig is a generics-based convenience wrapper package that aims at making the math/big API more ergonomic to use.

Go Reference

Examples

The following are all valid ways to use xbig:

// 2π/2¹²⁸
QuoFloat(MulFloat(math.PI, 2), ExpInt(2, 128))

// 4793825749327547329903472 * 9843728974589574362758946543567483265783432
MulInt("4793825749327547329903472", "9843728974589574362758946543567483265783432")

You can test xbig on the Go playground.

Functionalities

Simplified instance construction

NewInt, NewRat, NewFloat accept the following source and create a new instance of the corresponding big.Int, big.Rat, or big.Float type. SetInt, SetRat, SetFloat do the same, but set the provided big.Int, big.Rat, or big.Float instance.

The following table summarizes which conversions are supported (✅ means supported, and the conversion can not fail; ⚠️ means supported, but the conversion can fail depending on the source value; ❌ means not supported)

Source type *big.Int *big.Rat *big.Float
int ✅ ✅ ✅
int8 ✅ ✅ ✅
int16 ✅ ✅ ✅
int32 ✅ ✅ ✅
int64 ✅ ✅ ✅
uint ✅ ✅ ✅
uint8 ✅ ✅ ✅
uint16 ✅ ✅ ✅
uint32 ✅ ✅ ✅
uint64 ✅ ✅ ✅
float32 ❌ ⚠️ (±∞, NaN) ⚠️ (NaN)
float64 ❌ ⚠️ (±∞, NaN) ⚠️ (NaN)
*big.Int ✅ ✅ ✅
*big.Rat ❌ ✅ ✅
*big.Float ❌ ⚠️ (±∞) ✅
string ⚠️ ⚠️ ⚠️
[]byte ✅ ❌ ❌
[]big.Word ✅ ❌ ❌
Simplified function composition

Most aritchmetic functions in the package automatically perform type conversions (see the previous section), so it is e.g. possible to pass a big.Rat or int to functions that in math/big only accept a big.Float as input.

Furthermore, all functions (apart from the Set* functions) allocate a new result and never modify their inputs, so accidental shadowing of variables is not possible.

Arbitrary precision constants

The functions Pi, Phi, and E return the mathematical constants $\pi$, $\varphi$, and $e$ with arbitrary precision.

Notes

This package is a WIP: APIs are not stable yet.

Ergonomy comes at a slight increase in memory allocations and copies, as well as dispatch logic. Improvements in the go compiler/runtime should eventually be able to remove most of these overheads: golang/go#59591, golang/go#64824, golang/go#48849 and so on.

Documentation

Overview

Package xbig provides a more ergonomic interface to the math/big package. It provides functions for creating and manipulating big integers, floats, and rationals, and for performing arithmetic operations on these types.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AbsFloat

func AbsFloat[T floatNums](x T) *big.Float

AbsFloat returns the absolute value of the given float value as a new big.Float. The input value is not modified.

See big.Float.Abs for more details.

func AbsInt

func AbsInt[T intNums](x T) *big.Int

AbsInt computes the absolute value of x and returns the result as a new big.Int. The input is unmodified.

See big.Int.Abs for more details.

func AbsRat

func AbsRat[T ratNums](x T) *big.Rat

AbsRat returns the absolute value of the given rational value as a new big.Rat. The input value is not modified.

See big.Rat.Abs for more details.

func AddFloat

func AddFloat[T, U floatNums](x T, y U) *big.Float

AddFloat adds two float values and returns the result as a new big.Float. The input values are not modified.

See big.Float.Add for more details.

func AddInt

func AddInt[T, U intNums](x T, y U) *big.Int

AddInt adds two integers and returns the result as a new big.Int. The inputs are unmodified.

See big.Int.Add for more details.

func AddRat

func AddRat[T, U ratNums](x T, y U) *big.Rat

AddRat adds two rational values and returns the result as a new big.Rat. The input values are not modified.

See big.Rat.Add for more details.

func AndInt

func AndInt[T, U intNums](x T, y U) *big.Int

AndInt computes the bitwise AND of two integers and returns the result as a new big.Int. The inputs are unmodified.

See big.Int.And for more details.

func AndNotInt

func AndNotInt[T, U intNums](x T, y U) *big.Int

AndNotInt computes the bitwise AND NOT of two integers and returns the result as a new big.Int. The inputs are unmodified.

See big.Int.AndNot for more details.

func CmpAbsInt

func CmpAbsInt[T, U intNums](x T, y U) int

CmpAbsInt compares the absolute values of two integers and returns:

-1 if |x| < |y|
0 if |x| == |y|
1 if |x| > |y|

The inputs are unmodified.

See big.Int.CmpAbs for more details.

func CmpFloat

func CmpFloat[T, U floatNums](x T, y U) int

CmpFloat compares two float values and returns:

-1 if x < y
0 if x == y
1 if x > y

The input values are not modified.

See big.Float.Cmp for more details.

func CmpInt

func CmpInt[T, U intNums](x T, y U) int

CmpInt compares two integers and returns:

-1 if x < y
0 if x == y
1 if x > y

The inputs are unmodified.

See big.Int.Cmp for more details.

func CmpRat

func CmpRat[T, U ratNums](x T, y U) int

CmpRat compares two rational values and returns:

-1 if x < y
0 if x == y
1 if x > y

The input values are not modified.

See big.Rat.Cmp for more details.

func DivInt

func DivInt[T, U intNums](x T, y U) *big.Int

DivInt divides two integers and returns the result as a new big.Int. The inputs are unmodified.

See big.Int.Div for more details.

func DivModInt

func DivModInt[T, U intNums](x T, y U) (*big.Int, *big.Int)

DivModInt divides two integers and returns the quotient and remainder as two new [big.Int]s. The inputs are unmodified.

See big.Int.DivMod for more details.

func E added in v0.2.0

func E(prec uint) *big.Float

E is the constant e (Euler's number) with the given precision (in bits).

func ExpFloat added in v0.2.0

func ExpFloat[T floatNums](x T) *big.Float

ExpFloat computes e^x and returns the result as a new big.Float. The input value is not modified.

See github.com/ALTree/bigfloat.Exp for more details.

func ExpInt

func ExpInt[T, U intNums](x T, y U) *big.Int

ExpInt computes x to the power of y and returns the result as a new big.Int. The inputs are unmodified.

See big.Int.Exp for more details.

func ExpModInt

func ExpModInt[T, U, V intNums](x T, y U, z V) *big.Int

ExpModInt computes x to the power of y modulo z and returns the result as a new big.Int. The inputs are unmodified.

See big.Int.Exp for more details.

func FMAFloat added in v0.2.0

func FMAFloat[T, U, V floatNums](x T, y U, z V) *big.Float

FMAFloat computes the fused multiply-add of x, y, and z and returns the result as a new big.Float. The input values are not modified. The precision of the result is the maximum of the precision of x, y, and z.

func FMAInt added in v0.2.0

func FMAInt[T, U, V intNums](x T, y U, z V) *big.Int

FMAInt computes the fused multiply-add of x, y, and z and returns the result as a new big.Int. The inputs are unmodified.

See big.Int.FMA for more details.

func FMARat added in v0.2.0

func FMARat[T, U, V ratNums](x T, y U, z V) *big.Rat

FMARat computes the fused multiply-add of three rational values and returns the result as a new big.Rat. The inputs are not modified.

func GCDInt

func GCDInt[T, U intNums](x T, y U) *big.Int

GCDInt computes the greatest common divisor of x and y and returns the result as a new big.Int. The inputs are unmodified.

See big.Int.GCD for more details.

func GCDPolyInt added in v0.2.0

func GCDPolyInt[T, U intNums](a T, b U) (*big.Int, *big.Int, *big.Int)

GCDPolyInt computes the greatest common divisor of x and y and returns the result as a new big.Int, and two new [big.Int]s that are the coefficients of the GCD polynomial. The inputs are unmodified.

See big.Int.GCD for more details.

func InvRat

func InvRat[T ratNums](x T) *big.Rat

InvRat returns the inverse of the given rational value as a new big.Rat. The input value is not modified.

See big.Rat.Inv for more details.

func LogBaseFloat added in v0.2.1

func LogBaseFloat[T, U floatNums](a T, b U) *big.Float

LogBaseFloat computes the logarithm of a in base b and returns the result as a new big.Float. The input values are not modified.

func LogFloat added in v0.2.0

func LogFloat[T floatNums](x T) *big.Float

LogFloat computes the natural logarithm of x and returns the result as a new big.Float. The input value is not modified.

See github.com/ALTree/bigfloat.Log for more details.

func LshInt

func LshInt[T, U intNums](x T, y U) *big.Int

LshInt computes the left shift of x by y and returns the result as a new big.Int. The inputs are unmodified.

See big.Int.Lsh for more details.

func ModInt

func ModInt[T, U intNums](x T, y U) *big.Int

ModInt computes the modulus of two integers and returns the result as a new big.Int. The inputs are unmodified.

See big.Int.Mod for more details.

func ModInverseInt

func ModInverseInt[T, U intNums](x T, y U) *big.Int

ModInverseInt computes the modular inverse of x modulo y and returns the result as a new big.Int. The inputs are unmodified.

See big.Int.ModInverse for more details.

func ModSqrtInt added in v0.2.0

func ModSqrtInt[T, U intNums](x T, y U) *big.Int

ModSqrtInt computes the modular square root of x modulo y and returns the result as a new big.Int. The inputs are unmodified.

See big.Int.ModSqrt for more details.

func MulFloat

func MulFloat[T, U floatNums](x T, y U) *big.Float

MulFloat multiplies two float values and returns the result as a new big.Float. The input values are not modified.

See big.Float.Mul for more details.

func MulInt

func MulInt[T, U intNums](x T, y U) *big.Int

MulInt multiplies two integers and returns the result as a new big.Int. The inputs are unmodified.

See big.Int.Mul for more details.

func MulRat

func MulRat[T, U ratNums](x T, y U) *big.Rat

MulRat multiplies two rational values and returns the result as a new big.Rat. The input values are not modified.

See big.Rat.Mul for more details.

func NegFloat

func NegFloat[T floatNums](x T) *big.Float

NegFloat returns the negation of the given float value as a new big.Float. The input value is not modified.

See big.Float.Neg for more details.

func NegInt

func NegInt[T intNums](x T) *big.Int

NegInt computes the negation of x and returns the result as a new big.Int. The input is unmodified.

See big.Int.Neg for more details.

func NegRat

func NegRat[T ratNums](x T) *big.Rat

NegRat returns the negation of the given rational value as a new big.Rat. The input value is not modified.

See big.Rat.Neg for more details.

func NewFloat

func NewFloat[T floatNums](x T) *big.Float

NewFloat creates a new big.Float from the given float value. The input is not modified. If the input is a string, it is parsed using [*math/big.Float).SetString(x.] The function returns the new big.Float on success. On failure, it returns nil. The precision of the new big.Float depends on the input value precision.

func NewInt

func NewInt[T intNums](x T) *big.Int

NewInt creates a new big.Int from the given integer value. The input is not modified. The function the new big.Int on success. On failure, it returns nil.

func NewRat

func NewRat[T ratNums](x T) *big.Rat

NewRat creates a new big.Rat from the given rational value. The input is not modified. If the input is a string, it is parsed using [*math/big.Rat.SetString(x)]. The function returns the new big.Rat on success. On failure, it returns nil.

func NewRatFrac

func NewRatFrac[T, U ratNums](x T, y U) *big.Rat

NewRatFrac creates a new big.Rat from the given rational values. The inputs are not modified.

func NotInt

func NotInt[T, U intNums](x T, y U) *big.Int

NotInt computes the bitwise NOT of x and returns the result as a new big.Int. The input is unmodified.

See big.Int.Not for more details.

func OrInt

func OrInt[T, U intNums](x T, y U) *big.Int

OrInt computes the bitwise OR of two integers and returns the result as a new big.Int. The inputs are unmodified.

See big.Int.Or for more details.

func Phi added in v0.2.1

func Phi(prec uint) *big.Float

Phi is the golden ratio φ constant with the given precision (in bits).

func Pi added in v0.2.1

func Pi(prec uint) *big.Float

Pi is the constant π with the given precision (in bits).

func PowFloat added in v0.2.0

func PowFloat[T, U floatNums](x T, y U) *big.Float

PowFloat computes x^y and returns the result as a new big.Float. The input values are not modified.

See github.com/ALTree/bigfloat.Pow for more details.

func QuoFloat added in v0.2.0

func QuoFloat[T, U floatNums](x T, y U) *big.Float

QuoFloat divides two float values and returns the result as a new big.Float. The input values are not modified.

See big.Float.Quo for more details.

func QuoInt

func QuoInt[T, U intNums](x T, y U) *big.Int

QuoInt divides two integers and returns the quotient as a new big.Int. The inputs are unmodified.

See big.Int.Quo for more details.

func QuoRat

func QuoRat[T, U ratNums](x T, y U) *big.Rat

QuoRat divides two rational values and returns the result as a new big.Rat. The input values are not modified.

See big.Rat.Quo for more details.

func QuoRemInt

func QuoRemInt[T, U intNums](x T, y U) (*big.Int, *big.Int)

QuoRemInt divides two integers and returns the quotient and remainder as two new [big.Int]s. The inputs are unmodified.

See big.Int.QuoRem for more details.

func RandInt

func RandInt[T intNums](r *rand.Rand, x T) *big.Int

RandInt generates a random integer in the range [0, x) and returns it as a new big.Int. The input x is unmodified.

See big.Int.Rand for more details.

func RemInt

func RemInt[T, U intNums](x T, y U) *big.Int

RemInt computes the modulus of two integers and returns the result as a new big.Int. The inputs are unmodified.

See big.Int.Rem for more details.

func RshInt

func RshInt[T, U intNums](x T, y U) *big.Int

RshInt computes the right shift of x by y and returns the result as a new big.Int. The inputs are unmodified.

See big.Int.Rsh for more details.

func SetFloat

func SetFloat[T floatNums](f *big.Float, x T) *big.Float

SetFloat sets the value of the given big.Float to the given float value. The input float value is not modified. If the input is a string, it is parsed using [*math/big.Float).SetString(x.] If the big.Int is nil, a new one is created. Otherwise, the existing one is modified. The function returns the modified big.Float on success. On failure, it returns nil and the state of the big.Float is undefined. The precision of the modified big.Float depends on the input value precision.

func SetInt

func SetInt[T intNums](f *big.Int, x T) *big.Int

SetInt sets the value of the given big.Int to the given integer value. The input integer value is not modified. If the input is a string, it is parsed using big.Int.SetString(x, 0). If the input is a []byte, it is parsed using big.Int.SetBytes(x).

If the big.Int is nil, a new one is created. Otherwise, the existing one is modified. The function returns the modified big.Int on success. On failure, it returns nil and the state of the big.Int is undefined.

func SetMantExpFloat

func SetMantExpFloat[T floatNums](x T, exp int) *big.Float

SetMantExpFloat sets the mantissa and exponent of the given float value and returns the result as a new big.Float. The input value is not modified.

See big.Float.SetMantExp for more details.

func SetModeFloat

func SetModeFloat[T floatNums](x T, mode big.RoundingMode) *big.Float

SetModeFloat sets the rounding mode of the given float value and returns the result as a new big.Float. The input value is not modified.

See big.Float.SetMode for more details.

func SetPrecFloat

func SetPrecFloat[T floatNums](x T, prec uint) *big.Float

SetPrecFloat sets the precision of the given float value and returns the result as a new big.Float. The input value is not modified.

See big.Float.SetPrec for more details.

func SetRat

func SetRat[T ratNums](f *big.Rat, x T) *big.Rat

SetRat sets the value of the given big.Rat to the given rational value. The input rational value is not modified. If the input is a string, it is parsed using [*math/big.Rat.SetString(x)]. If the input is a float, and it can not be converted to a rational big.Rat, it returns nil. The function returns the modified big.Rat on success. On failure, it returns nil and the state of the big.Rat is undefined.

func SetRatFrac

func SetRatFrac[T, U ratNums](f *big.Rat, x T, y U) *big.Rat

SetRatFrac sets the value of the given big.Rat to the given rational values. The inputs are not modified.

func SqrtFloat

func SqrtFloat[T floatNums](x T) *big.Float

SqrtFloat returns the square root of the given float value as a new big.Float. The input value is not modified.

See big.Float.Sqrt for more details.

func SubFloat

func SubFloat[T, U floatNums](x T, y U) *big.Float

SubFloat subtracts two float values and returns the result as a new big.Float. The input values are not modified.

See big.Float.Sub for more details.

func SubInt

func SubInt[T, U intNums](x T, y U) *big.Int

SubInt subtracts two integers and returns the result as a new big.Int. The inputs are unmodified.

See big.Int.Sub for more details.

func SubRat

func SubRat[T, U ratNums](x T, y U) *big.Rat

SubRat subtracts two rational values and returns the result as a new big.Rat. The input values are not modified.

See big.Rat.Sub for more details.

func XorInt

func XorInt[T, U intNums](x T, y U) *big.Int

XorInt computes the bitwise XOR of two integers and returns the result as a new big.Int. The inputs are unmodified.

See big.Int.Xor for more details.

Types

This section is empty.

Jump to

Keyboard shortcuts

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