std::ratio

From Cppreference

Jump to: navigation, search
Defined in header <ratio>

template<

    intmax_t Num,
    intmax_t Denom = 1

> class ratio;
(since C++11)

The class template ratio is the core class in the compile-time rational arithmetic library. It reduces the supplied numerator and denominator to the lowest terms.

Several convenience SI typedefs are already provided:

typedef std::ratio<1, 1000000000000000000000000> yocto; //see below

typedef std::ratio<1, 1000000000000000000000> zepto;    //see below
typedef std::ratio<1, 1000000000000000000> atto;
typedef std::ratio<1, 1000000000000000> femto;
typedef std::ratio<1, 1000000000000> pico;
typedef std::ratio<1, 1000000000> nano;
typedef std::ratio<1, 1000000> micro;
typedef std::ratio<1, 1000> milli;
typedef std::ratio<1, 100> centi;
typedef std::ratio<1, 10> deci;
typedef std::ratio<10, 1> deca;
typedef std::ratio<100, 1> hecto;
typedef std::ratio<1000, 1> kilo;
typedef std::ratio<1000000, 1> mega;
typedef std::ratio<1000000000, 1> giga;
typedef std::ratio<1000000000000, 1> tera;
typedef std::ratio<1000000000000000, 1> peta;
typedef std::ratio<1000000000000000000, 1> exa;
typedef std::ratio<1000000000000000000000, 1> zetta;    //see below

typedef std::ratio<1000000000000000000000000, 1> yotta; //see below
(since C++11)

The typedefs yocto, zepto, zetta, yotta are provided only if intmax_t is sufficient to represent the values used to instantiate the templates.

[edit] Template parameters

Nom - defines the numerator of the ratio
Denom - defines the denominator of the ratio. It cannot be equal to 0

[edit] Member objects

static constexpr intmax_t num
[static]
defines the reduced numerator. Equivalent to sign(N) * sign(D) * abs(N) / gcd(D,N)
(public static member constant)
static constexpr intmax_t den
[static]
defines the reduced denominator. Equivalent to abs(D) / gcd(D,N)
(public static member constant)