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 thecmpfunction 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 iffqis zeroIsOne(q)-- true iffqis 1IsMinusOne(q)-- true iffqis -1IsOneNum(q)-- true iffnum(q)is 1IsOneDen(q)-- true iffden(q)is 1IsPowerOf2(q)-- true iffqis a power of 2sign(q)-- gives -1 (machine integer) to meanqis negative, 0 (machine integer) to meanqis zero, +1 (machine integer) to meanqis positive.
- Exponentiation
power(a, b)-- returnsato the powerb(result is always aBigRat)
- The cmp function (three way comparison)
cmp(a,b)-- returns anintwhich is< 0ifa < b, or== 0ifa == b, or> 0ifa > b.CmpAbs(a,b)-- equivalent tocmp(abs(a),abs(b))
- Other functions
abs(q)-- gives the absolute value ofqfloor(q)-- returns aBigIntfor the greatest integer<= qceil(q)-- returns aBigIntfor the least integer>= qround(q)-- returns aBigIntwhich is the nearest toq(halves round the same way as inRoundDiv, seeBigIntOps)num(q)-- returns aBigIntwhich is the numerator ofqden(q)-- returns a positiveBigIntwhich is the denominator ofqCommonDenom(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 integerksuch thatpower(base,k) <= abs(q); error ifbase < 2mantissa(q)-- returns adoublebetween 0.5 and 1 (excluded)exponent(q)--
Conversion functions
Only for BigInt
mantissa(N)--Nrepresented as a floating-point number. IfNis 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 alongwhose value is the least integer e such that 2^e > abs(n). IfNis zero, result is zero.
Miscellany
Only for BigInt
SizeInBase(N, b)-- (returnslong) the number of digitsNhas 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