BigInteger
Usage
use BigInteger;
or
import BigInteger;
Provides a ‘bigint’ type and supporting math operations.
The bigint type supports arithmetic operations on arbitrary
precision integers in a manner that is broadly consistent with
the conventional operations on primitive fixed length integers.
The current implementation is based on the low-level types and
functions defined in the GMP module i.e. it is implemented using the
GNU Multiple Precision Integer Arithmetic library (GMP). More specifically,
bigint wraps the GMP type mpz_t.
The primary benefits of bigint over mpz_t are
support for multi-locale programs
the convenience of arithmetic operator overloads
automatic memory management of GMP data structures
In addition to the expected set of operations, this record provides a number of methods that wrap GMP functions in a natural way:
use BigInteger;
var a = new bigint(234958444);
const b = new bigint("4847382292989382987395534934347");
var c = new bigint();
writeln(a * b);
fac(c, 00);
writeln(c);
Casting and declarations can be used to create bigint records as
well:
use BigInteger;
var a = 234958444: bigint;
const b = "4847382292989382987395534934347": bigint;
var c: bigint;
Warning
Creating a bigint from an integer literal that is larger than
max(uint(64)) would cause integer overflow before the
bigint is created, and so results in a compile-time error.
Strings should be used instead of integer literals for cases
like this:
// These would result in integer overflow and cause compile-time errors
var bad1 = 4847382292989382987395534934347: bigint;
var bad2 = new bigint(4847382292989382987395534934347);
// These give the desired result
var good1 = "4847382292989382987395534934347": bigint;
var good2 = new bigint("4847382292989382987395534934347");
Wrapping an mpz_t in a bigint record may introduce a
measurable overhead in some cases.
The GMP library defines a low-level API that is based on side-effecting compound operations. The documentation recommends that one prefer to reuse a small number of existing mpz_t structures rather than using many values of short duration.
Matching this style using bigint records and the compound
assignment operators is likely to provide comparable performance to an
implementation based on mpz_t. So, for example:
x = b;
x *= c;
x += a;
is likely to achieve better performance than:
x = a + b * c;
The Chapel compiler currently introduces two short lived temporaries for the intermediate results of the binary operators.
The operators on bigint include variations that accept Chapel
integers e.g.:
var a = new bigint("9738639463465935");
var b = 939573915 * a;
The Chapel int(64) literal is converted to an underlying, platform-specific C integer, to invoke the underlying GMP primitive function. This example is likely to work well on popular 64-bit platforms but to fail on common 32-bit platforms. Runtime checks are used to ensure the Chapel types can safely be cast to the platform-specific types. Ths program will halt if the Chapel value cannot be represented using the GMP scalar type.
The checks are controlled by the compiler options --[no-]cast-checks,
--fast, etc.
Casting from bigint to integral and real types is also
supported. Values that are too large for the resultant type are
truncated. GMP primitives are used to first cast to platform-specific C
types, which are then cast to Chapel types. As a result, casting to
64-bit types on 32-bit platforms may result in additional truncation.
Additionally, casting a negative bigint to a uint will result in
the absolute value truncated to fit within the type:
var a = new bigint(-1);
writeln(a:uint); // prints "1"
See GMP for more information on how to use GMP with Chapel.
- record bigint : serializable
The bigint type supports arithmetic operations on arbitrary precision integers across multiple locales.
- proc init(const ref x: bigint)
Initializes a
bigintto the value ofx.
- proc init(x: int(?))
See
init
- proc init(x: uint(?))
See
init
- proc init(x: string, base: int = 0) throws
Initialize a
bigintfrom a string and optionally a provided base to use with the string. If the string is not a correct basebasenumber, will throw aBadFormatError.- Arguments:
x :
string– The value to be stored in the resultingbigint.base :
int– The base to use when creating thebigintfromx. May vary from2to62or be0. Defaults to0, which causes the base to be read from the start of thexitself (0xand0Xwill give hexadecimal,0band0Bwill give binary,0will give octal, and everything else will be interpreted as decimal).
- Throws:
BadFormatError – Thrown when
xis not a correctly formatted number in basebase.
- proc init=(x: integral)
See
init=
- proc sizeInBase(base: int) : int
Determine the size of
thismeasured in number of digits in the givenbase. The sign ofthisis ignored, only the absolute value is used.- Arguments:
base :
int– The base in which to compute the number of digits used to representthis. Can be between 2 and 62.- Returns:
The size of
thismeasured in number of digits in the givenbase. Will either be exact or 1 too big. Ifbaseis a power of 2, will always be exact. Ifthisis 0, will always return 1.- Return type:
int
See also
- proc getImpl() : __mpz_struct
Warning
getImpl is provided as a convenience but its result may change in the future
Return the underlying implementation of
bigint. Currently, the type returned is__mpz_struct.
- proc getD2Exp() : (real, uint(32))
Convert
thisto a tuple containing areal(truncated if necessary, by rounding towards zero) and the exponent. The returned tuple fulfills the conditiond * 2^exp == thiswheredis the first value in the tuple andexpis the second.- Returns:
a tuple representing the number in multiple parts,
(d, exp), such that their combinationd * 2^expis equal tothis.din this case will be in the range0.5 <= abs(d) < 1, unlessthisis0, in which cased == 0.0andexp == 0.- Return type:
(real, uint(32))
See also
- proc serialize(writer, ref serializer) throws
Writes this number to a
fileWriter
- proc ref deserialize(reader, ref deserializer) throws
Read this number from a
fileReader
- enum roundingMode { down = -1, zero = 0, up = 1 }
An enumeration of the different rounding strategies, for use with e.g.
divto determine how to round the quotient when performing the computation.- enum constant down = -1
Indicates that the quotient should be rounded down towards -infinity and any remainder should have the same sign as the denominator.
- enum constant zero = 0
Indicates that the quotient should be rounded towards zero and any remainder should have the same sign as the numerator.
- enum constant up = 1
Indicates that the quotient should be rounded up towards +infinity and any remainder should have the opposite sign as the denominator.
- operator :(x: integral, type t: bigint) : bigint
Constructs a new
bigintfromx, seebigint.init.
- operator :(x: string, type t: bigint) : bigint throws
Constructs a new
bigintfromx, see thebigint.initoverload which takes astring.
- operator :(x: bool, type t: bigint) : bigint throws
Constructs a new
bigintfromx, seebigint.init.
- operator :(x: real, type t: bigint) : bigint
Constructs a new
bigintfromx
- operator :(const ref x: bigint, type t: numeric) where isIntType(t)
Convert
xto a signed integer. Ifxis larger thant, the value returned is the least significant part ofxwith the same sign asx.See also
GMP.mpz_get_siand mpz_get_si.
- operator :(const ref x: bigint, type t: numeric) where isUintType(t)
Convert
xto an unsigned integer. Ifxis larger thant, the value returned is the least significant part ofxignoring the sign ofx.See also
GMP.mpz_get_uiand mpz_get_ui.
- operator :(const ref x: bigint, type t: numeric) where isRealType(t)
Convert
xto arealwith typet(truncated if necessary, by rounding towards zero).Warning
If the resulting exponent from the conversion is too big, the result is system dependent. If supported, an infinity may be returned. A hardware overflow trap may also occur.
See also
GMP.mpz_get_dand mpz_get_d.
- operator :(const ref x: bigint, type t: string)
Convert
xto a string representation.
- operator bigint.=(ref lhs: bigint, const ref rhs: bigint)
See
bigint.set
- operator bigint.=(ref lhs: bigint, rhs: int(?))
See
bigint.set
- operator bigint.=(ref lhs: bigint, rhs: uint(?))
See
bigint.set
- operator bigint.+(const ref a: bigint) : bigint
See
bigint.init
- operator bigint.+(const ref a: bigint, const ref b: bigint) : bigint
See
add
- operator bigint.+(const ref a: bigint, b: int(?)) : bigint
See
add
- operator bigint.+(const ref a: bigint, b: uint(?)) : bigint
See
add
- operator bigint.+(a: int(?), const ref b: bigint) : bigint
See
add
- operator bigint.+(a: uint(?), const ref b: bigint) : bigint
See
add
- operator bigint.-(const ref a: bigint, const ref b: bigint) : bigint
See
sub
- operator bigint.-(const ref a: bigint, b: int(?)) : bigint
See
sub
- operator bigint.-(const ref a: bigint, b: uint(?)) : bigint
See
sub
- operator bigint.-(a: int(?), const ref b: bigint) : bigint
See
sub
- operator bigint.-(a: uint(?), const ref b: bigint) : bigint
See
sub
- operator bigint.*(const ref a: bigint, b: int(?)) : bigint
See
mul
- operator bigint.*(const ref a: bigint, b: uint(?)) : bigint
See
mul
- operator bigint.*(a: int(?), const ref b: bigint) : bigint
See
mul
- operator bigint.*(a: uint(?), const ref b: bigint) : bigint
See
mul
- operator bigint./(const ref a: bigint, b: integral) : bigint
See
div
- operator bigint.**(const ref base: bigint, exp: int(?)) : bigint
See
pow
- operator bigint.**(const ref base: bigint, exp: uint(?)) : bigint
See
pow
- operator bigint.%(const ref a: bigint, const ref b: bigint) : bigint
Computes the mod operator on the two arguments, defined as
a % b = a - b * trunc(a / b).The result is always >= 0 if a > 0. It is an error if b == 0.
See
rem
- operator bigint.%(const ref a: bigint, b: int(?)) : bigint
See
bigint.%
- operator bigint.%(const ref a: bigint, b: uint(?)) : bigint
See
bigint.%
- operator bigint.<<(const ref a: bigint, b: uint(?)) : bigint
See
shiftLeft
- operator bigint.>>(const ref a: bigint, b: int(?)) : bigint
See
shiftRight
- operator bigint.>>(const ref a: bigint, b: uint(?)) : bigint
See
shiftRight
- operator bigint.==(const ref a: bigint, const ref b: bigint) : bool
See
bigint.cmp
- operator bigint.==(const ref a: bigint, b: int(?)) : bool
See
bigint.cmp
- operator bigint.==(const ref a: bigint, b: uint(?)) : bool
See
bigint.cmp
- operator bigint.==(a: int(?), const ref b: bigint) : bool
See
bigint.cmp
- operator bigint.==(a: uint(?), const ref b: bigint) : bool
See
bigint.cmp
- operator bigint.!=(const ref a: bigint, const ref b: bigint) : bool
See
bigint.cmp
- operator bigint.!=(const ref a: bigint, b: int(?)) : bool
See
bigint.cmp
- operator bigint.!=(const ref a: bigint, b: uint(?)) : bool
See
bigint.cmp
- operator bigint.!=(a: int(?), const ref b: bigint) : bool
See
bigint.cmp
- operator bigint.!=(a: uint(?), const ref b: bigint) : bool
See
bigint.cmp
- operator bigint.>(const ref a: bigint, const ref b: bigint) : bool
See
bigint.cmp
- operator bigint.>(const ref a: bigint, b: int(?)) : bool
See
bigint.cmp
- operator bigint.>(const ref a: bigint, b: uint(?)) : bool
See
bigint.cmp
- operator bigint.>(a: int(?), const ref b: bigint) : bool
See
bigint.cmp
- operator bigint.>(a: uint(?), const ref b: bigint) : bool
See
bigint.cmp
- operator bigint.<(const ref a: bigint, const ref b: bigint) : bool
See
bigint.cmp
- operator bigint.<(const ref a: bigint, b: int(?)) : bool
See
bigint.cmp
- operator bigint.<(const ref a: bigint, b: uint(?)) : bool
See
bigint.cmp
- operator bigint.<(a: int(?), const ref b: bigint) : bool
See
bigint.cmp
- operator bigint.<(a: uint(?), const ref b: bigint) : bool
See
bigint.cmp
- operator bigint.>=(const ref a: bigint, const ref b: bigint) : bool
See
bigint.cmp
- operator bigint.>=(const ref a: bigint, b: int(?)) : bool
See
bigint.cmp
- operator bigint.>=(const ref a: bigint, b: uint(?)) : bool
See
bigint.cmp
- operator bigint.>=(a: int(?), const ref b: bigint) : bool
See
bigint.cmp
- operator bigint.>=(a: uint(?), const ref b: bigint) : bool
See
bigint.cmp
- operator bigint.<=(const ref a: bigint, const ref b: bigint) : bool
See
bigint.cmp
- operator bigint.<=(const ref a: bigint, b: int(?)) : bool
See
bigint.cmp
- operator bigint.<=(const ref a: bigint, b: uint(?)) : bool
See
bigint.cmp
- operator bigint.<=(a: int(?), const ref b: bigint) : bool
See
bigint.cmp
- operator bigint.<=(a: uint(?), const ref b: bigint) : bool
See
bigint.cmp
- operator bigint.+=(ref a: bigint, b: int(?))
See
add
- operator bigint.+=(ref a: bigint, b: uint(?))
See
add
- operator bigint.-=(ref a: bigint, b: int(?))
See
sub
- operator bigint.-=(ref a: bigint, b: uint(?))
See
sub
- operator bigint.*=(ref a: bigint, b: int(?))
See
mul
- operator bigint.*=(ref a: bigint, b: uint(?))
See
mul
- operator bigint./=(ref a: bigint, b: integral)
See
div
- operator bigint.**=(ref base: bigint, exp: int(?))
See
pow
- operator bigint.**=(ref base: bigint, exp: uint(?))
See
pow
- operator bigint.%=(ref a: bigint, b: int(?))
See
bigint.%
- operator bigint.%=(ref a: bigint, b: uint(?))
See
bigint.%
- operator bigint.<<=(ref a: bigint, b: uint(?))
See
shiftLeft
- operator bigint.>>=(ref a: bigint, b: int(?))
See
shiftRight
- operator bigint.>>=(ref a: bigint, b: uint(?))
See
shiftRight
- operator bigint.<=>(ref a: bigint, ref b: bigint)
See
bigint.swap
- proc jacobi(const ref a: bigint, const ref b: bigint) : int
Warning
jacobi is unstable and may change in the future
Returns the Jacobi symbol
a/b, which is defined only whenbis odd.- Return type:
int
See also
GMP.mpz_jacobiand mpz_jacobi.
- proc legendre(const ref a: bigint, const ref p: bigint) : int
Warning
legendre is unstable and may change in the future
Returns the Legendre symbol
a/p, which is defined only whenpis an odd positive prime number.- Return type:
int
See also
- proc kronecker(const ref a: bigint, const ref b: bigint) : int
Warning
kronecker is unstable and may change in the future
Returns the Jacobi symbol
a/bwith the Kronecker extension. Whenbis odd this is the same as the Jacobi symbol.- Return type:
int
See also
- proc kronecker(const ref a: bigint, b: int(?)) : int
Warning
kronecker is unstable and may change in the future
See
kronecker
- proc kronecker(a: int(?), const ref b: bigint) : int
Warning
kronecker is unstable and may change in the future
See
kronecker
- proc kronecker(const ref a: bigint, b: uint(?)) : int
Warning
kronecker is unstable and may change in the future
See
kronecker
- proc kronecker(a: uint(?), const ref b: bigint) : int
Warning
kronecker is unstable and may change in the future
See
kronecker
- proc divExact(ref result: bigint, const ref numer: bigint, const ref denom: bigint)
Computes
numer/denomand stores the result inresult, which is abigintinstance.Warning
divExactis optimized to handle cases wherenumer/denomresults in an integer. Whennumer/denomdoes not produce an integer, this method may produce incorrect results.- Arguments:
See also
- proc divExact(ref result: bigint, const ref numer: bigint, denom: integral)
See
divExact
- proc bigint.isDivisible(const ref div: bigint) : bool
Return
trueifthisis exactly divisible bydiv.thisis divisible bydivif there exists an integerqsatisfyingthis = q*div. Unlike the other division functions,0is an acceptable value fordivand only0is considered divisible by0.- Arguments:
div :
bigint,intoruint– number to check ifthisis divisible by- Returns:
trueifthisis exactly divisible bydiv,falseotherwise- Return type:
bool
See also
- proc bigint.isDivisible(div: int(?)) : bool
See
isDivisible
- proc bigint.isDivisible(div: uint(?)) : bool
See
isDivisible
- proc bigint.isDivisibleBy2Pow(exp: integral) : bool
Return
trueifthisis exactly divisible by2^exp.thisis divisible by2^expif there exists an integerqsatisfyingthis = q*2^exp.- Arguments:
exp :
integral– power of 2 to check ifthisis divisible by- Returns:
trueifthisis exactly divisible by2^exp,falseotherwise- Return type:
bool
See also
- proc bigint.isCongruent(const ref con: bigint, const ref mod: bigint) : bool
Return
trueifthisis congruent tocon % mod.thisis congruent tocon % modif there exists an integerqsatisfyingthis = con + q*mod. Unlike the other division functions,0is an acceptable value formod. As a resultthisandconare considered congruent modulo0only when exactly equal.- Arguments:
- Returns:
trueifthisis congruent toconmodulomod,falseotherwise- Return type:
bool
See also
- proc bigint.isCongruent(con: integral, mod: integral) : bool
See
isCongruent
- proc bigint.isCongruentBy2Pow(const ref con: bigint, modExp: integral) : bool
Return
trueifthisis congruent tocon % 2^modExp.thisis congruent tocon % 2^modExpif there exists an integerqsatisfyingthis = con + q*2^modExp.- Arguments:
con :
bigintorintegral– number to determine ifthisis congruent to, modulo2^modExp.modExp :
integral– power of 2 to use as the divisor ofconwhen determining ifconis congruent tothis.
- Returns:
trueifthisis congruent toconmodulo2^modExp,falseotherwise.- Return type:
bool
See also
- proc powMod(ref result: bigint, const ref base: bigint, const ref exp: bigint, const ref mod: bigint)
Set
resultto the result of(base**exp) modulo mod.- Arguments:
result :
bigint– Where the result is storedbase :
bigint– The value to be raised to the power ofexpbefore performing a modulo operation on.exp :
bigint,int, oruint– The exponent to raisebaseto the power of prior to the modulo operation. Can be negative if the inverse (1/base) modulomodexists.mod :
bigint– The divisor for the modulo operation.
Warning
The program behavior is undefined if
expis negative and the inverse(1/base) modulo moddoes not exist.See also
GMP.mpz_powmand mpz_powm.
- proc powMod(ref result: bigint, const ref base: bigint, exp: int(?), const ref mod: bigint)
See
powMod
- proc powMod(ref result: bigint, const ref base: bigint, exp: uint(?), const ref mod: bigint)
See
powMod
- proc pow(ref result: bigint, const ref base: bigint, exp: int(?))
Set
resultto the result ofbaseraised toexp.- Arguments:
See also
GMP.mpz_pow_uiand mpz_pow_ui.
- proc pow(ref result: bigint, const ref base: bigint, exp: uint(?))
See
pow
- proc pow(ref result: bigint, base: int(?), exp: int(?))
See
pow
- proc pow(ref result: bigint, base: uint(?), exp: uint(?))
See
pow
- proc root(ref result: bigint, const ref x: bigint, n: uint) : int
Sets
resultto the truncated integernth root ofx.- Arguments:
See also
GMP.mpz_rootand mpz_root.
- proc rootRem(ref result: bigint, ref remain: bigint, const ref x: bigint, n: uint)
Sets
resultto the truncated integernth root ofx. Stores the remainder inremain.- Arguments:
See also
- proc sqrt(ref result: bigint, const ref x: bigint)
Sets
resultto the truncated integer square root ofx.- Arguments:
See also
GMP.mpz_sqrtand mpz_sqrt.
- proc sqrtRem(ref result: bigint, ref remain: bigint, const ref x: bigint)
Sets
resultto the truncated integer square root ofx. Stores the remainder inremain.Warning
If
resultis also passed as theremainargument, the program behavior is undefined.- Arguments:
See also
- proc bigint.isPerfectPower() : bool
Return
trueifthisis a perfect power, i.e., if there exist integersaandbwithb > 1, such thatthis = a^b.Under this definition both 0 and 1 are considered to be perfect powers. Negative values can only be odd perfect powers.
- Returns:
trueifthisis a perfect power,falseotherwise.- Return type:
bool
See also
- proc bigint.isPerfectSquare() : bool
Return
trueifthisis a perfect square, i.e., if the square root ofthisis an integer.Under this definition both
0and1are considered to be perfect squares.- Returns:
trueifthisis a perfect square,falseotherwise.- Return type:
bool
See also
- enum primality { notPrime = 0, maybePrime, isPrime }
An enumeration of the different possibilities of a number being prime, for use with e.g.
probablyPrimeto determine if a number is prime or not.- enum constant notPrime = 0
Indicates that the number is not a prime.
- enum constant maybePrime
Indicates that the number may or may not be a prime.
- enum constant isPrime
Indicates that the number is a prime.
- proc bigint.probablyPrime(reps: int) : primality
Warning
bigint.probablyPrime is unstable and may change in the future
Determine whether
thisis prime. Returns one of theprimalityconstants -isPrime,maybePrime, ornotPrime.Performs some trial divisions, a Baillie-PSW probable prime test, and reps-24 Miller-Rabin probabilistic primality tests. A higher
repsvalue will reduce the chances of a non-prime being identified as “probably prime”. A composite number will be identified as a prime with an asymptotic probability of less than4^(-reps). Reasonable values ofrepsare between 15 and 50.- Arguments:
reps :
int– number of attempts before returningmaybePrimeif a definitive answer can’t be found before then.- Returns:
isPrime,maybePrime, ornotPrime.- Return type:
See also
- proc nextPrime(ref result: bigint, const ref x: bigint)
Warning
nextPrime is unstable and may change in the future
Set
resultto the next prime number greater thanx.Note
This is a probabilistic function and in an unlikely case may set
resultto a composite number.- Arguments:
See also
- proc gcd(ref result: bigint, const ref a: bigint, const ref b: bigint)
Warning
gcd is unstable and may change in the future
Set
resultto the greatest common divisor ofaandb- Arguments:
See also
GMP.mpz_gcdand mpz_gcd.
- proc gcd(ref result: bigint, const ref a: bigint, b: int(?))
Warning
gcd is unstable and may change in the future
See
gcd
- proc gcd(ref result: bigint, const ref a: bigint, b: uint(?))
Warning
gcd is unstable and may change in the future
See
gcd
- proc gcd(ref result: bigint, const ref a: bigint, const ref b: bigint, ref s: bigint, ref t: bigint) : void
Warning
gcd is unstable and may change in the future
Set
resultto the greatest common divisor ofaandb, and setsandtto coefficients such thata*s + b*t == result.Note
The result stored in
resultis always positive, even if one or both ofaandbare negative (or zero if both are zero).- Arguments:
result :
bigint– Where the result is storeda :
bigint– One of the numbers to compute the greatest common divisor ofb :
bigint– One of the numbers to compute the greatest common divisor ofs :
bigint– The returned coefficient that can be multiplied bya.t :
bigint– The returned coefficient that can be multiplied byb.
See also
GMP.mpz_gcdextand mpz_gcdext.
- proc lcm(ref result: bigint, const ref a: bigint, const ref b: bigint)
Warning
lcm is unstable and may change in the future
Set
resultto the least common multiple ofaandb- Arguments:
See also
GMP.mpz_lcmand mpz_lcm.
- proc lcm(ref result: bigint, const ref a: bigint, b: int(?))
Warning
lcm is unstable and may change in the future
See
lcm
- proc lcm(ref result: bigint, const ref a: bigint, b: uint(?))
Warning
lcm is unstable and may change in the future
See
lcm
- class InversionError : Error
An
InversionErroris thrown if ainvert()is attempted with invalid arguments that result in a non-existent inverse. Specifically, if the arguments cause a divide by zero, this error notifies the caller that the internal value of thebigintwas left in an undefined state.- proc init()
Create an
InversionErrorwith the default error message: Inverse does not exist
- proc invert(ref result: bigint, const ref x: bigint, const ref y: bigint) throws
Set the value of
resultto the inverse ofxmoduloy- Arguments:
- Throws:
InversionError – Thrown when the inverse does not exist and the value of
resultwill be left undefined.
See also
GMP.mpz_invertand mpz_invert.
- proc removeFactor(ref result: bigint, const ref x: bigint, const ref fac: bigint) : uint
Remove all occurrences of the factor
facfromxand store the result inresult. Return the number of occurrences removed.- Arguments:
- Returns:
The number of occurrences of
facfound inx.- Return type:
uint
See also
- proc fac(ref result: bigint, a: integral)
Warning
fac is unstable and may change in the future
Set
resultto the factorial ofa.- Arguments:
result :
bigint– Where the result is storeda :
integral– Number to take the factorial of
See also
GMP.mpz_fac_uiand mpz_fac_ui.
- proc bin(ref result: bigint, const ref n: bigint, k: integral)
Warning
bin is unstable and may change in the future
Set
resultto the binomial coefficient ofnoverk.- Arguments:
See also
GMP.mpz_bin_uiand mpz_bin_ui.
- proc bin(ref result: bigint, n: uint, k: integral)
Warning
bin is unstable and may change in the future
See
bin
- proc fib(ref result: bigint, n: integral)
Warning
fib is unstable and may change in the future
Set
resultto thenth Fibonacci number.- Arguments:
result :
bigint– return value that will contain the Fibonacci numbern :
integral– which Fibonacci number to compute forresult.
See also
GMP.mpz_fib_uiand mpz_fib_ui.
- proc fib2(ref result: bigint, ref fnsub1: bigint, n: integral)
Warning
fib2 is unstable and may change in the future
Set
resultto thenth Fibonacci number and setfnsub1to then-1th Fibonacci number.- Arguments:
See also
- proc lucNum(ref result: bigint, n: integral)
Warning
lucNum is unstable and may change in the future
Set
resultto thenth Lucas number.- Arguments:
result :
bigint– return value that will contain the Lucas numbern :
integral– which Lucas number to compute
See also
- proc lucNum2(ref result: bigint, ref fnsub1: bigint, n: integral)
Warning
lucNum2 is unstable and may change in the future
Set
resultto thenth Lucas number and setfnsub1to then-1th Lucas number.- Arguments:
See also
- proc bigint.popCount() : uint
Returns the number of
1bits inthis. Ifthisis negative, the number of1bits is infinite and the return value is the largest possiblemp_bitcnt_t.- Returns:
The number of
1bits inthis- Return type:
uint
See also
- proc bigint.hammingDistance(const ref x: bigint) : uint
Warning
bigint.hammingDistance is unstable and may change in the future
Returns the number of bit positions that differ between
thisandx. Ifthisandxhave different signs, the number of bits that differ is infinite and the return value is the largest possiblemp_bitcnt_t.- Arguments:
x :
bigint– value to comparethisagainst- Returns:
The number of bits that differ
- Return type:
uint
See also
- proc bigint.findNext0(startBitIdx: integral) : uint
Returns the index of the first
0bit found, starting fromstartBitIdxand searching towards the more significant bits.If the bit at
startBitIdxis1, will returnstartBitIdx.- Arguments:
startBitIdx :
integral– The index of the first bit to start searching for a0- Returns:
The index of the first
0bit afterstartBitIdx, inclusive- Return type:
uint
See also
GMP.mpz_scan0and mpz_scan0.
- proc bigint.findNext1(startBitIdx: integral) : uint
Returns the index of the first
1bit found, starting fromstartBitIdxand searching towards the more significant bits.If the bit at
startBitIdxis1, will returnstartBitIdx.- Arguments:
startBitIdx :
integral– The index of the first bit to start searching for a1- Returns:
The index of the first
1bit afterstartBitIdx, inclusive- Return type:
uint
See also
GMP.mpz_scan1and mpz_scan1.
- proc ref bigint.setBit(idx: integral)
Set the bit at
idxofthis.- Arguments:
idx :
integral– The index of the bit to be set
See also
GMP.mpz_setbitand mpz_setbit.
- proc ref bigint.clearBit(idx: integral)
Clear the bit at
idxofthis.- Arguments:
idx :
integral– The index of the bit to be cleared
See also
GMP.mpz_clrbitand mpz_clrbit.
- proc ref bigint.toggleBit(idx: integral)
Toggle the bit at
idxofthis. If the bit was 1, set it to 0. If the bit was 0, set it to 1.- Arguments:
idx :
integral– The index of the bit to be toggled
See also
GMP.mpz_combitand mpz_combit.
- proc bigint.getBit(idx: integral) : int
Get the bit at
idxofthis.- Arguments:
idx :
integral– The index of the bit to be returned- Returns:
The bit at index
idx- Return type:
int
See also
GMP.mpz_tstbitand mpz_tstbit.
- proc bigint.fitsInto(type t: integral) : bool
Test whether a
bigintwill fit into one of the standard integer types.- Arguments:
t :
integral– The Integral type to check against.- Return type:
bool
See also
- proc bigint.isEven() : bool
Returns
trueifthisis an even number,falseotherwise.- Return type:
bool
See also
GMP.mpz_even_pand mpz_even_p.
- proc bigint.isOdd() : bool
Returns
trueifthisis an odd number,falseotherwise.- Return type:
bool
See also
GMP.mpz_odd_pand mpz_odd_p.
- proc add(ref result: bigint, const ref x: bigint, const ref y: bigint)
Sets
resultto the sum ofxandy.- Arguments:
See also
GMP.mpz_add,GMP.mpz_add_ui, and mpz_add.
- proc add(ref result: bigint, const ref x: bigint, y: int(?))
See
add
- proc add(ref result: bigint, const ref x: bigint, y: uint(?))
See
add
- proc sub(ref result: bigint, const ref x: bigint, const ref y: bigint)
Sets
resultto the difference ofxandy.- Arguments:
See also
GMP.mpz_sub,GMP.mpz_sub_ui, and mpz_sub.
- proc sub(ref result: bigint, const ref x: bigint, y: int(?))
See
sub
- proc sub(ref result: bigint, const ref x: bigint, y: uint(?))
See
sub
- proc sub(ref result: bigint, x: int(?), const ref y: bigint)
See
sub
- proc sub(ref result: bigint, x: uint(?), const ref y: bigint)
See
sub
- proc mul(ref result: bigint, const ref x: bigint, const ref y: bigint)
Sets
resultto the product ofxandy.- Arguments:
See also
- proc mul(ref result: bigint, const ref x: bigint, y: int(?))
See
mul
- proc mul(ref result: bigint, const ref x: bigint, y: uint(?))
See
mul
- proc addMul(ref result: bigint, const ref x: bigint, const ref y: bigint)
Adds the product of
xandytoresult(result = result + (x * y)).- Arguments:
See also
- proc addMul(ref result: bigint, const ref x: bigint, y: int(?))
See
addMul
- proc addMul(ref result: bigint, const ref x: bigint, y: uint(?))
See
addMul
- proc subMul(ref result: bigint, const ref x: bigint, const ref y: bigint)
Subtracts the product of
xandyfromresult(result = result - (x * y)).- Arguments:
See also
- proc subMul(ref result: bigint, const ref x: bigint, y: int(?))
See
addMul
- proc subMul(ref result: bigint, const ref x: bigint, y: uint(?))
See
addMul
- proc mul2Exp(ref result: bigint, const ref x: bigint, exp: integral)
Warning
mul2Exp is unstable and may change in the future
Computes
x*(2**exp)and stores the result inresult.This is the same as performing a left bit shift of
xbyexpbits.- Arguments:
See also
- proc neg(ref result: bigint, const ref x: bigint)
Sets
resultto the negation ofx.See also
GMP.mpz_negamd mpz_neg.
- proc abs(ref result: bigint, const ref x: bigint)
Sets
resultto the absolute value ofx.- Arguments:
See also
GMP.mpz_absamd mpz_abs.
- proc div(ref result: bigint, const ref numer: bigint, const ref denom: bigint, param rounding = roundingMode.zero)
Divide
numerbydenom, forming a quotient and storing it inresult.- Arguments:
result :
bigint– Where the result is storednumer :
bigint– The numerator of the division operation to be performeddenom :
bigint,integral– The denominator of the division operation to be performedrounding :
roundingMode– The rounding style to use, seeroundingModefor a description of what the rounding styles entail. Defaults tozeroif unspecified
Warning
If the denominator is zero, the program behavior is undefined.
See also
GMP.mpz_cdiv_q,GMP.mpz_fdiv_q,GMP.mpz_tdiv_q, and mpz_*div_q.
- proc div(ref result: bigint, const ref numer: bigint, denom: integral, param rounding = roundingMode.zero)
See
div
- proc rem(ref result: bigint, const ref numer: bigint, const ref denom: bigint, param rounding = roundingMode.zero)
Divide
numerbydenom, forming a remainder and storing it inresult. The absolute value of the remainder will always be less than the absolute value of the denominator (i.e.abs(result) < abs(denom)).- Arguments:
result :
bigint– Where the result is storednumer :
bigint– The numerator of the division operation to be performeddenom :
bigint,integral– The denominator of the division operation to be performedrounding :
roundingMode– The rounding style to use, seeroundingModefor a description of what the rounding styles entail. Defaults tozeroif unspecified
Warning
If the denominator is zero, the program behavior is undefined.
See also
GMP.mpz_cdiv_r,GMP.mpz_fdiv_r,GMP.mpz_tdiv_r, and mpz_*div_r.
- proc rem(ref result: bigint, const ref numer: bigint, denom: integral, param rounding = roundingMode.zero)
See
rem
- proc divRem(ref result: bigint, ref remain: bigint, const ref numer: bigint, const ref denom: bigint, param rounding = roundingMode.zero)
Divide
numerbydenom, forming a quotient and storing it inresult, and a remainder and storing it inremain. The quotient and remainder will always satisfynumer = result*denom + remainafter the operation has finished. The absolute value of the remainder will always be less than the absolute value of the denominator (i.e.abs(result) < abs(denom)).Warning
If
resultis also passed as theremainargument, the program behavior is undefined.- Arguments:
result :
bigint– Where the result is storedremain :
bigint– Stores the remainder of the divisionnumer :
bigint– The numerator of the division operation to be performeddenom :
bigint,integral– The denominator of the division operation to be performedrounding :
roundingMode– The rounding style to use, seeroundingModefor a description of what the rounding styles entail. Defaults tozeroif unspecified
Warning
If the denominator is zero, the program behavior is undefined.
See also
GMP.mpz_cdiv_qr,GMP.mpz_fdiv_qr,GMP.mpz_tdiv_qr, and mpz_*div_qr.
- proc divRem(ref result: bigint, ref remain: bigint, const ref numer: bigint, denom: integral, param rounding = roundingMode.zero)
See
divRem
- proc div2Exp(ref result: bigint, const ref numer: bigint, exp: integral, param rounding = roundingMode.zero)
Warning
div2Exp is unstable and may change in the future
Divide
numerby2^exp, forming a quotient and storing it inresult.This is the same as performing a right bit shift of
numerbyexpbits whenroundingisdown.- Arguments:
result :
bigint– Where the result is storednumer :
bigint– The numerator of the division operation to be performedexp :
integral– The exponent that 2 should be raised to before being used as the denominator of the division operation to be performedrounding :
roundingMode– The rounding style to use, seeroundingModefor a description of what the rounding styles entail. Defaults tozeroif unspecified
See also
GMP.mpz_cdiv_q_2exp,GMP.mpz_fdiv_q_2exp,GMP.mpz_tdiv_q_2exp, and mpz_*div_q_2exp.
- proc rem2Exp(ref result: bigint, const ref numer: bigint, exp: integral, param rounding = roundingMode.zero)
Warning
rem2Exp is unstable and may change in the future
Divide
numerby2^exp, forming a remainder and storing it inresult.- Arguments:
result :
bigint– Where the result is storednumer :
bigint– The numerator of the division operation to be performedexp :
integral– The exponent that 2 should be raised to before being used as the denominator of the division operation to be performedrounding :
roundingMode– The rounding style to use, seeroundingModefor a description of what the rounding styles entail. Defaults tozeroif unspecified
See also
GMP.mpz_cdiv_r_2exp,GMP.mpz_fdiv_r_2exp,GMP.mpz_tdiv_r_2exp, and mpz_*div_r_2exp.
- proc shiftLeft(ref result: bigint, const ref x: bigint, n: integral)
Stores
xshifted left bynbits inresult. Negativenwill result in a right shift.
- proc shiftRight(ref result: bigint, const ref x: bigint, n: integral)
Stores
xshifted right bynbits inresult. Negativenwill result in a left shift.
- proc mod(ref result: bigint, const ref x: bigint, const ref y: bigint)
Computes the mod operator on the two arguments, defined as
mod(x, y) = x - y * floor(x / y).The result is always >= 0 if y > 0. It is an error if y == 0.
- Arguments:
Note
If
yis auint, then fewer conditionals will be evaluated at run time.
- proc mod(ref result: bigint, const ref x: bigint, y: integral) : int
See
mod
- proc bigint.cmp(const ref x: bigint) : int
Compares
thisandx.- Arguments:
x :
bigint,uint,int,real– The number to compare against- Returns:
Returns a positive value if
thisis greater thanx, a negative value ifthisis less thanx, or zero if they are equal.- Return type:
int
See also
GMP.mpz_cmpand mpz_cmp.
- proc bigint.cmp(x: int(?)) : int
See
cmp
- proc bigint.cmp(x: uint(?)) : int
See
cmp
- proc bigint.cmp(x: real) : int
See
cmp
- proc bigint.cmpabs(const ref x: bigint) : int
Compares the absolute value of
thisand the absolute value ofx.- Arguments:
x :
bigint,uint,real– The number to compare against- Returns:
Returns a positive value if
abs(this)is greater thanabs(x), a negative value ifabs(this)is less thanabs(x), or zero if they are equal.- Return type:
int
See also
GMP.mpz_cmpabsand mpz_cmpabs.
- proc bigint.cmpabs(x: uint) : int
See
cmpabs
- proc bigint.cmpabs(x: real) : int
See
cmpabs
- proc bigint.sgn() : int
Warning
bigint.sgn is unstable and may change its name and return type in the future
Returns the sign of
this.- Returns:
Returns 1 if positive, -1 if negative, and 0 if zero.
- Return type:
int
See also
GMP.mpz_sgnand mpz_sgn.
- proc and(ref result: bigint, const ref x: bigint, const ref y: bigint)
Compute the bitwise and of
xandyand store it inresult.- Arguments:
See also
GMP.mpz_andand mpz_and.
- proc or(ref result: bigint, const ref x: bigint, const ref y: bigint)
Compute the bitwise inclusive or of
xandyand store it inresult.- Arguments:
See also
GMP.mpz_iorand mpz_ior.
- proc xor(ref result: bigint, const ref x: bigint, const ref y: bigint)
Compute the bitwise exclusive or of
xandyand store it inresult.- Arguments:
See also
GMP.mpz_xorand mpz_xor.
- proc com(ref result: bigint, const ref x: bigint)
Compute the bitwise one’s complement of
xand store it inresult.See also
GMP.mpz_comand mpz_com.
- proc ref bigint.set(const ref x: bigint)
Assign
xtothis- Arguments:
x :
bigint– Number to be assigned
See also
GMP.mpz_setand mpz_set.
- proc ref bigint.set(x: int(?))
See
bigint.set
- proc ref bigint.set(x: uint(?))
See
bigint.set
- proc ref bigint.set(x: real)
See
bigint.set
- proc ref bigint.set(x: string, base: int = 0)
See
bigint.set
- proc ref bigint.swap(ref x: bigint)
Swaps
thisandx- Arguments:
x :
bigint– Number to be swapped
See also
GMP.mpz_swapand mpz_swap.