Compile-time rational arithmetic

From Cppreference

Jump to: navigation, search

[edit] ratio

Defined in header <ratio>

template< intmax_t Num, intmax_t Denom = 1 >
class ratio;
(C++11 feature)

The class template ratio provides compile-time rational arithmetic support.

Several convenience SI typedefs are already provided:

Defined in header <ratio>

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] ratio arithmetic

Several class templates, that perform arithmetic operations on ratio objects at compile-time are provided.

Defined in header <ratio>
ratio_add (C++11)
adds two ratio objects at compile-time
(class template)
ratio_subtract (C++11)
subtracts two ratio objects at compile-time
(class template)
ratio_multiply (C++11)
multiplies two ratio objects at compile-time
(class template)
ratio_divide (C++11)
divides two ratio objects at compile-time
(class template)

[edit] ratio comparison

Several class templates, that perform comparison operations on ratio objects at compile-time are provided.

Defined in header <ratio>
ratio_equal (C++11)
compares two ratio objects for equality at compile-time
(class template)
ratio_not_equal (C++11)
compares two ratio objects for inequality at compile-time
(class template)
ratio_less (C++11)
compares two ratio objects for less than at compile-time
(class template)
ratio_less_equal (C++11)
compares two ratio objects for less than or equal to at compile-time
(class template)
ratio_greater (C++11)
compares two ratio objects for greater than at compile-time
(class template)
ratio_greater_equal (C++11)
compares two ratio objects for greater than or equal to at compile-time
(class template)