ratio学习之ratio_add,ratio_subtract,ratio_multiply,ratio_divide的使用

ratio是比例类std::ratio代表一个比例,就是将给定的两个整数进行约分得到一个分数(分子及分母),例如3/6,约分后就是1/2。它主要用于很小的单位,比如,毫秒,微秒,毫米,微米,纳米等。下面是ratio_add,ratio_subtract,ratio_multiply,ratio_divide的使用。

ratio

表示精确的有理分数
(类模板)

算术

ratio_add

(C++11)

在编译时相加两个 ratio 对象
(别名模板)

ratio_subtract

(C++11)

在编译时相减两个 ratio 对象
(别名模板)

ratio_multiply

(C++11)

在编译时相乘两个 ratio 对象
(别名模板)

ratio_divide

(C++11)

在编译时相除两个 ratio 对象
(别名模板)
#include <iostream>
#include <ratio>


using namespace std;

int main()
{
    ratio<2, 6> two_six;
    ratio<3, 6> three_six;

    cout << "two_six======" << two_six.num << "/" << two_six.den << endl;
    cout << "two_six======" << three_six.num << "/" << three_six.den << endl;

    cout << "Hello World!" << endl;
    return 0;
}

 ratio_add 两个ratio对象相加

#include <iostream>
#include <ratio>


using namespace std;

int main()
{
    ratio<2, 6> two_six;
    ratio<3, 6> three_six;

    cout << "two_six======" << two_six.num << "/" << two_six.den << endl;
    cout << "two_six======" << three_six.num << "/" << three_six.den << endl;

    ratio_add<decltype(two_six), decltype(three_six)> ratioAdd;
    cout << "ratio_add======" << ratioAdd.num << "/" << ratioAdd.den << endl;

    cout << "Hello World!" << endl;
    return 0;
}

ratio_subtract两个ratio对象相减
#include <iostream>
#include <ratio>


using namespace std;

int main()
{
    ratio<2, 6> two_six;
    ratio<3, 6> three_six;

    cout << "two_six======" << two_six.num << "/" << two_six.den << endl;
    cout << "two_six======" << three_six.num << "/" << three_six.den << endl;

    ratio_subtract<decltype(two_six), decltype(three_six)> ratioSubtract;
    cout << "ratio_subtract======" << ratioSubtract.num << "/" << ratioSubtract.den << endl;

    cout << "Hello World!" << endl;
    return 0;
}

 

ratio_multiply两个ratio对象相乘
#include <iostream>
#include <ratio>


using namespace std;

int main()
{
    ratio<2, 6> two_six;
    ratio<3, 6> three_six;

    cout << "two_six======" << two_six.num << "/" << two_six.den << endl;
    cout << "two_six======" << three_six.num << "/" << three_six.den << endl;

    ratio_multiply<decltype(two_six), decltype(three_six)> ratioMultiply;
    cout << "ratio_multiply======" << ratioMultiply.num << "/" << ratioMultiply.den << endl;

    cout << "Hello World!" << endl;
    return 0;
}

 ratio_divide两个ratio对象相除

#include <iostream>
#include <ratio>


using namespace std;

int main()
{
    ratio<2, 6> two_six;
    ratio<3, 6> three_six;

    cout << "two_six======" << two_six.num << "/" << two_six.den << endl;
    cout << "two_six======" << three_six.num << "/" << three_six.den << endl;

    ratio_divide<decltype(two_six), decltype(three_six)> ratioDivide;
    cout << "ratio_divide======" << ratioDivide.num << "/" << ratioDivide.den << endl;

    cout << "Hello World!" << endl;
    return 0;
}

 

参考:

标准库头文件 <ratio> - cppreference.com

C++11 std::chrono时间库_长乐村长的博客-CSDN博客_std::chrono

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值