当前位置:网站首页>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
边栏推荐
- Understanding English (speech understanding)
- What is a physical firewall? What's the effect?
- Tencent map API request source is not authorized, this request source domain name
- TS function
- Discuz magazine / news report template (jeavi_line) utf8 GBK / DZ template download
- TS class
- r语言绘图参数(R语言plot画图)
- 四旋翼飞行器的飞控实现「建议收藏」
- Bind class style and bind style style
- Network Security Learning notes-1 file upload
猜你喜欢

How to set pseudo static for WordPress fixed links

Analysis of direction finding error of multi baseline interferometer system

wordpress去掉网站发布时间

Enterprise level inventory management system of code audit

多模态——Deep Multi-Modal Sets

The fifth article in the series of radar Fundamentals: the function of radar modulation style

Node Foundation

Idea sets get and set templates to solve the naming problem of boolean type fields

Firewall command simple operation

@Import
随机推荐
762. 二进制表示中质数个计算置位
Serialize common default values and column parameters
File contains vulnerability
Discuz atmosphere game style template / imitation lol hero League game DZ game template GBK
PHP JSON variable array problem
r语言绘图参数(R语言plot画图)
Implementation of mesh parameterized least squares conformal maps (3D mesh mapping to 2D plane)
Moment.js
类和对象(2)(6个默认成员函数)
TS interface
Release of v6.5.1/2/3 series of versions of Xingyun housekeeper: the ability of database OpenAPI continues to be strengthened
【微信小程序】页面导航
PyTorch的数据输入格式要求及转换
模拟实现string类常用接口
[QNX hypervisor 2.2 user manual]9.6 GDB
ETL tool (data synchronization) II
Summary of kotlin common knowledge points
多模态——Deep Multi-Modal Sets
Flight control implementation of four rotor aircraft "suggestions collection"
Serialize data type