类模板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