Rational is a C++ class implementing rational numbers of limited size with overflow detection. Both numerator and denominator are of type 'int'.

Rational allows you to perform exact calculations and verify at any time if overflow has occurred. Rational does not throw exceptions.

In case of domain error or overflow, denominator is set to 0 and numerator is set to >0, <0, or 0, meaning +INF, -INF, and NAN respectively. This error condition can be tested with the function 'error', and can only be cleared assigning a new value to the Rational.

While in error state, arithmetic operators become no ops and relational operators return false, except '!=', which returns true. This is done to preserve the error condition which triggered the error, and because the error value is considered to be out of domain and therefore is different from any valid value, but not necessarily larger or smaller.

The function 'parse' can be used to obtain values exactly representable by small fractions like '0.1', '1/3', or '355/113' from the command line.

The function 'to_decimal' prints values in decimal point form (123.456), while the function 'to_fraction' prints values in fraction form (123/456). Both functions print '+INF', '-INF', or 'NAN' to signal an error condition.

Values are kept normalized (gcd(numerator,denominator) == 1, and the denominator is positive). Range extends from INT_MAX to -INT_MAX. Maximum resolution is 1 / INT_MAX.

To use the Rational class in your own programs simply copy the files 'rational.h' and 'rational.cc' in your source tree. See the file 'main.cc' for an example of use.

Rational is used in combination with Arg_parser in GNU ddrescue and GNU Ocrad.


The latest released version of Rational can be found at http://download.savannah.gnu.org/releases/arg-parser/rational/. You may also subscribe to arg-parser-bug and receive an email every time a new version is released.

To decompress Rational tarballs you may need lzip from http://www.nongnu.org/lzip/lzip.html. Then use "tar -xf rational[version].tar.lz" or "lzip -cd rational[version].tar.lz | tar -xf -" to extract the files.

How to Get Help

For general discussion of bugs in Rational the mailing list arg-parser-bug@nongnu.org is the most appropriate forum. Please send messages as plain text. Please do not send messages encoded as HTML nor encoded as base64 MIME nor included as multiple formats. Please include a descriptive subject line. If all of the subject are "bug in rational" it is impossible to differentiate them.

An archive of the bug report mailing list is available at http://lists.gnu.org/mailman/listinfo/arg-parser-bug.

How to Help

To contact the author, either to report a bug or to contribute fixes or improvements, send mail to arg-parser-bug@nongnu.org. Please send messages as plain text. If posting patches they should be in unified diff format against the latest version. They should include a text description.

See also the Arg_parser project page at Savannah.


This library is free software distributed under a 2-clause BSD license.

Valid HTML 4.01 Strict

Copyright © 2020 Antonio Diaz Diaz

You are free to copy, modify and distribute all or part of this article without limitation.

Updated: 2020-02-22