The class BigInt
is intended to represent (signed) integers of
practically unlimited range; it is currently based on the
implementation in the GMP big integer library.
The class MachineInt
is intended to help you write functions which
accept arguments whose type is a machine integer offering a safe interface
for signed and unsigned maching integers.
When the CoCoALib documentation says integer it means both big integers and machine integers which are mainly interchangable. Do remember, though, that operations between two machine integers are handled directly by C++, and problems of overflow can occur.
The few exceptions accepting only long
are clearly indicated and
usually apply to indices and subscripts.
IsEven(n)
-- true iff n
is even
IsOdd(n)
-- true iff n
is odd
IsDivisible(n,d)
-- true iff n
is divisibile by d
IsSquare(n)
-- true iff n
is a perfect square
IsPower(n)
-- true iff n
is a perfect k-th power for some k > 1
IsExactIroot(X,N,r)
-- true iff N
is a perfect r
-th power, assigns iroot(N,r)
to X
Only for BigInt
IsZero(n)
-- true iff n
is zero
IsOne(n)
-- true iff n
is 1
IsMinusOne(n)
-- true iff n
is -1
+
the sum
-
the difference
*
the product
/
floor quotient (divisor must be positive)
%
least non-negative remainder (divisor must be positive)
=
assignment
==
, !=
<
, <=
, >
, >=
-- comparison (using the normal arithmetic ordering)
-- see also the cmp
function below.
++
, --
(prefix, e.g. ++a
) use these if you can
++
, --
(postfix, e.g. a++
) avoid these if you can, as they create temporaries
(three way comparison)
cmp(a, b)
-- returns an int
which is
< 0
, == 0
, or > 0
if
a < b
, a == b
, or a > b
respectively
(Several basic number theoretical operations are defined in NumTheory
)
Let n
be an integer,
let hi
, lo
be machine integers
abs(n)
-- the absolute value of n
sign(n)
-- result is int
:
-1 if n<0
, 0 if n==0
, and +1 if n>0
log(n)
-- natural logarithm of the absolute value of n
(as a double
)
RoundDiv(N,D)
-- rounded division of N
by D
, halves round towards +infinity
isqrt(N)
-- the (truncated) integer part of the square root of N
ILogBase(N,b)
-- the integer part of log(abs(N))/log(b);
error if N=0 or b<2 (as a long
)
These functions return BigInt
power(a, b)
-- returns a
to the power b
(b
must be >=0)
factorial(n)
-- factorial for non-negative n
RangeFactorial(lo,hi)
-- lo*(lo+1)*(lo+2)*...*hi
binomial(n, r)
-- binomial coefficient
fibonacci(n)
-- n
-th Fibonacci number
iroot(N,r)
-- the (truncated) integer part of the r
-th root of N
(see also IsExactIroot
)
RandomBigInt(GlobalRandomSource(),lo, hi)
-- a uniform random
integer N
s.t. lo <= N <= hi
; error if lo > hi
(was
random(lo,hi)
up to v0.9946)
-- see also RandomLongStream
and RandomZZStream
.
Only for BigInt
mantissa(n)
-- n represented as a floating-point number.
if n
is zero, produces 0.0
otherwise if n>0
a value between 0.5 and 0.999...
otherwise (when n<0
) a value between -0.5 and -0.999...
The bits of the floating point result are the topmost
bits of the binary representation of n
.
exponent(n)
-- result is a long
whose value is the least integer e such that
2^e > abs(n). If n
is zero, result is zero.
Only for BigInt
NumDigits(n, b)
-- the number of digits (as a long
) n
has
when written in base b
(the result may sometimes to be too large by 1)
Assignment is always to leftmost argument(s) a
, a BigInt
.
Second and/or third argument of type BigInt
.
add(a, b, c)
-- a = b+c
sub(a, b, c)
-- a = b-c
mul(a, b, c)
-- a = b*c
div(a, b, c)
-- a = b/c (floor division, divisor must be positive)
mod(a, b, c)
-- a = b%c (least non-negative remainder, divisor must be positive)
quorem(a, b, c, d)
-- a = c/d, b = c%d (divisor must be positive)
divexact(a, b, c)
-- a = b/c (fast, but division must be exact)
power(a, b, c)
-- a = b^c
neg(a, b)
-- a = -b
abs(a, b)
-- a = abs(b)
Error conditions are signalled by exceptions. Examples of error conditions
are impossible arithmetic operations such as division by zero, overly large
arguments (e.g. second argument to binomial must fit into a machine
long
), and exhaustion of resources.
Currently the exception structure is very simplistic:
CoCoA::ErrorInfo
exception; for instance
ERR::ArgTooBig |
value supplied is too large for the answer to be computed |
ERR::BadArg |
unsuitable arg(s) supplied (or input number too large) |
ERR::BadNumBase |
the base must be between 2 and 36 |
ERR::DivByZero |
division by zero |
ERR::ExpTooBig |
exponent is too large |
ERR::IntDivByNeg |
inexact integer division by a negative divisor |
ERR::NegExp |
negative exponent |
ERR::ZeroModulus |
the modulus specified is zero |
The power functions should handle power(0,0)
correctly; maybe they
should also allow high powers of -1,0,1 (without complaining about
the exponent being too big).
Only partial access to all the various division functions offered by the C interface to GMP. Many other GMP functions are not directly accessible.
IsExactIroot has rather a lot of signatures.
2012
BigInt
and MachineInt
together into IntOperations
-