We recommend using Visual Studio 2017



The latest version of this topic can be found at <ratio>.

Include the standard header <ratio> to define constants and templates that are used to store and manipulate rational numbers at compile time.

#include <ratio>  

ratio Structure

struct ratio { static constexpr intmax_t num; static constexpr intmax_t den; typedef ratio<num, den>
type; };

The ratio Structure defines the static constants num and den such that num / den == N / D and num and den have no common factors. num / den is the value that is represented by the template class. Therefore, type designates the instantiation ratio<N0, D0> for which num == N0 and den == D0.


<ratio> also defines specializations of ratio that have the following form.

template <class R1, class R2> struct ratio_specialization

Each specialization takes two template parameters that must also be specializations of ratio. The value of type is determined by an associated logical operation.

Nametype Value
ratio_addR1 + R2
ratio_divideR1 / R2
ratio_equalR1 == R2
ratio_greaterR1 > R2
ratio_greater_equalR1 >= R2
ratio_lessR1 < R2
ratio_less_equalR1 <= R2
ratio_multiplyR1 * R2
ratio_not_equal!(R1 == R2)
ratio_subtractR1 - R2


typedef ratio<1,  1000000000000000000> atto;
typedef ratio<1,     1000000000000000> femto;
typedef ratio<1,        1000000000000> pico;
typedef ratio<1, 1000000000> nano;
typedef ratio<1, 1000000> micro;
typedef ratio<1, 1000> milli;
typedef ratio<1,  100> centi;
typedef ratio<1,   10> deci;
typedef ratio<10, 1> deca;
typedef ratio<100, 1> hecto;
typedef ratio<1000, 1> kilo;
typedef ratio<1000000, 1> mega;
typedef ratio<1000000000, 1> giga;
typedef ratio<1000000000000, 1> tera;
typedef ratio<1000000000000000, 1> peta;
typedef ratio<1000000000000000000, 1> exa;

Header Files Reference