Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All


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

#include <ratio>

template <intmax_t N, intmax_t D = 1>
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.


type Value


R1 + R2


R1 / R2


R1 == R2


R1 > R2


R1 >= R2


R1 < R2


R1 <= R2


R1 * R2


!(R1 == R2)


R1 - 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;
© 2015 Microsoft