std::ratio

From Cppreference

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

template< intmax_t Num, intmax_t Denom = 1 >

class ratio {
public:
    static const num = /* see below */;
    static const dem = /* see below */;

}
(C++11 feature)

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
(C++11 feature)

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

num defines the reduced numerator
(public static member constant)
den defines the reduced denominator
(public static member constant)