A C++ rational (fraction) template class
Include rational.h to be able to do fraction calculations. By simply including rational.h and
specifying the storage type (any integer variant) you can create and use a fractional data type.
For example, Rational<long> foo(3, 4) would create a fraction named foo with a value of 3/4,
and store the fraction using the long data type.
Simple to use, fast, and modifiable for your needs, because its all in the one header file
rational.h at your access. It includes all basic mathematical operations as well as comparison
operators, and is very flexible. If you would like to see a feature implemented, just ask here.
The storage type should represent all integers within some (possibly infinite) interval
including 0 and 1. For example, the native signed or unsigned int and long types, or
arbitrary-precision integers, may be used. Beyond ordinary integers, you should also be
able to use any other Euclidean domain,
perhaps not even an ordered ring, but support
for such types is experimental and has not been thoroughly tested. In fact, you should be able
to use any integral domain, but you may need
to apply a more sophisticated GCD algorithm; you can fall back to GCD_null if overflow is
not a concern in practice. Finally, using non-integral domains is very likely to fail.
-
exchangeable
GCDalgorithms [GCD_euclid_fast(default),GCD_euclidandGCD_stein, choose as second template parameter, i. e.Rational<long, GCD_stein> foo(3, 4)forStein] -
optional signed overflow/unsigned wrap checking by throwing an
std::domain_errorexception (i.e.Rational<storage_type, GCD_algo, Commons::Math::ENABLE_OVERFLOW_CHECK>, default is no checking:Commons::Math::NO_OVERFLOW_CHECK) -
optimized for
signedandunsignedtypes -
additional operators:
modto split inproper fractions in integer and fraction partabsto get the absolute value%(modulo) for rational modulo- increment (
++x&x++) and decrement (--x&x--) - unary
plusandminus
-
Construction of inproper (mixed) fractions, i.e.
Rational<long> foo(2, 3, 4)for2 3/4resp.2.75 -
Construction of approximate fractions, i.e.
Rational<long> foo(3.14159265358979323846)forπresp.245850922/78256779(approximation is dependent on compiler and chosen storage type) -
Support for
- the GNU Multiple Precision Arithmetic Library
(include
gmp_rational.h) - CLN - Class Library for Numbers (include
cln_rational.h)
as underlying storage type
- the GNU Multiple Precision Arithmetic Library
(include
-
Expression templates for domain specific programming (include
expr_rational.h) -
Construction of fractions from expression strings (i.e.
Rational<long> expr("(11/2) * +(4.25+3.75)")) -
Construction of fractions from continued fractions (from container of integer types)
-
Extraction of continued fractions sequences from a fraction
-
Construction of fractions from repeating decimals (i.e.
0.16666...=>1/6)
rational.h depends on following specialized fields of std::numeric_limits<custom_type>
std::numeric_limits<custom_type>::is_signedstd::numeric_limits<custom_type>::is_integerstd::numeric_limits<custom_type>::is_exact
(gmp_rational.h provides this specializations for GMP versions below 5.1)
See the test cases for examples on how to use rational.h with C++ built-in types as well
as with the GNU Multiple Precision Arithmetic Library or
the CLN - Class Library for Numbers as underlying storage type.
-
The header
gmp_rational.hcontains specializations especially for the GNU Multiple Precision Arithmetic Library as underlying storage type.If you use the GMP extensions, you'll need to link your application with
-lgmpxx -lgmp -
The header
cln_rational.hcontains specializations especially for the CLN - Class Library for Numbers as underlying storage type.If you use the CLN extensions, you'll need to link your application with
-lcln