velnias75 / rational

A C++ rational (fraction) template class
GNU General Public License v3.0
8 stars 0 forks source link
cln cplusplus cplusplus-11 expression-template fraction gmp library math mathematics numbertheory template

rational

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.

Features

Notes for custom number types

rational.h depends on following specialized fields of std::numeric_limits<custom_type>

(gmp_rational.h provides this specializations for GMP versions below 5.1)

How to use

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.