Documentation ΒΆ
Overview ΒΆ
Package big implements multi-precision arithmetic (big numbers). The following numeric types are supported:
Int signed integers Rat rational numbers Float floating-point numbers
Methods are typically of the form:
func (z *T) Unary(x *T) *T // z = op x func (z *T) Binary(x, y *T) *T // z = x op y func (x *T) M() T1 // v = x.M()
with T one of Int, Rat, or Float. For unary and binary operations, the result is the receiver (usually named z in that case); if it is one of the operands x or y it may be overwritten (and its memory reused). To enable chaining of operations, the result is also returned. Methods returning a result other than *Int, *Rat, or *Float take an operand as the receiver (usually named x in that case).
Index ΒΆ
- Constants
- type Accuracy
- type Float
- func (z *Float) Abs(x *Float) *Float
- func (x *Float) Acc() Accuracy
- func (z *Float) Add(x, y *Float) *Float
- func (x *Float) Append(buf []byte, format byte, prec int) []byte
- func (x *Float) Cmp(y *Float) cmpResult
- func (z *Float) Copy(x *Float) *Float
- func (x *Float) Float64() (float64, Accuracy)
- func (x *Float) Format(format byte, prec int) string
- func (x *Float) Int(z *Int) (*Int, Accuracy)
- func (x *Float) Int64() (int64, Accuracy)
- func (x *Float) IsFinite() bool
- func (x *Float) IsInf() bool
- func (x *Float) IsInt() bool
- func (x *Float) IsNaN() bool
- func (x *Float) IsNeg() bool
- func (x *Float) IsZero() bool
- func (x *Float) MantExp(mant *Float) (exp int)
- func (x *Float) MinPrec() uint
- func (x *Float) Mode() RoundingMode
- func (z *Float) Mul(x, y *Float) *Float
- func (z *Float) Neg(x *Float) *Float
- func (z *Float) Parse(s string, base int) (f *Float, b int, err error)
- func (x *Float) Prec() uint
- func (z *Float) Quo(x, y *Float) *Float
- func (x *Float) Rat(z *Rat) (*Rat, Accuracy)
- func (z *Float) Scan(r io.ByteScanner, base int) (f *Float, b int, err error)
- func (z *Float) Set(x *Float) *Float
- func (z *Float) SetFloat64(x float64) *Float
- func (z *Float) SetInf(sign int) *Float
- func (z *Float) SetInt(x *Int) *Float
- func (z *Float) SetInt64(x int64) *Float
- func (z *Float) SetMantExp(mant *Float, exp int) *Float
- func (z *Float) SetMode(mode RoundingMode) *Float
- func (z *Float) SetNaN() *Float
- func (z *Float) SetPrec(prec uint) *Float
- func (z *Float) SetRat(x *Rat) *Float
- func (z *Float) SetString(s string) (*Float, bool)
- func (z *Float) SetUint64(x uint64) *Float
- func (x *Float) Sign() int
- func (x *Float) String() string
- func (z *Float) Sub(x, y *Float) *Float
- func (x *Float) Uint64() (uint64, Accuracy)
- type Int
- func (z *Int) Abs(x *Int) *Int
- func (z *Int) Add(x, y *Int) *Int
- func (z *Int) And(x, y *Int) *Int
- func (z *Int) AndNot(x, y *Int) *Int
- func (z *Int) Binomial(n, k int64) *Int
- func (x *Int) Bit(i int) uint
- func (x *Int) BitLen() int
- func (x *Int) Bits() []Word
- func (x *Int) Bytes() []byte
- func (x *Int) Cmp(y *Int) (r int)
- func (z *Int) Div(x, y *Int) *Int
- func (z *Int) DivMod(x, y, m *Int) (*Int, *Int)
- func (z *Int) Exp(x, y, m *Int) *Int
- func (x *Int) Format(s fmt.State, ch rune)
- func (z *Int) GCD(x, y, a, b *Int) *Int
- func (z *Int) GobDecode(buf []byte) error
- func (x *Int) GobEncode() ([]byte, error)
- func (x *Int) Int64() int64
- func (z *Int) Lsh(x *Int, n uint) *Int
- func (z *Int) MarshalJSON() ([]byte, error)
- func (z *Int) MarshalText() (text []byte, err error)
- func (z *Int) Mod(x, y *Int) *Int
- func (z *Int) ModInverse(g, n *Int) *Int
- func (z *Int) Mul(x, y *Int) *Int
- func (z *Int) MulRange(a, b int64) *Int
- func (z *Int) Neg(x *Int) *Int
- func (z *Int) Not(x *Int) *Int
- func (z *Int) Or(x, y *Int) *Int
- func (x *Int) ProbablyPrime(n int) bool
- func (z *Int) Quo(x, y *Int) *Int
- func (z *Int) QuoRem(x, y, r *Int) (*Int, *Int)
- func (z *Int) Rand(rnd *rand.Rand, n *Int) *Int
- func (z *Int) Rem(x, y *Int) *Int
- func (z *Int) Rsh(x *Int, n uint) *Int
- func (z *Int) Scan(s fmt.ScanState, ch rune) error
- func (z *Int) Set(x *Int) *Int
- func (z *Int) SetBit(x *Int, i int, b uint) *Int
- func (z *Int) SetBits(abs []Word) *Int
- func (z *Int) SetBytes(buf []byte) *Int
- func (z *Int) SetInt64(x int64) *Int
- func (z *Int) SetString(s string, base int) (*Int, bool)
- func (z *Int) SetUint64(x uint64) *Int
- func (x *Int) Sign() int
- func (x *Int) String() string
- func (z *Int) Sub(x, y *Int) *Int
- func (x *Int) Uint64() uint64
- func (z *Int) UnmarshalJSON(text []byte) error
- func (z *Int) UnmarshalText(text []byte) error
- func (z *Int) Xor(x, y *Int) *Int
- type Rat
- func (z *Rat) Abs(x *Rat) *Rat
- func (z *Rat) Add(x, y *Rat) *Rat
- func (x *Rat) Cmp(y *Rat) int
- func (x *Rat) Denom() *Int
- func (x *Rat) Float32() (f float32, exact bool)
- func (x *Rat) Float64() (f float64, exact bool)
- func (x *Rat) FloatString(prec int) string
- func (z *Rat) GobDecode(buf []byte) error
- func (x *Rat) GobEncode() ([]byte, error)
- func (z *Rat) Inv(x *Rat) *Rat
- func (x *Rat) IsInt() bool
- func (r *Rat) MarshalText() (text []byte, err error)
- func (z *Rat) Mul(x, y *Rat) *Rat
- func (z *Rat) Neg(x *Rat) *Rat
- func (x *Rat) Num() *Int
- func (z *Rat) Quo(x, y *Rat) *Rat
- func (x *Rat) RatString() string
- func (z *Rat) Scan(s fmt.ScanState, ch rune) error
- func (z *Rat) Set(x *Rat) *Rat
- func (z *Rat) SetFloat64(f float64) *Rat
- func (z *Rat) SetFrac(a, b *Int) *Rat
- func (z *Rat) SetFrac64(a, b int64) *Rat
- func (z *Rat) SetInt(x *Int) *Rat
- func (z *Rat) SetInt64(x int64) *Rat
- func (z *Rat) SetString(s string) (*Rat, bool)
- func (x *Rat) Sign() int
- func (x *Rat) String() string
- func (z *Rat) Sub(x, y *Rat) *Rat
- func (r *Rat) UnmarshalText(text []byte) error
- type RoundingMode
- type Word
- Bugs
Examples ΒΆ
Constants ΒΆ
const ( MaxExp = math.MaxInt32 // largest supported exponent MinExp = math.MinInt32 // smallest supported exponent MaxPrec = math.MaxUint32 // largest (theoretically) supported precision; likely memory-limited )
Exponent and precision limits.
const MaxBase = 'z' - 'a' + 10 + 1
MaxBase is the largest number base accepted for string conversions.
Variables ΒΆ
This section is empty.
Functions ΒΆ
This section is empty.
Types ΒΆ
type Accuracy ΒΆ
type Accuracy byte
Accuracy describes the rounding error produced by the most recent operation that generated a Float value, relative to the exact value. The accuracy is Undef for operations on and resulting in NaNs since they are neither Below nor Above any other value.
type Float ΒΆ
type Float struct {
// contains filtered or unexported fields
}
A nonzero finite Float represents a multi-precision floating point number
sign Γ mantissa Γ 2**exponent
with 0.5 <= mantissa < 1.0, and MinExp <= exponent <= MaxExp. A Float may also be zero (+0, -0), infinite (+Inf, -Inf) or not-a-number (NaN). Except for NaNs, all Floats are ordered, and the ordering of two Floats x and y is defined by x.Cmp(y). NaNs are always different from any other Float value.
Each Float value also has a precision, rounding mode, and accuracy. The precision is the maximum number of mantissa bits available to represent the value. The rounding mode specifies how a result should be rounded to fit into the mantissa bits, and accuracy describes the rounding error with respect to the exact result.
All operations, including setters, that specify a *Float variable for the result (usually via the receiver with the exception of MantExp), round the numeric result according to the precision and rounding mode of the result variable, unless specified otherwise.
If the provided result precision is 0 (see below), it is set to the precision of the argument with the largest precision value before any rounding takes place, and the rounding mode remains unchanged. Thus, uninitialized Floats provided as result arguments will have their precision set to a reasonable value determined by the operands and their mode is the zero value for RoundingMode (ToNearestEven).
By setting the desired precision to 24 or 53 and using matching rounding mode (typically ToNearestEven), Float operations produce the same results as the corresponding float32 or float64 IEEE-754 arithmetic. Exponent underflow and overflow lead to a 0 or an Infinity for different values than IEEE-754 because Float exponents have a much larger range.
The zero (uninitialized) value for a Float is ready to use and represents the number +0.0 exactly, with precision 0 and rounding mode ToNearestEven.
func NewFloat ΒΆ
NewFloat allocates and returns a new Float set to x, with precision 53 and rounding mode ToNearestEven.
func ParseFloat ΒΆ
ParseFloat is like f.Parse(s, base) with f set to the given precision and rounding mode.
func ScanFloat ΒΆ
func ScanFloat(r io.ByteScanner, base int, prec uint, mode RoundingMode) (f *Float, b int, err error)
ScanFloat is like f.Scan(r, base) with f set to the given precision and rounding mode.
func (*Float) Abs ΒΆ
Abs sets z to the (possibly rounded) value |x| (the absolute value of x) and returns z.
func (*Float) Add ΒΆ
Add sets z to the rounded sum x+y and returns z. If z's precision is 0, it is changed to the larger of x's or y's precision before the operation. Rounding is performed according to z's precision and rounding mode; and z's accuracy reports the result error relative to the exact (not rounded) result. BUG(gri) Float.Add returns NaN if an operand is Inf. BUG(gri) When rounding ToNegativeInf, the sign of Float values rounded to 0 is incorrect.
Example ΒΆ
package main import ( "fmt" "math/big" ) func main() { // Operating on numbers of different precision. var x, y, z big.Float x.SetInt64(1000) // x is automatically set to 64bit precision y.SetFloat64(2.718281828) // y is automatically set to 53bit precision z.SetPrec(32) z.Add(&x, &y) fmt.Printf("x = %s (%s, prec = %d, acc = %s)\n", &x, x.Format('p', 0), x.Prec(), x.Acc()) fmt.Printf("y = %s (%s, prec = %d, acc = %s)\n", &y, y.Format('p', 0), y.Prec(), y.Acc()) fmt.Printf("z = %s (%s, prec = %d, acc = %s)\n", &z, z.Format('p', 0), z.Prec(), z.Acc()) }
Output: x = 1000 (0x.fap10, prec = 64, acc = Exact) y = 2.718281828 (0x.adf85458248cd8p2, prec = 53, acc = Exact) z = 1002.718282 (0x.faadf854p10, prec = 32, acc = Below)
func (*Float) Append ΒΆ
Append appends the string form of the floating-point number x, as generated by x.Format, to buf and returns the extended buffer.
func (*Float) Cmp ΒΆ
Cmp compares x and y and returns:
Below if x < y Exact if x == y (incl. -0 == 0, -Inf == -Inf, and +Inf == +Inf) Above if x > y Undef if any of x, y is NaN
Example ΒΆ
package main import ( "fmt" "math" "math/big" ) func main() { inf := math.Inf(1) zero := 0.0 nan := math.NaN() operands := []float64{-inf, -1.2, -zero, 0, +1.2, +inf, nan} fmt.Println(" x y cmp eql neq lss leq gtr geq") fmt.Println("-----------------------------------------------") for _, x64 := range operands { x := big.NewFloat(x64) for _, y64 := range operands { y := big.NewFloat(y64) t := x.Cmp(y) fmt.Printf( "%4s %4s %5s %c %c %c %c %c %c\n", x, y, t.Acc(), mark(t.Eql()), mark(t.Neq()), mark(t.Lss()), mark(t.Leq()), mark(t.Gtr()), mark(t.Geq())) } fmt.Println() } } func mark(p bool) rune { if p { return 'β' } return 'β' }
Output: x y cmp eql neq lss leq gtr geq ----------------------------------------------- -Inf -Inf Exact β β β β β β -Inf -1.2 Below β β β β β β -Inf -0 Below β β β β β β -Inf 0 Below β β β β β β -Inf 1.2 Below β β β β β β -Inf +Inf Below β β β β β β -Inf NaN Undef β β β β β β -1.2 -Inf Above β β β β β β -1.2 -1.2 Exact β β β β β β -1.2 -0 Below β β β β β β -1.2 0 Below β β β β β β -1.2 1.2 Below β β β β β β -1.2 +Inf Below β β β β β β -1.2 NaN Undef β β β β β β -0 -Inf Above β β β β β β -0 -1.2 Above β β β β β β -0 -0 Exact β β β β β β -0 0 Exact β β β β β β -0 1.2 Below β β β β β β -0 +Inf Below β β β β β β -0 NaN Undef β β β β β β 0 -Inf Above β β β β β β 0 -1.2 Above β β β β β β 0 -0 Exact β β β β β β 0 0 Exact β β β β β β 0 1.2 Below β β β β β β 0 +Inf Below β β β β β β 0 NaN Undef β β β β β β 1.2 -Inf Above β β β β β β 1.2 -1.2 Above β β β β β β 1.2 -0 Above β β β β β β 1.2 0 Above β β β β β β 1.2 1.2 Exact β β β β β β 1.2 +Inf Below β β β β β β 1.2 NaN Undef β β β β β β +Inf -Inf Above β β β β β β +Inf -1.2 Above β β β β β β +Inf -0 Above β β β β β β +Inf 0 Above β β β β β β +Inf 1.2 Above β β β β β β +Inf +Inf Exact β β β β β β +Inf NaN Undef β β β β β β NaN -Inf Undef β β β β β β NaN -1.2 Undef β β β β β β NaN -0 Undef β β β β β β NaN 0 Undef β β β β β β NaN 1.2 Undef β β β β β β NaN +Inf Undef β β β β β β NaN NaN Undef β β β β β β
func (*Float) Copy ΒΆ
Copy sets z to x, with the same precision, rounding mode, and accuracy as x, and returns z. x is not changed even if z and x are the same.
func (*Float) Float64 ΒΆ
Float64 returns the closest float64 value of x by rounding to nearest with 53 bits precision. BUG(gri) Float.Float64 doesn't handle exponent overflow.
func (*Float) Format ΒΆ
Format converts the floating-point number x to a string according to the given format and precision prec. The format is one of:
'e' -d.ddddeΒ±dd, decimal exponent, at least two (possibly 0) exponent digits 'E' -d.ddddEΒ±dd, decimal exponent, at least two (possibly 0) exponent digits 'f' -ddddd.dddd, no exponent 'g' like 'e' for large exponents, like 'f' otherwise 'G' like 'E' for large exponents, like 'f' otherwise 'b' -ddddddpΒ±dd, binary exponent 'p' -0x.dddpΒ±dd, binary exponent, hexadecimal mantissa
For the binary exponent formats, the mantissa is printed in normalized form:
'b' decimal integer mantissa using x.Prec() bits, or -0 'p' hexadecimal fraction with 0.5 <= 0.mantissa < 1.0, or -0
The precision prec controls the number of digits (excluding the exponent) printed by the 'e', 'E', 'f', 'g', and 'G' formats. For 'e', 'E', and 'f' it is the number of digits after the decimal point. For 'g' and 'G' it is the total number of digits. A negative precision selects the smallest number of digits necessary such that ParseFloat will return f exactly. The prec value is ignored for the 'b' or 'p' format.
BUG(gri) Float.Format does not accept negative precisions.
func (*Float) Int ΒΆ
Int returns the result of truncating x towards zero; or nil if x is an infinity or NaN. The result is Exact if x.IsInt(); otherwise it is Below for x > 0, and Above for x < 0. If a non-nil *Int argument z is provided, Int stores the result in z instead of allocating a new Int.
func (*Float) Int64 ΒΆ
Int64 returns the integer resulting from truncating x towards zero. If math.MinInt64 <= x <= math.MaxInt64, the result is Exact if x is an integer, and Above (x < 0) or Below (x > 0) otherwise. The result is (math.MinInt64, Above) for x < math.MinInt64, (math.MaxInt64, Below) for x > math.MaxInt64, and (0, Undef) for NaNs.
func (*Float) IsInt ΒΆ
IsInt reports whether x is an integer. Β±Inf and NaN values are not integers.
func (*Float) MantExp ΒΆ
MantExp breaks x into its mantissa and exponent components and returns the exponent. If a non-nil mant argument is provided its value is set to the mantissa of x, with the same precision and rounding mode as x. The components satisfy x == mant Γ 2**exp, with 0.5 <= |mant| < 1.0. Calling MantExp with a nil argument is an efficient way to get the exponent of the receiver.
Special cases are:
( Β±0).MantExp(mant) = 0, with mant set to Β±0 (Β±Inf).MantExp(mant) = 0, with mant set to Β±Inf ( NaN).MantExp(mant) = 0, with mant set to NaN
x and mant may be the same in which case x is set to its mantissa value.
func (*Float) MinPrec ΒΆ
MinPrec returns the minimum precision required to represent x exactly (i.e., the smallest prec before x.SetPrec(prec) would start rounding x). The result is 0 if x is 0 or not finite.
func (*Float) Mul ΒΆ
Mul sets z to the rounded product x*y and returns z. Precision, rounding, and accuracy reporting are as for Add. BUG(gri) Float.Mul returns NaN if an operand is Inf.
func (*Float) Neg ΒΆ
Neg sets z to the (possibly rounded) value of x with its sign negated, and returns z.
func (*Float) Parse ΒΆ
Parse is like z.Scan(r, base), but instead of reading from an io.ByteScanner, it parses the string s. An error is also returned if the string contains invalid or trailing bytes not belonging to the number.
func (*Float) Prec ΒΆ
Prec returns the mantissa precision of x in bits. The result may be 0 for |x| == 0, |x| == Inf, or NaN.
func (*Float) Quo ΒΆ
Quo sets z to the rounded quotient x/y and returns z. Precision, rounding, and accuracy reporting are as for Add. BUG(gri) Float.Quo returns NaN if an operand is Inf.
func (*Float) Rat ΒΆ
Rat returns the rational number corresponding to x; or nil if x is an infinity or NaN. The result is Exact is x is not an Inf or NaN. If a non-nil *Rat argument z is provided, Rat stores the result in z instead of allocating a new Rat.
func (*Float) Scan ΒΆ
Scan scans the number corresponding to the longest possible prefix of r representing a floating-point number with a mantissa in the given conversion base (the exponent is always a decimal number). It sets z to the (possibly rounded) value of the corresponding floating-point number, and returns z, the actual base b, and an error err, if any. If z's precision is 0, it is changed to 64 before rounding takes effect. The number must be of the form:
number = [ sign ] [ prefix ] mantissa [ exponent ] . sign = "+" | "-" . prefix = "0" ( "x" | "X" | "b" | "B" ) . mantissa = digits | digits "." [ digits ] | "." digits . exponent = ( "E" | "e" | "p" ) [ sign ] digits . digits = digit { digit } . digit = "0" ... "9" | "a" ... "z" | "A" ... "Z" .
The base argument must be 0, 2, 10, or 16. Providing an invalid base argument will lead to a run-time panic.
For base 0, the number prefix determines the actual base: A prefix of "0x" or "0X" selects base 16, and a "0b" or "0B" prefix selects base 2; otherwise, the actual base is 10 and no prefix is accepted. The octal prefix "0" is not supported (a leading "0" is simply considered a "0").
A "p" exponent indicates a binary (rather then decimal) exponent; for instance "0x1.fffffffffffffp1023" (using base 0) represents the maximum float64 value. For hexadecimal mantissae, the exponent must be binary, if present (an "e" or "E" exponent indicator cannot be distinguished from a mantissa digit).
The returned *Float f is nil and the value of z is valid but not defined if an error is reported.
BUG(gri) The Float.Scan signature conflicts with Scan(s fmt.ScanState, ch rune) error.
func (*Float) Set ΒΆ
Set sets z to the (possibly rounded) value of x and returns z. If z's precision is 0, it is changed to the precision of x before setting z (and rounding will have no effect). Rounding is performed according to z's precision and rounding mode; and z's accuracy reports the result error relative to the exact (not rounded) result.
func (*Float) SetFloat64 ΒΆ
SetFloat64 sets z to the (possibly rounded) value of x and returns z. If z's precision is 0, it is changed to 53 (and rounding will have no effect).
func (*Float) SetInf ΒΆ
SetInf sets z to the infinite Float +Inf for sign >= 0, or -Inf for sign < 0, and returns z. The precision of z is unchanged and the result is always Exact.
func (*Float) SetInt ΒΆ
SetInt sets z to the (possibly rounded) value of x and returns z. If z's precision is 0, it is changed to the larger of x.BitLen() or 64 (and rounding will have no effect).
func (*Float) SetInt64 ΒΆ
SetInt64 sets z to the (possibly rounded) value of x and returns z. If z's precision is 0, it is changed to 64 (and rounding will have no effect).
func (*Float) SetMantExp ΒΆ
SetMantExp sets z to mant Γ 2**exp and and returns z. The result z has the same precision and rounding mode as mant. SetMantExp is an inverse of MantExp but does not require 0.5 <= |mant| < 1.0. Specifically:
mant := new(Float) new(Float).SetMantExp(mant, x.SetMantExp(mant)).Cmp(x).Eql() is true
Special cases are:
z.SetMantExp( Β±0, exp) = Β±0 z.SetMantExp(Β±Inf, exp) = Β±Inf z.SetMantExp( NaN, exp) = NaN
z and mant may be the same in which case z's exponent is set to exp.
func (*Float) SetMode ΒΆ
func (z *Float) SetMode(mode RoundingMode) *Float
SetMode sets z's rounding mode to mode and returns an exact z. z remains unchanged otherwise. z.SetMode(z.Mode()) is a cheap way to set z's accuracy to Exact.
func (*Float) SetNaN ΒΆ
SetNaN sets z to a NaN value, and returns z. The precision of z is unchanged and the result accuracy is always Undef.
func (*Float) SetPrec ΒΆ
SetPrec sets z's precision to prec and returns the (possibly) rounded value of z. Rounding occurs according to z's rounding mode if the mantissa cannot be represented in prec bits without loss of precision. SetPrec(0) maps all finite values to Β±0; infinite and NaN values remain unchanged. If prec > MaxPrec, it is set to MaxPrec.
func (*Float) SetRat ΒΆ
SetRat sets z to the (possibly rounded) value of x and returns z. If z's precision is 0, it is changed to the largest of a.BitLen(), b.BitLen(), or 64; with x = a/b.
func (*Float) SetString ΒΆ
SetString sets z to the value of s and returns z and a boolean indicating success. s must be a floating-point number of the same format as accepted by Scan, with number prefixes permitted.
func (*Float) SetUint64 ΒΆ
SetUint64 sets z to the (possibly rounded) value of x and returns z. If z's precision is 0, it is changed to 64 (and rounding will have no effect).
func (*Float) String ΒΆ
BUG(gri): Float.String uses x.Format('g', 10) rather than x.Format('g', -1).
func (*Float) Sub ΒΆ
Sub sets z to the rounded difference x-y and returns z. Precision, rounding, and accuracy reporting are as for Add. BUG(gri) Float.Sub returns NaN if an operand is Inf.
type Int ΒΆ
type Int struct {
// contains filtered or unexported fields
}
An Int represents a signed multi-precision integer. The zero value for an Int represents the value 0.
func (*Int) Bit ΒΆ
Bit returns the value of the i'th bit of x. That is, it returns (x>>i)&1. The bit index i must be >= 0.
func (*Int) BitLen ΒΆ
BitLen returns the length of the absolute value of x in bits. The bit length of 0 is 0.
func (*Int) Bits ΒΆ
Bits provides raw (unchecked but fast) access to x by returning its absolute value as a little-endian Word slice. The result and x share the same underlying array. Bits is intended to support implementation of missing low-level Int functionality outside this package; it should be avoided otherwise.
func (*Int) Div ΒΆ
Div sets z to the quotient x/y for y != 0 and returns z. If y == 0, a division-by-zero run-time panic occurs. Div implements Euclidean division (unlike Go); see DivMod for more details.
func (*Int) DivMod ΒΆ
DivMod sets z to the quotient x div y and m to the modulus x mod y and returns the pair (z, m) for y != 0. If y == 0, a division-by-zero run-time panic occurs.
DivMod implements Euclidean division and modulus (unlike Go):
q = x div y such that m = x - y*q with 0 <= m < |q|
(See Raymond T. Boute, βThe Euclidean definition of the functions div and modβ. ACM Transactions on Programming Languages and Systems (TOPLAS), 14(2):127-144, New York, NY, USA, 4/1992. ACM press.) See QuoRem for T-division and modulus (like Go).
func (*Int) Exp ΒΆ
Exp sets z = x**y mod |m| (i.e. the sign of m is ignored), and returns z. If y <= 0, the result is 1 mod |m|; if m == nil or m == 0, z = x**y. See Knuth, volume 2, section 4.6.3.
func (*Int) Format ΒΆ
Format is a support routine for fmt.Formatter. It accepts the formats 'b' (binary), 'o' (octal), 'd' (decimal), 'x' (lowercase hexadecimal), and 'X' (uppercase hexadecimal). Also supported are the full suite of package fmt's format verbs for integral types, including '+', '-', and ' ' for sign control, '#' for leading zero in octal and for hexadecimal, a leading "0x" or "0X" for "%#x" and "%#X" respectively, specification of minimum digits precision, output field width, space or zero padding, and left or right justification.
func (*Int) GCD ΒΆ
GCD sets z to the greatest common divisor of a and b, which both must be > 0, and returns z. If x and y are not nil, GCD sets x and y such that z = a*x + b*y. If either a or b is <= 0, GCD sets z = x = y = 0.
func (*Int) Int64 ΒΆ
Int64 returns the int64 representation of x. If x cannot be represented in an int64, the result is undefined.
func (*Int) MarshalJSON ΒΆ
MarshalJSON implements the json.Marshaler interface.
func (*Int) MarshalText ΒΆ
MarshalText implements the encoding.TextMarshaler interface.
func (*Int) Mod ΒΆ
Mod sets z to the modulus x%y for y != 0 and returns z. If y == 0, a division-by-zero run-time panic occurs. Mod implements Euclidean modulus (unlike Go); see DivMod for more details.
func (*Int) ModInverse ΒΆ
ModInverse sets z to the multiplicative inverse of g in the ring β€/nβ€ and returns z. If g and n are not relatively prime, the result is undefined.
func (*Int) MulRange ΒΆ
MulRange sets z to the product of all integers in the range [a, b] inclusively and returns z. If a > b (empty range), the result is 1.
func (*Int) ProbablyPrime ΒΆ
ProbablyPrime performs n Miller-Rabin tests to check whether x is prime. If it returns true, x is prime with probability 1 - 1/4^n. If it returns false, x is not prime. n must be > 0.
func (*Int) Quo ΒΆ
Quo sets z to the quotient x/y for y != 0 and returns z. If y == 0, a division-by-zero run-time panic occurs. Quo implements truncated division (like Go); see QuoRem for more details.
func (*Int) QuoRem ΒΆ
QuoRem sets z to the quotient x/y and r to the remainder x%y and returns the pair (z, r) for y != 0. If y == 0, a division-by-zero run-time panic occurs.
QuoRem implements T-division and modulus (like Go):
q = x/y with the result truncated to zero r = x - y*q
(See Daan Leijen, βDivision and Modulus for Computer Scientistsβ.) See DivMod for Euclidean division and modulus (unlike Go).
func (*Int) Rem ΒΆ
Rem sets z to the remainder x%y for y != 0 and returns z. If y == 0, a division-by-zero run-time panic occurs. Rem implements truncated modulus (like Go); see QuoRem for more details.
func (*Int) Scan ΒΆ
Scan is a support routine for fmt.Scanner; it sets z to the value of the scanned number. It accepts the formats 'b' (binary), 'o' (octal), 'd' (decimal), 'x' (lowercase hexadecimal), and 'X' (uppercase hexadecimal).
Example ΒΆ
package main import ( "fmt" "log" "math/big" ) func main() { // The Scan function is rarely used directly; // the fmt package recognizes it as an implementation of fmt.Scanner. i := new(big.Int) _, err := fmt.Sscan("18446744073709551617", i) if err != nil { log.Println("error scanning value:", err) } else { fmt.Println(i) } }
Output: 18446744073709551617
func (*Int) SetBit ΒΆ
SetBit sets z to x, with x's i'th bit set to b (0 or 1). That is, if b is 1 SetBit sets z = x | (1 << i); if b is 0 SetBit sets z = x &^ (1 << i). If b is not 0 or 1, SetBit will panic.
func (*Int) SetBits ΒΆ
SetBits provides raw (unchecked but fast) access to z by setting its value to abs, interpreted as a little-endian Word slice, and returning z. The result and abs share the same underlying array. SetBits is intended to support implementation of missing low-level Int functionality outside this package; it should be avoided otherwise.
func (*Int) SetBytes ΒΆ
SetBytes interprets buf as the bytes of a big-endian unsigned integer, sets z to that value, and returns z.
func (*Int) SetString ΒΆ
SetString sets z to the value of s, interpreted in the given base, and returns z and a boolean indicating success. If SetString fails, the value of z is undefined but the returned value is nil.
The base argument must be 0 or a value between 2 and MaxBase. If the base is 0, the string prefix determines the actual conversion base. A prefix of β0xβ or β0Xβ selects base 16; the β0β prefix selects base 8, and a β0bβ or β0Bβ prefix selects base 2. Otherwise the selected base is 10.
Example ΒΆ
package main import ( "fmt" "math/big" ) func main() { i := new(big.Int) i.SetString("644", 8) // octal fmt.Println(i) }
Output: 420
func (*Int) Uint64 ΒΆ
Uint64 returns the uint64 representation of x. If x cannot be represented in a uint64, the result is undefined.
func (*Int) UnmarshalJSON ΒΆ
UnmarshalJSON implements the json.Unmarshaler interface.
func (*Int) UnmarshalText ΒΆ
UnmarshalText implements the encoding.TextUnmarshaler interface.
type Rat ΒΆ
type Rat struct {
// contains filtered or unexported fields
}
A Rat represents a quotient a/b of arbitrary precision. The zero value for a Rat represents the value 0.
func (*Rat) Denom ΒΆ
Denom returns the denominator of x; it is always > 0. The result is a reference to x's denominator; it may change if a new value is assigned to x, and vice versa.
func (*Rat) Float32 ΒΆ
Float32 returns the nearest float32 value for x and a bool indicating whether f represents x exactly. If the magnitude of x is too large to be represented by a float32, f is an infinity and exact is false. The sign of f always matches the sign of x, even if f == 0.
func (*Rat) Float64 ΒΆ
Float64 returns the nearest float64 value for x and a bool indicating whether f represents x exactly. If the magnitude of x is too large to be represented by a float64, f is an infinity and exact is false. The sign of f always matches the sign of x, even if f == 0.
func (*Rat) FloatString ΒΆ
FloatString returns a string representation of x in decimal form with prec digits of precision after the decimal point and the last digit rounded.
func (*Rat) MarshalText ΒΆ
MarshalText implements the encoding.TextMarshaler interface.
func (*Rat) Num ΒΆ
Num returns the numerator of x; it may be <= 0. The result is a reference to x's numerator; it may change if a new value is assigned to x, and vice versa. The sign of the numerator corresponds to the sign of x.
func (*Rat) Quo ΒΆ
Quo sets z to the quotient x/y and returns z. If y == 0, a division-by-zero run-time panic occurs.
func (*Rat) RatString ΒΆ
RatString returns a string representation of x in the form "a/b" if b != 1, and in the form "a" if b == 1.
func (*Rat) Scan ΒΆ
Scan is a support routine for fmt.Scanner. It accepts the formats 'e', 'E', 'f', 'F', 'g', 'G', and 'v'. All formats are equivalent.
Example ΒΆ
package main import ( "fmt" "log" "math/big" ) func main() { // The Scan function is rarely used directly; // the fmt package recognizes it as an implementation of fmt.Scanner. r := new(big.Rat) _, err := fmt.Sscan("1.5000", r) if err != nil { log.Println("error scanning value:", err) } else { fmt.Println(r) } }
Output: 3/2
func (*Rat) SetFloat64 ΒΆ
SetFloat64 sets z to exactly f and returns z. If f is not finite, SetFloat returns nil.
func (*Rat) SetString ΒΆ
SetString sets z to the value of s and returns z and a boolean indicating success. s can be given as a fraction "a/b" or as a floating-point number optionally followed by an exponent. If the operation failed, the value of z is undefined but the returned value is nil.
Example ΒΆ
package main import ( "fmt" "math/big" ) func main() { r := new(big.Rat) r.SetString("355/113") fmt.Println(r.FloatString(3)) }
Output: 3.142
func (*Rat) String ΒΆ
String returns a string representation of x in the form "a/b" (even if b == 1).
func (*Rat) UnmarshalText ΒΆ
UnmarshalText implements the encoding.TextUnmarshaler interface.
type RoundingMode ΒΆ
type RoundingMode byte
RoundingMode determines how a Float value is rounded to the desired precision. Rounding may change the Float value; the rounding error is described by the Float's Accuracy.
const ( ToNearestEven RoundingMode = iota // == IEEE 754-2008 roundTiesToEven ToNearestAway // == IEEE 754-2008 roundTiesToAway ToZero // == IEEE 754-2008 roundTowardZero AwayFromZero // no IEEE 754-2008 equivalent ToNegativeInf // == IEEE 754-2008 roundTowardNegative ToPositiveInf // == IEEE 754-2008 roundTowardPositive )
The following rounding modes are supported.
func (RoundingMode) String ΒΆ
func (i RoundingMode) String() string
Notes ΒΆ
Bugs ΒΆ
Float.Float64 doesn't handle exponent overflow.
Float.Add returns NaN if an operand is Inf.
When rounding ToNegativeInf, the sign of Float values rounded to 0 is incorrect.
Float.Sub returns NaN if an operand is Inf.
Float.Mul returns NaN if an operand is Inf.
Float.Quo returns NaN if an operand is Inf.
The Float.Scan signature conflicts with Scan(s fmt.ScanState, ch rune) error.
Float.Format does not accept negative precisions.
Float.String uses x.Format('g', 10) rather than x.Format('g', -1).