Overview
Ratios are used everywhere. For example, 1 millisecond is represented as 1/1000 second. 1 kilogram is represented as 1000 grams Similarly in US, gasoline per gallon is represented as 4 dollars, 89 cents and 9/10 cent.
Details
standard library defines a new template class ratio to define ratios that can be used only during compile time.
standard library defines a new template class ratio to define ratios that can be used only during compile time.
It's represented as shown below.
template <intmax_t N, intmax_t D = 1> struct ratio { static constexpr intmax_t num=N; static constexpr intmax_t den=D; typedef ratio<num, den> type; }; //Examples ratio<9, 10> gas_appendage; ratio<1, 1000> millisec; ratio<1000, 1> kilogram;
ratio is used in chrono library to represent different time units such as millisecond, hour etc. Note that ratio<2,3> is same as ratio<4,6>.
Operations
As ratios are not meant to be instantiated like objects, operators such as +,-,*, == are not available. Instead following template functions are defined.
Arithmetic Operations
Name | Description |
---|---|
ratio_add | Add two ratios Example using two_third = ratio<2, 3>; using one_sixth = ratio<1, 6>; //ret:5/6 using ret = ratio_add<two_third, one_sixth>; |
ratio_subtract | Subtract ratios Example using two_third = ratio<2, 3>; using one_sixth = ratio<1, 6>; //ret:1/2 using ret = ratio_subtract<two_third, one_sixth>; |
ratio_multiply | Multiply two ratios Example using two_third = ratio<2, 3>; using one_sixth = ratio<1, 6>; //ret:1/9 using ret = ratio_multiply<two_third, one_sixth>; |
ratio_divide | Divide ratios Example using two_third = ratio<2, 3>; using one_sixth = ratio<1, 6>; //ret:4/1 using ret = ratio_divide<two_third, one_sixth>; |
Comparison Operations
Name | Description |
---|---|
ratio_equal | Compare ratios Example using four_third = ratio<4, 6>; using two_third = ratio<2, 3>; //ret:true auto ret = ratio_equal<four_third, two_third>::value; |
ratio_not_equal | Compare ratios for inequality Example using four_third = ratio<4, 6>; using two_third = ratio<2, 3>; //ret:false auto ret = ratio_not_equal<four_third, two_third>::value; |
ratio_less | Compare ratios for less-than inequality Example using four_third = ratio<4, 6>; using two_third = ratio<2, 3>; //ret:false auto ret = ratio_less<four_third, two_third>::value; |
ratio_less_equal | Compare ratios for equality or less-than inequality Example using four_third = ratio<4, 6>; using two_third = ratio<2, 3>; //ret:true auto ret = ratio_less_equal<four_third, two_third>::value; |
ratio_greater | Compare ratios for greater than inequality Example using four_third = ratio<4, 6>; using two_third = ratio<2, 3>; //ret:false auto ret = ratio_greater<four_third, two_third>::value; |
ratio_greater_equal | Compare ratios for equality or greater-than inequality Example using four_third = ratio<4, 6>; using two_third = ratio<2, 3>; //ret:true auto ret = ratio_greater_equal<four_third, two_third>::value; |
Predefined Ratios
The following ratios are predefined. These are used in chrono library to define durations.
Type | Definition | Description |
---|---|---|
atto | ratio<1,1000000000000000000> | 10-18 |
femto | ratio<1,1000000000000000> | 10-15 |
pico | ratio<1,1000000000000> | 10-12 |
nano | ratio<1,1000000000> | 10-9 |
micro | ratio<1,1000000> | 10-6 |
milli | ratio<1,1000> | 10-3 |
centi | ratio<1,100> | 10-2 |
deci | ratio<1,10> | 10-1 |
deca | ratio<10,1> | 101 |
hecto | ratio<100,1> | 102 |
kilo | ratio<1000,1> | 103 |
mega | ratio<1000000,1> | 106 |
giga | ratio<1000000000,1> | 109 |
tera | ratio<1000000000000,1> | 1012 |
peta | ratio<1000000000000000,1> | 1015 |
exa | ratio<1000000000000000000,1> | 1018 |
No comments:
Post a Comment