A C++ template library for embedded applications
MIT licensed
Designed and
maintained by
John Wellbelove

ratio

A template constant to encode ratios.
C++11 equivalent std::ratio

____________________________________________________________________________________________________
template <const size_t NUM, const size_t DEN = 1>
struct ratio
{
  static ETL_CONSTANT intmax_t num = NUM;
  static ETL_CONSTANT intmax_t den = DEN;
};

num : Numerator
den : Denominator

____________________________________________________________________________________________________

Predefined ratios


If INT_MAX > INT32_MAX
typedef ratio<1, 1000000000000000000000000> yocto;
typedef ratio<1, 1000000000000000000000>    zepto;
typedef ratio<1, 1000000000000000000>       atto;
typedef ratio<1, 1000000000000000>          femto;
typedef ratio<1, 1000000000000>             pico;

If INT_MAX >= INT32_MAX
typedef ratio<1, 1000000000>                nano;
typedef ratio<1, 1000000>                   micro;

If INT_MAX >= INT16_MAX
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;

If INT_MAX >= INT32_MAX
typedef ratio<1000000, 1>                   mega;
typedef ratio<1000000000, 1>                giga;

If INT_MAX > INT32_MAX
typedef ratio<1000000000000, 1>             tera;
typedef ratio<1000000000000000, 1>          peta;
typedef ratio<1000000000000000000, 1>       exa;
typedef ratio<1000000000000000000000, 1>    zetta;
typedef ratio<1000000000000000000000000, 1> yotta;

An approximation of PI to 6 digits.
typedef ratio<355, 113> ratio_pi;

An approximation of root 2.
typedef ratio<239, 169> ratio_root2;

An approximation of e.
typedef ratio<326, 120> ratio_e;
ratio.h