Examples
User documentation
Here is a collection of basic operations available for rational values;
see also the more advanced functions in NumTheory
.
The usual arithmetic operations are available with standard C++
syntax. The type BigRat
is provided for convenience of
representing rational values rather than for rapid computation; the
native GMP operations may be noticeably faster.
There is an important exception to the natural syntax: ^
does not
denote exponentiation; you must use the function power
instead.
We have chosen not to define operator^
to perform exponentiation
because it is too easy to write misleading code: for instance,
a*b^2
is interpreted by the compiler as (a*b)^2
. There is no
way to make the C++ compiler use the expected interpretation.
Arithmetic may also be performed between a BigRat
and a machine
integer or a BigInt
. The result is always of type BigRat
(even if the value turns out to be an integer). Do remember, though,
that operations between two machine integers are handled directly by
C++, and problems of overflow can occur.
Infix operators
NOTE: similar to operations on BigInt
-- see BigIntOps
- normal arithmetic (potentially inefficient because of temporaries)
+
the sum-
the difference*
the product/
quotient=
assignment
- arithmetic and assignment
+=
,-=
,*=
,/=
-- definitions as expected; LHS must be of typeBigRat
- arithmetic ordering
==
,!=
<
,<=
,>
,>=
-- comparison (using the normal arithmetic ordering) -- see also thecmp
function below.
- increment/decrement
++
,--
(prefix, e.g.++a
) use these if you can++
,--
(postfix, e.g.a++
) avoid these if you can, as they create temporaries
More functions
- query functions (all take 1 argument)
IsZero(q)
-- true iffq
is zeroIsOne(q)
-- true iffq
is 1IsMinusOne(q)
-- true iffq
is -1IsOneNum(q)
-- true iffnum(q)
is 1IsOneDen(q)
-- true iffden(q)
is 1IsPowerOf2(q)
-- true iffq
is a power of 2sign(q)
-- gives -1 (machine integer) to meanq
is negative, 0 (machine integer) to meanq
is zero, +1 (machine integer) to meanq
is positive.
- Exponentiation
power(a, b)
-- returnsa
to the powerb
(result is always aBigRat
)
- The cmp function (three way comparison)
cmp(a,b)
-- returns anint
which is< 0
ifa < b
, or== 0
ifa == b
, or> 0
ifa > b
.CmpAbs(a,b)
-- equivalent tocmp(abs(a),abs(b))
- Other functions
abs(q)
-- gives the absolute value ofq
floor(q)
-- returns aBigInt
for the greatest integer<= q
ceil(q)
-- returns aBigInt
for the least integer>= q
round(q)
-- returns aBigInt
which is the nearest toq
(halves round the same way as inRoundDiv
, seeBigIntOps
)num(q)
-- returns aBigInt
which is the numerator ofq
den(q)
-- returns a positiveBigInt
which is the denominator ofq
CommonDenom(v)
-- returns least (positive) common denominator for a vector of BigRatlog(q)
-- returns a double whose value is (approx) the natural logarithm ofq
; error if `` q <= 0``.LogAbs(q)
-- equiv tolog(abs(q))
FloorLog2(q) -- same as ``FloorLogBase(q,2)
FloorLog10(q) -- same as ``FloorLogBase(q,10)
FloorLogBase(q,base)
-- returns largest integerk
such thatpower(base,k) <= abs(q)
; error ifbase < 2
mantissa(q)
-- returns adouble
between 0.5 and 1 (excluded)exponent(q)
--
Conversion functions
Only for BigInt
mantissa(N)
--N
represented as a floating-point number. IfN
is zero, produces 0.0. IfN>0
, produces a value between 0.5 and 0.999...; otherwise (whenN<0
) a value between -0.5 and -0.999... The bits of the floating point result are the topmost bits of the binary representation ofN
.exponent(N)
-- result is along
whose value is the least integer e such that 2^e > abs(n). IfN
is zero, result is zero.
Miscellany
Only for BigInt
SizeInBase(N, b)
-- (returnslong
) the number of digitsN
has when written in baseb
. Very fast! WARNING the result may sometimes to be too large by 1; use1+FloorLogBase(N)
to get the exact result.
Maintainer Documentation
Most impls are very simple (since GMP does all the real work).
Bugs, shortcomings and other ideas
Impl of FloorLogBase
is ugly!
There are some NYI functions!
Main changes
2018
- June
- split off from
BigRat
- split off from