Sunday, January 26, 2025

ratio library

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. 
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
NameDescription
ratio_addAdd 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_subtractSubtract 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_multiplyMultiply 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_divideDivide 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
NameDescription
ratio_equalCompare 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_equalCompare 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_lessCompare 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_equalCompare 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_greaterCompare 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_equalCompare 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;
The example depicts the usage.

Predefined Ratios
The following ratios are predefined. These are used in chrono library to define durations.
TypeDefinitionDescription
attoratio<1,1000000000000000000>10-18
femtoratio<1,1000000000000000>10-15
picoratio<1,1000000000000>10-12
nanoratio<1,1000000000>10-9
microratio<1,1000000>10-6
milliratio<1,1000>10-3
centiratio<1,100>10-2
deciratio<1,10>10-1
decaratio<10,1>101
hectoratio<100,1>102
kiloratio<1000,1>103
megaratio<1000000,1>106
gigaratio<1000000000,1>109
teraratio<1000000000000,1>1012
petaratio<1000000000000000,1>1015
exaratio<1000000000000000000,1>1018


No comments:

Post a Comment