[原]C++新标准之std::ratio

概览

std::ratio定义在<ratio>文件中,提供了编译期的比例计算功能。为std::chrono::duration提供基础服务。

类定义

std::ratio是一个模板类,关键代码摘录如下(格式有调整):

  1. template<intmax_t _Nx, intmax_t _Dx = 1
  2. struct ratio 
  3. static_assert(_Dx != 0, "zero denominator"); 
  4. static_assert(-INTMAX_MAX <= _Nx, "numerator too negative"); 
  5. static_assert(-INTMAX_MAX <= _Dx, "denominator too negative"); 
  6.  
  7. static constexpr intmax_t num = _Sign_of<_Nx>::value 
  8. * _Sign_of<_Dx>::value * _Abs<_Nx>::value / _Gcd<_Nx, _Dx>::value; 
  9.  
  10. static constexpr intmax_t den = _Abs<_Dx>::value / _Gcd<_Nx, _Dx>::value; 
  11.  
  12. typedef ratio<num, den> type; 
  13. }; 

第一个参数_Nx代表了分子,第二个参数 _Dx代表了分母。
num是计算后的分子,den是计算后的分母。在duration转换的时候会用到这两个值。

注:这里的计算是指约分,可以看到传入的分子和分母都除以了最大公约数。

numnumerator的缩写,表示分子。
dendenominator的缩写,表示分母。

预定义ratio

为了方便代码的书写,标准库提供了如下定义:

Type Definition
yocto std::ratio<1, 1000000000000000000000000>, if std::intmax_t can represent the denominator
zepto std::ratio<1, 1000000000000000000000>, if std::intmax_t can represent the denominator
atto std::ratio<1, 1000000000000000000>
femto std::ratio<1, 1000000000000000>
pico std::ratio<1, 1000000000000>
nano std::ratio<1, 1000000000>
micro std::ratio<1, 1000000>
milli std::ratio<1, 1000>
centi std::ratio<1, 100>
deci std::ratio<1, 10>
deca std::ratio<10, 1>
hecto std::ratio<100, 1>
kilo std::ratio<1000, 1>
mega std::ratio<1000000, 1>
giga std::ratio<1000000000, 1>
tera std::ratio<1000000000000, 1>
peta std::ratio<1000000000000000, 1>
exa std::ratio<1000000000000000000, 1>
zetta std::ratio<1000000000000000000000, 1>, if std::intmax_t can represent the numerator
yotta std::ratio<1000000000000000000000000, 1>, if std::intmax_t can represent the numerator

应用

如果想表示1/5,那么可以这样写std::ratio<1, 5>
回想我们在学习std::chrono::duration中遇到到的milli的定义:
typedef ratio<1, 1000> milli;,表示一千分之一,因为约定了基本计算单位是,所以milli表示一千分之一秒。

示例代码

  1. #include <iostream> 
  2. #include <chrono> 
  3.  
  4. typedef std::chrono::duration<float, std::ratio<3, 1> > three_seconds; 
  5. typedef std::chrono::duration<float, std::ratio<1, 10> > one_tenth_seconds; 
  6.  
  7. int main() 
  8. three_seconds s = std::chrono::duration_cast<three_seconds>(one_tenth_seconds(3)); 
  9. std::cout << "3 [1/10 seconds] equal to " << s.count() << " [3 seconds] "
  10. std::cin.get(); 

参考资料

原文地址:https://www.cnblogs.com/bianchengnan/p/9478844.html