Documentation
¶
Overview ¶
A package for arbitrary precision arithmethic. It implements the following numeric types:
- Natural unsigned integers
- Integer signed integers
- Rational rational numbers
This package has been designed for ease of use but the functions it provides are likely to be quite slow. It may be deprecated eventually. Use package big instead, if possible.
Index ¶
- func Div128(x1, x0, y uint64) (q, r uint64)
- func Iadd(z, x, y *Integer)
- func Iscale(z *Integer, d int64)
- func Isub(z, x, y *Integer)
- func Mul128(x, y uint64) (z1, z0 uint64)
- func MulAdd128(x, y, c uint64) (z1, z0 uint64)
- func Nadd(zp *Natural, x, y Natural)
- func Nscale(z *Natural, d uint64)
- func Nsub(zp *Natural, x, y Natural)
- type Integer
- func (x *Integer) Abs() Natural
- func (x *Integer) Add(y *Integer) *Integer
- func (x *Integer) And(y *Integer) *Integer
- func (x *Integer) AndNot(y *Integer) *Integer
- func (x *Integer) Cmp(y *Integer) int
- func (x *Integer) Div(y *Integer) *Integer
- func (x *Integer) DivMod(y *Integer) (*Integer, *Integer)
- func (x *Integer) Format(h fmt.State, c int)
- func (x *Integer) IsEven() bool
- func (x *Integer) IsNeg() bool
- func (x *Integer) IsOdd() bool
- func (x *Integer) IsPos() bool
- func (x *Integer) IsZero() bool
- func (x *Integer) Mod(y *Integer) *Integer
- func (x *Integer) Mul(y *Integer) *Integer
- func (x *Integer) Mul1(d int64) *Integer
- func (x *Integer) MulNat(y Natural) *Integer
- func (x *Integer) Neg() *Integer
- func (x *Integer) Not() *Integer
- func (x *Integer) Or(y *Integer) *Integer
- func (x *Integer) Quo(y *Integer) *Integer
- func (x *Integer) QuoRem(y *Integer) (*Integer, *Integer)
- func (x *Integer) Rem(y *Integer) *Integer
- func (x *Integer) Shl(s uint) *Integer
- func (x *Integer) Shr(s uint) *Integer
- func (x *Integer) String() string
- func (x *Integer) Sub(y *Integer) *Integer
- func (x *Integer) ToString(base uint) string
- func (x *Integer) Value() int64
- func (x *Integer) Xor(y *Integer) *Integer
- type Natural
- func (x Natural) Add(y Natural) Natural
- func (x Natural) And(y Natural) Natural
- func (x Natural) AndNot(y Natural) Natural
- func (x Natural) Cmp(y Natural) int
- func (x Natural) Div(y Natural) Natural
- func (x Natural) DivMod(y Natural) (Natural, Natural)
- func (x Natural) Format(h fmt.State, c int)
- func (x Natural) Gcd(y Natural) Natural
- func (x Natural) IsEven() bool
- func (x Natural) IsOdd() bool
- func (x Natural) IsZero() bool
- func (x Natural) Log2() uint
- func (x Natural) Mod(y Natural) Natural
- func (x Natural) Mul(y Natural) Natural
- func (x Natural) Mul1(d uint64) Natural
- func (x Natural) Or(y Natural) Natural
- func (x Natural) Pop() uint
- func (xp Natural) Pow(n uint) Natural
- func (x Natural) Shl(s uint) Natural
- func (x Natural) Shr(s uint) Natural
- func (x Natural) String() string
- func (x Natural) Sub(y Natural) Natural
- func (x Natural) ToString(base uint) string
- func (x Natural) Value() uint64
- func (x Natural) Xor(y Natural) Natural
- type Rational
- func (x *Rational) Add(y *Rational) *Rational
- func (x *Rational) Cmp(y *Rational) int
- func (x *Rational) Format(h fmt.State, c int)
- func (x *Rational) IsInt() bool
- func (x *Rational) IsNeg() bool
- func (x *Rational) IsPos() bool
- func (x *Rational) IsZero() bool
- func (x *Rational) Mul(y *Rational) *Rational
- func (x *Rational) Neg() *Rational
- func (x *Rational) Quo(y *Rational) *Rational
- func (x *Rational) String() string
- func (x *Rational) Sub(y *Rational) *Rational
- func (x *Rational) ToString(base uint) string
- func (x *Rational) Value() (numerator *Integer, denominator Natural)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Integer ¶
type Integer struct {
// contains filtered or unexported fields
}
Integer represents a signed integer value of arbitrary precision.
func IntFromString ¶
IntFromString returns the integer corresponding to the longest possible prefix of s representing an integer in a given conversion base, the actual conversion base used, and the prefix length. The syntax of integers follows the syntax of signed integer literals in Go.
If the base argument 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. Otherwise the selected base is 10.
func MakeInt ¶
MakeInt makes an integer given a sign and a mantissa. The number is positive (>= 0) if sign is false or the mantissa is zero; it is negative otherwise.
func (*Integer) And ¶
And returns the “bitwise and” x & y for the 2's-complement representation of x and y.
func (*Integer) AndNot ¶
AndNot returns the “bitwise clear” x &^ y for the 2's-complement representation of x and y.
func (*Integer) Cmp ¶
Cmp compares x and y. The result is an int value that is
< 0 if x < y == 0 if x == y > 0 if x > y
func (*Integer) Div ¶
Div returns the quotient q = x / y for y != 0. If y == 0, a division-by-zero run-time error occurs.
Div and Mod implement Euclidian division and modulus:
q = x.Div(y) r = x.Mod(y) with: 0 <= r < |q| and: y = x*q + r
(Raymond T. Boute, “The Euclidian 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 (*Integer) Format ¶
Format is a support routine for fmt.Formatter. It accepts the formats 'b' (binary), 'o' (octal), and 'x' (hexadecimal).
func (*Integer) Mod ¶
Mod returns the modulus r of the division x / y for y != 0, with r = x - y*x.Div(y). r is always positive. If y == 0, a division-by-zero run-time error occurs.
func (*Integer) Or ¶
Or returns the “bitwise or” x | y for the 2's-complement representation of x and y.
func (*Integer) Quo ¶
Quo returns the quotient q = x / y for y != 0. If y == 0, a division-by-zero run-time error occurs.
Quo and Rem implement T-division and modulus (like C99):
q = x.Quo(y) = trunc(x/y) (truncation towards zero) r = x.Rem(y) = x - y*q
(Daan Leijen, “Division and Modulus for Computer Scientists”.)
func (*Integer) QuoRem ¶
QuoRem returns the pair (x.Quo(y), x.Rem(y)) for y != 0. If y == 0, a division-by-zero run-time error occurs.
func (*Integer) Rem ¶
Rem returns the remainder r of the division x / y for y != 0, with r = x - y*x.Quo(y). Unless r is zero, its sign corresponds to the sign of x. If y == 0, a division-by-zero run-time error occurs.
func (*Integer) String ¶
String converts x to its decimal string representation. x.String() is the same as x.ToString(10).
type Natural ¶
type Natural []digit
Natural represents an unsigned integer value of arbitrary precision.
func MulRange ¶
MulRange computes the product of all the unsigned integers in the range [a, b] inclusively.
func NatFromString ¶
NatFromString returns the natural number corresponding to the longest possible prefix of s representing a natural number in a given conversion base, the actual conversion base used, and the prefix length. The syntax of natural numbers follows the syntax of unsigned integer literals in Go.
If the base argument 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. Otherwise the selected base is 10.
func (Natural) And ¶
And returns the “bitwise and” x & y for the 2's-complement representation of x and y.
func (Natural) AndNot ¶
AndNot returns the “bitwise clear” x &^ y for the 2's-complement representation of x and y.
func (Natural) Cmp ¶
Cmp compares x and y. The result is an int value
< 0 if x < y == 0 if x == y > 0 if x > y
func (Natural) Div ¶
Div returns the quotient q = x / y for y > 0, with x = y*q + r and 0 <= r < y. If y == 0, a division-by-zero run-time error occurs.
func (Natural) DivMod ¶
DivMod returns the pair (x.Div(y), x.Mod(y)) for y > 0. If y == 0, a division-by-zero run-time error occurs.
func (Natural) Format ¶
Format is a support routine for fmt.Formatter. It accepts the formats 'b' (binary), 'o' (octal), and 'x' (hexadecimal).
func (Natural) Log2 ¶
Log2 computes the binary logarithm of x for x > 0. The result is the integer n for which 2^n <= x < 2^(n+1). If x == 0 a run-time error occurs.
func (Natural) Mod ¶
Mod returns the modulus r of the division x / y for y > 0, with x = y*q + r and 0 <= r < y. If y == 0, a division-by-zero run-time error occurs.
func (Natural) Or ¶
Or returns the “bitwise or” x | y for the 2's-complement representation of x and y.
func (Natural) String ¶
String converts x to its decimal string representation. x.String() is the same as x.ToString(10).
func (Natural) Sub ¶
Sub returns the difference x - y for x >= y. If x < y, an underflow run-time error occurs (use Cmp to test if x >= y).
type Rational ¶
type Rational struct {
// contains filtered or unexported fields
}
Rational represents a quotient a/b of arbitrary precision.
func RatFromString ¶
RatFromString returns the rational number corresponding to the longest possible prefix of s representing a rational number in a given conversion base, the actual conversion base used, and the prefix length. The syntax of a rational number is:
rational = mantissa [exponent] . mantissa = integer ('/' natural | '.' natural) . exponent = ('e'|'E') integer .
If the base argument is 0, the string prefix determines the actual conversion base for the mantissa. A prefix of “0x” or “0X” selects base 16; the “0” prefix selects base 8. Otherwise the selected base is 10. If the mantissa is represented via a division, both the numerator and denominator may have different base prefixes; in that case the base of of the numerator is returned. If the mantissa contains a decimal point, the base for the fractional part is the same as for the part before the decimal point and the fractional part does not accept a base prefix. The base for the exponent is always 10.
func (*Rational) Cmp ¶
Cmp compares x and y. The result is an int value
< 0 if x < y == 0 if x == y > 0 if x > y
func (*Rational) Format ¶
Format is a support routine for fmt.Formatter. It accepts the formats 'b' (binary), 'o' (octal), and 'x' (hexadecimal).
func (*Rational) IsInt ¶
IsInt returns true iff x can be written with a denominator 1 in the form x == x'/1; i.e., if x is an integer value.
func (*Rational) Quo ¶
Quo returns the quotient x / y for y != 0. If y == 0, a division-by-zero run-time error occurs.
func (*Rational) String ¶
String converts x to its decimal string representation. x.String() is the same as x.ToString(10).