元编程库-编译期有理数

类模板std::ratio及相关的模板提供编译期有理数算术支持。文章的代码库为:

https://gitee.com/gamestorm577/CppStd

ratio

类模版ratio表示分数

template <intmax_t Num, intmax_t Den = 1>
class ratio;

Num表示分数,Den表示分母。ratio有两个成员num以及den,num表示约分后的分子,den表示约分后的分母,例如:

std::ratio<20, 30> val;
std::cout << "val num = " << val.num << std::endl;
std::cout << "val den = " << val.den << std::endl;

输出结果为:

val num = 2
val den = 3

标准库中定义了一些常用的有理数,例如:

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>

编译时有理数算术

标准库提供了别名模版可用于编译期进行ratio的计算

ratio_add

在编译期相加两个ratio,例如:

using two_third = std::ratio<2, 3>;
using third_four = std::ratio<3, 4>;
using sum = std::ratio_add<two_third, third_four>;
sum val;
std::cout << "val num = " << val.num << std::endl;
std::cout << "val den = " << val.den << std::endl;

输出结果为:

val num = 17
val den = 12

ratio_subtract

在编译期相减两个ratio,例如:

using two_third = std::ratio<2, 3>;
using third_four = std::ratio<3, 4>;
using subtract = std::ratio_subtract<two_third, third_four>;
std::cout << "subtract num = " << subtract::num << std::endl;
std::cout << "subtract den = " << subtract::den << std::endl;

输出结果为:

subtract num = -1
subtract den = 12

ratio_multiply

在编译期相乘两个ratio,例如:

using two_third = std::ratio<2, 3>;
using third_four = std::ratio<3, 4>;
using multiply = std::ratio_multiply<two_third, third_four>;
std::cout << "multiply num = " << multiply::num << std::endl;
std::cout << "multiply den = " << multiply::den << std::endl;

输出结果为:

multiply num = 1
multiply den = 2

ratio_divide

在编译期相除两个ratio,例如:

using two_third = std::ratio<2, 3>;
using third_four = std::ratio<3, 4>;
using divide = std::ratio_divide<two_third, third_four>;
std::cout << "divide num = " << divide::num << std::endl;
std::cout << "divide den = " << divide::den << std::endl;

输出结果为:

divide num = 8
divide den = 9

编译期有理数比较

标准库提供了类模版用于编译期进行ratio的比较

ratio_equal

在编译期比较两个ratio的相等性,例如:

using two_third = std::ratio<2, 3>;
using third_four = std::ratio<3, 4>;
using result = std::ratio_equal<two_third, third_four>;
printf("result value = %d\n", result::value);

输出结果为:

result value = 0

ratio_not_equal

在编译期比较两个ratio的不相等性,例如:

using two_third = std::ratio<2, 3>;
using third_four = std::ratio<3, 4>;
using result = std::ratio_not_equal<two_third, third_four>;
printf("result value = %d\n", result::value);

输出结果为: 

result value = 1

ratio_less

在编译期比较两个ratio的小于关系,例如:

using two_third = std::ratio<2, 3>;
using third_four = std::ratio<3, 4>;
using result = std::ratio_less<two_third, third_four>;
printf("result value = %d\n", result::value);

输出结果为: 

result value = 1

ratio_less_equal

在编译期比较两个ratio的小于或等于关系,例如:

using two_third = std::ratio<2, 3>;
using third_four = std::ratio<3, 4>;
using result = std::ratio_less_equal<two_third, third_four>;
printf("result value = %d\n", result::value);

输出结果为: 

result value = 1

ratio_greater

在编译期比较两个ratio的大于关系,例如:

using two_third = std::ratio<2, 3>;
using third_four = std::ratio<3, 4>;
using result = std::ratio_greater<two_third, third_four>;
printf("result value = %d\n", result::value);

输出结果为: 

result value = 0

ratio_greater_equal

在编译期比较两个ratio的大于或等于关系,例如:

using two_third = std::ratio<2, 3>;
using third_four = std::ratio<3, 4>;
using result = std::ratio_greater_equal<two_third, third_four>;
printf("result value = %d\n", result::value);

 输出结果为: 

result value = 0

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值