C++新标准之std::ratio

https://www.cnblogs.com/bianchengnan/p/9478844.html

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

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

template<intmax_t _Nx,	intmax_t _Dx = 1> 
struct ratio 
{ 
    static_assert(_Dx != 0,	"zero denominator"); 
    static_assert(-INTMAX_MAX <= _Nx, "numerator too negative"); 
    static_assert(-INTMAX_MAX <= _Dx, "denominator too negative"); 
 
    static constexpr intmax_t num = _Sign_of<_Nx>::value 
        * _Sign_of<_Dx>::value * _Abs<_Nx>::value / _Gcd<_Nx, _Dx>::value; 
 
    static constexpr intmax_t den = _Abs<_Dx>::value / _Gcd<_Nx, _Dx>::value; 
 
    typedef ratio<num, den> type; 
}; 

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

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

num是numerator的缩写,表示分子。
den是denominator的缩写,表示分母。

预定义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表示一千分之一秒。

示例代码

#include <iostream> 
#include <chrono> 
 
typedef std::chrono::duration<float, std::ratio<3, 1> > three_seconds; 
typedef std::chrono::duration<float, std::ratio<1, 10> > one_tenth_seconds; 
 
int main() 
{ 
    three_seconds s = std::chrono::duration_cast<three_seconds>(one_tenth_seconds(3)); 
    std::cout << "3 [1/10 seconds] equal to " << s.count() << " [3 seconds]\n"; 
    std::cin.get(); 
} 

参考资料
vs源码
cppreference

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值