BigRatOps

© 2018,2020,2023 John Abbott and Anna M. Bigatti
GNU Free Documentation License, Version 1.2



CoCoALib Documentation Index

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

  1. normal arithmetic (potentially inefficient because of temporaries)
  2. arithmetic and assignment
  3. arithmetic ordering
  4. increment/decrement

More functions

  1. query functions (all take 1 argument)
  2. Exponentiation
  3. The cmp function (three way comparison)
  4. Other functions

Conversion functions

Only for BigInt

Summation

To sum many rationals use a SumBigRat object. This class is not thread-safe.

Let n be an integer, q be a BigRat, and SBR be a SumBigRat object.

Currently there is no operator-=; should there be?

Maintainer Documentation

Most impls are very simple (since GMP does all the real work).

Bugs, shortcomings and other ideas

Impl of FloorLogBase is ugly! The SizeInBase(N, b) has been removed: use 1+FloorLogBase(N) instead. There are some NYI functions!

Main changes

2023