Documentation ¶
Overview ¶
Package big implements multi-precision arithmetic (big numbers). The following numeric types are supported:
- Int signed integers
- Rat rational numbers
All methods on Int take the result as the receiver; if it is one of the operands it may be overwritten (and its memory reused). To enable chaining of operations, the result is also returned.
Index ¶
- Constants
- func GcdInt(d, x, y, a, b *Int)
- func ProbablyPrime(z *Int, n int) bool
- 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 (z *Int) Bit(i int) uint
- func (z *Int) BitLen() int
- func (z *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 int)
- func (z *Int) GobDecode(buf []byte) os.Error
- func (z *Int) GobEncode() ([]byte, os.Error)
- func (x *Int) Int64() int64
- func (z *Int) Lsh(x *Int, n uint) *Int
- func (z *Int) Mod(x, y *Int) *Int
- func (z *Int) ModInverse(g, p *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 (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 int) os.Error
- func (z *Int) Set(x *Int) *Int
- func (z *Int) SetBit(x *Int, i int, b uint) *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 (x *Int) Sign() int
- func (x *Int) String() string
- func (z *Int) Sub(x, y *Int) *Int
- 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) (r int)
- func (z *Rat) Denom() *Int
- func (z *Rat) FloatString(prec int) string
- func (z *Rat) GobDecode(buf []byte) os.Error
- func (z *Rat) GobEncode() ([]byte, os.Error)
- func (x *Rat) IsInt() bool
- func (z *Rat) Mul(x, y *Rat) *Rat
- func (z *Rat) Neg(x *Rat) *Rat
- func (z *Rat) Num() *Int
- func (z *Rat) Quo(x, y *Rat) *Rat
- func (z *Rat) RatString() string
- func (z *Rat) Scan(s fmt.ScanState, ch int) os.Error
- func (z *Rat) Set(x *Rat) *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 (z *Rat) String() string
- func (z *Rat) Sub(x, y *Rat) *Rat
- type Word
Constants ¶
const MaxBase = 'z' - 'a' + 10 + 1 // = hexValue('z') + 1
MaxBase is the largest number base accepted for string conversions.
Variables ¶
This section is empty.
Functions ¶
func GcdInt ¶
func GcdInt(d, x, y, a, b *Int)
GcdInt sets d to the greatest common divisor of a and b, which must be positive numbers. If x and y are not nil, GcdInt sets x and y such that d = a*x + b*y. If either a or b is not positive, GcdInt sets d = x = y = 0.
func ProbablyPrime ¶
ProbablyPrime performs n Miller-Rabin tests to check whether z is prime. If it returns true, z is prime with probability 1 - 1/4^n. If it returns false, z is not prime.
Types ¶
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 z. That is, it returns (z>>i)&1. The bit index i must be >= 0.
func (*Int) BitLen ¶
BitLen returns the length of the absolute value of z in bits. The bit length of 0 is 0.
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. 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.)
func (*Int) Exp ¶
Exp sets z = x**y mod m. If m is nil, 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) Int64 ¶
Int64 returns the int64 representation of x. If x cannot be represented in an int64, the result is undefined.
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. See DivMod for more details.
func (*Int) ModInverse ¶
ModInverse sets z to the multiplicative inverse of g in the group ℤ/pℤ (where p is a prime) and returns z.
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) 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. 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”.)
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. 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).
func (*Int) SetBit ¶
SetBit sets the i'th bit of z to bit and returns z. That is, if bit is 1 SetBit sets z = x | (1 << i); if bit is 0 it sets z = x &^ (1 << i). If bit is not 0 or 1, SetBit will panic.
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.
The base argument must be 0 or a value from 2 through 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.
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, 0/0, is not a legal Rat.
func (*Rat) Denom ¶
Denom returns the denominator of z; it is always > 0. The result is a reference to z's denominator; it may change if a new value is assigned to z.
func (*Rat) FloatString ¶
FloatString returns a string representation of z in decimal form with prec digits of precision after the decimal point and the last digit rounded.
func (*Rat) Num ¶
Num returns the numerator of z; it may be <= 0. The result is a reference to z's numerator; it may change if a new value is assigned to z.
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 z 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.
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.