当前位置:网站首页>ratio学习之ratio_add,ratio_subtract,ratio_multiply,ratio_divide的使用
ratio学习之ratio_add,ratio_subtract,ratio_multiply,ratio_divide的使用
2022-07-25 23:21:00 【班公湖里洗过脚】
ratio是比例类std::ratio代表一个比例,就是将给定的两个整数进行约分得到一个分数(分子及分母),例如3/6,约分后就是1/2。它主要用于很小的单位,比如,毫秒,微秒,毫米,微米,纳米等。下面是ratio_add,ratio_subtract,ratio_multiply,ratio_divide的使用。
类 | |
| 表示精确的有理分数 (类模板) | |
算术 | |
(C++11) | 在编译时相加两个 ratio 对象(别名模板) |
(C++11) | 在编译时相减两个 ratio 对象(别名模板) |
(C++11) | 在编译时相乘两个 ratio 对象(别名模板) |
(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
边栏推荐
猜你喜欢

Solution of phpstudy service environment 80 port occupied by process system under Windows

firewall 命令简单操作

General paging function

数组中重复的数字

Take root downward, grow upward, and explore the "root" power of Huawei cloud AI

Several commonly used traversal methods

Classes and objects (3)

Release of v6.5.1/2/3 series of versions of Xingyun housekeeper: the ability of database OpenAPI continues to be strengthened

How to set pseudo static for WordPress fixed links

WebMvcConfigurationSupport
随机推荐
JS regular expression matches IP address (IP address regular expression verification)
Several commonly used traversal methods
Secure code warrior learning record (IV)
ETL工具(数据同步) 二
[QNX Hypervisor 2.2用户手册]9.8 load
谷粒学苑P98踩坑 e.GlobalExceptionHandler : null
POI特效 市场调研
Servlet overview
Why are there many snapshot tables in the BI system?
新手开户选择哪个券商公司好呢?安全吗
[QNX Hypervisor 2.2用户手册]9.7 generate
What is a physical firewall? What's the effect?
idea设置get、set模板解决boolean类型字段的命名问题
Simulink学习笔记(三)——Simulink自动代码生成(二)「建议收藏」
加拿大EE通道
TS union type
The difference between MySQL clustered index and non clustered index
Flight control implementation of four rotor aircraft "suggestions collection"
TS basic data type
Moment.js