主题
D.6 <ratio>头文件
<ratio>头文件提供在编译时进行的计算。
头文件内容
cpp
namespace std
{
template<intmax_t N,intmax_t D=1>
class ratio;
// ratio arithmetic
template <class R1, class R2>
using ratio_add = see description;
template <class R1, class R2>
using ratio_subtract = see description;
template <class R1, class R2>
using ratio_multiply = see description;
template <class R1, class R2>
using ratio_divide = see description;
// ratio comparison
template <class R1, class R2>
struct ratio_equal;
template <class R1, class R2>
struct ratio_not_equal;
template <class R1, class R2>
struct ratio_less;
template <class R1, class R2>
struct ratio_less_equal;
template <class R1, class R2>
struct ratio_greater;
template <class R1, class R2>
struct ratio_greater_equal;
typedef ratio<1, 1000000000000000000> atto;
typedef ratio<1, 1000000000000000> femto;
typedef ratio<1, 1000000000000> pico;
typedef ratio<1, 1000000000> nano;
typedef ratio<1, 1000000> micro;
typedef ratio<1, 1000> milli;
typedef ratio<1, 100> centi;
typedef ratio<1, 10> deci;
typedef ratio<10, 1> deca;
typedef ratio<100, 1> hecto;
typedef ratio<1000, 1> kilo;
typedef ratio<1000000, 1> mega;
typedef ratio<1000000000, 1> giga;
typedef ratio<1000000000000, 1> tera;
typedef ratio<1000000000000000, 1> peta;
typedef ratio<1000000000000000000, 1> exa;
}##D.6.1 std::ratio类型模板
std::ratio类型模板提供了一种对在编译时进行计算的机制,通过调用合理的数,例如:半(std::ratio<1,2>),2/3(std::ratio<2, 3>)或15/43(std::ratio<15, 43>)。其使用在C++标准库内部,用于初始化std::chrono::duration类型模板。
类型定义
cpp
template <intmax_t N, intmax_t D = 1>
class ratio
{
public:
typedef ratio<num, den> type;
static constexpr intmax_t num= see below;
static constexpr intmax_t den= see below;
};要求
D不能为0。
描述
num和den分别为分子和分母,构造分数N/D。den总是正数。当N和D的符号相同,那么num为正数;否则num为负数。
例子
cpp
ratio<4,6>::num == 2
ratio<4,6>::den == 3
ratio<4,-6>::num == -2
ratio<4,-6>::den == 3本节可运行示例(覆盖本节要点,
g++ -std=c++17 -pthread可直接编译运行):
cpp
#include <iostream>
#include <ratio>
// 演示 std::ratio:编译时有理数,自动约分,分母恒为正
int main() {
// 基本构造与自动约分:ratio<N,D> 归约到最简分数
std::cout << "ratio<4,6> = " << std::ratio<4,6>::num
<< "/" << std::ratio<4,6>::den << " (期望 2/3)\n";
std::cout << "ratio<4,-6> = " << std::ratio<4,-6>::num
<< "/" << std::ratio<4,-6>::den << " (期望 -2/3)\n";
std::cout << "ratio<-4,-6> = " << std::ratio<-4,-6>::num
<< "/" << std::ratio<-4,-6>::den << " (期望 2/3)\n";
std::cout << "ratio<15,43> = " << std::ratio<15,43>::num
<< "/" << std::ratio<15,43>::den << " (期望 15/43)\n";
std::cout << "ratio<6,3> = " << std::ratio<6,3>::num
<< "/" << std::ratio<6,3>::den << " (期望 2/1)\n";
// 标准库预定义的 SI 前缀别名(均为 ratio 的 typedef)
std::cout << "\nSI 前缀别名:\n";
std::cout << "milli = " << std::milli::num << "/" << std::milli::den << "\n";
std::cout << "centi = " << std::centi::num << "/" << std::centi::den << "\n";
std::cout << "micro = " << std::micro::num << "/" << std::micro::den << "\n";
std::cout << "kilo = " << std::kilo::num << "/" << std::kilo::den << "\n";
std::cout << "mega = " << std::mega::num << "/" << std::mega::den << "\n";
std::cout << "giga = " << std::giga::num << "/" << std::giga::den << "\n";
// 编译期验证:ratio 是类型,可在 static_assert 中使用
static_assert(std::ratio<4,6>::num == 2 && std::ratio<4,6>::den == 3, "约分错误");
static_assert(std::milli::num == 1 && std::milli::den == 1000, "milli 错误");
std::cout << "\nstatic_assert 全部通过\n";
return 0;
}D.6.2 std::ratio_add模板别名
std::ratio_add模板别名提供了两个std::ratio在编译时相加的机制(使用有理计算)。
定义
cpp
template <class R1, class R2>
using ratio_add = std::ratio<see below>;先决条件
R1和R2必须使用std::ratio进行初始化。
效果
ratio_add<R1, R2>被定义为一个别名,如果两数可以计算,且无溢出,该类型可以表示两个std::ratio对象R1和R2的和。如果计算出来的结果溢出了,那么程序里面就有问题了。在算术溢出的情况下,std::ratio_add<R1, R2>应该应该与std::ratio<R1::num * R2::den + R2::num * R1::den, R1::den * R2::den>相同。
例子
cpp
std::ratio_add<std::ratio<1,3>, std::ratio<2,5> >::num == 11
std::ratio_add<std::ratio<1,3>, std::ratio<2,5> >::den == 15
std::ratio_add<std::ratio<1,3>, std::ratio<7,6> >::num == 3
std::ratio_add<std::ratio<1,3>, std::ratio<7,6> >::den == 2本节可运行示例(覆盖本节要点,
g++ -std=c++17 -pthread可直接编译运行):
cpp
#include <iostream>
#include <ratio>
#include <chrono>
// 演示 std::ratio_add:编译时两个有理数相加,结果自动约分
int main() {
// 1/3 + 2/5 = (5+6)/15 = 11/15
using R = std::ratio_add<std::ratio<1,3>, std::ratio<2,5>>;
std::cout << "1/3 + 2/5 = " << R::num << "/" << R::den << " (期望 11/15)\n";
// 1/3 + 7/6 = (2+7)/6 = 9/6 = 3/2
using R2 = std::ratio_add<std::ratio<1,3>, std::ratio<7,6>>;
std::cout << "1/3 + 7/6 = " << R2::num << "/" << R2::den << " (期望 3/2)\n";
// 2/4 + 1/4 = 3/4
using R3 = std::ratio_add<std::ratio<2,4>, std::ratio<1,4>>;
std::cout << "2/4 + 1/4 = " << R3::num << "/" << R3::den << " (期望 3/4)\n";
// 与 SI 前缀相加:milli + centi = 1/1000 + 1/100 = 11/1000
using R4 = std::ratio_add<std::milli, std::centi>;
std::cout << "milli + centi = " << R4::num << "/" << R4::den << " (期望 11/1000)\n";
// 结合 chrono duration:1/3 秒周期与 2/5 秒周期相加
using P = std::ratio_add<std::ratio<1,3>, std::ratio<2,5>>;
std::chrono::duration<int, P> d(1);
std::cout << "duration<int,11/15>(1).count()=" << d.count() << "\n";
static_assert(R::num == 11 && R::den == 15, "");
std::cout << "static_assert 通过\n";
return 0;
}D.6.3 std::ratio_subtract模板别名
std::ratio_subtract模板别名提供两个std::ratio数在编译时进行相减(使用有理计算)。
定义
cpp
template <class R1, class R2>
using ratio_subtract = std::ratio<see below>;先决条件
R1和R2必须使用std::ratio进行初始化。
效果
ratio_add<R1, R2>被定义为一个别名,如果两数可以计算,且无溢出,该类型可以表示两个std::ratio对象R1和R2的和。如果计算出来的结果溢出了,那么程序里面就有问题了。在算术溢出的情况下,std::ratio_subtract<R1, R2>应该应该与std::ratio<R1::num * R2::den - R2::num * R1::den, R1::den * R2::den>相同。
例子
cpp
std::ratio_subtract<std::ratio<1,3>, std::ratio<1,5> >::num == 2
std::ratio_subtract<std::ratio<1,3>, std::ratio<1,5> >::den == 15
std::ratio_subtract<std::ratio<1,3>, std::ratio<7,6> >::num == -5
std::ratio_subtract<std::ratio<1,3>, std::ratio<7,6> >::den == 6本节可运行示例(覆盖本节要点,
g++ -std=c++17 -pthread可直接编译运行):
cpp
#include <iostream>
#include <ratio>
#include <chrono>
// 演示 std::ratio_subtract:编译时两个有理数相减,结果自动约分
int main() {
// 1/3 - 1/5 = (5-3)/15 = 2/15
using R = std::ratio_subtract<std::ratio<1,3>, std::ratio<1,5>>;
std::cout << "1/3 - 1/5 = " << R::num << "/" << R::den << " (期望 2/15)\n";
// 1/3 - 7/6 = (2-7)/6 = -5/6(符号在分子,分母恒正)
using R2 = std::ratio_subtract<std::ratio<1,3>, std::ratio<7,6>>;
std::cout << "1/3 - 7/6 = " << R2::num << "/" << R2::den << " (期望 -5/6)\n";
// 2/4 - 1/4 = 1/4
using R3 = std::ratio_subtract<std::ratio<2,4>, std::ratio<1,4>>;
std::cout << "2/4 - 1/4 = " << R3::num << "/" << R3::den << " (期望 1/4)\n";
// 1/2 - 3/4 = -1/4
using R4 = std::ratio_subtract<std::ratio<1,2>, std::ratio<3,4>>;
std::cout << "1/2 - 3/4 = " << R4::num << "/" << R4::den << " (期望 -1/4)\n";
// SI 前缀:centi - milli = 1/100 - 1/1000 = 9/1000
using R5 = std::ratio_subtract<std::centi, std::milli>;
std::cout << "centi - milli = " << R5::num << "/" << R5::den << " (期望 9/1000)\n";
// 实际用途:相减结果可作为 chrono duration 的周期单位
using D = std::chrono::duration<int, R>;
std::cout << "duration<int,2/15> 周期=" << D::period::num << "/" << D::period::den << "\n";
static_assert(R2::num == -5 && R2::den == 6, "减法结果错误");
std::cout << "static_assert 通过\n";
return 0;
}D.6.4 std::ratio_multiply模板别名
std::ratio_multiply模板别名提供两个std::ratio数在编译时进行相乘(使用有理计算)。
定义
cpp
template <class R1, class R2>
using ratio_multiply = std::ratio<see below>;先决条件
R1和R2必须使用std::ratio进行初始化。
效果
ratio_add<R1, R2>被定义为一个别名,如果两数可以计算,且无溢出,该类型可以表示两个std::ratio对象R1和R2的和。如果计算出来的结果溢出了,那么程序里面就有问题了。在算术溢出的情况下,std::ratio_multiply<R1, R2>应该应该与std::ratio<R1::num * R2::num, R1::den * R2::den>相同。
例子
cpp
std::ratio_multiply<std::ratio<1,3>, std::ratio<2,5> >::num == 2
std::ratio_multiply<std::ratio<1,3>, std::ratio<2,5> >::den == 15
std::ratio_multiply<std::ratio<1,3>, std::ratio<15,7> >::num == 5
std::ratio_multiply<std::ratio<1,3>, std::ratio<15,7> >::den == 7本节可运行示例(覆盖本节要点,
g++ -std=c++17 -pthread可直接编译运行):
cpp
#include <iostream>
#include <ratio>
#include <chrono>
// 演示 std::ratio_multiply:编译时两个有理数相乘,结果自动约分
int main() {
// 1/3 * 2/5 = 2/15
using R = std::ratio_multiply<std::ratio<1,3>, std::ratio<2,5>>;
std::cout << "1/3 * 2/5 = " << R::num << "/" << R::den << " (期望 2/15)\n";
// 1/3 * 15/7 = 15/21 = 5/7(自动约分)
using R2 = std::ratio_multiply<std::ratio<1,3>, std::ratio<15,7>>;
std::cout << "1/3 * 15/7 = " << R2::num << "/" << R2::den << " (期望 5/7)\n";
// 2/4 * 4/2 = 8/8 = 1/1
using R3 = std::ratio_multiply<std::ratio<2,4>, std::ratio<4,2>>;
std::cout << "2/4 * 4/2 = " << R3::num << "/" << R3::den << " (期望 1/1)\n";
// 负数:1/3 * -2/5 = -2/15
using R4 = std::ratio_multiply<std::ratio<1,3>, std::ratio<-2,5>>;
std::cout << "1/3 * -2/5 = " << R4::num << "/" << R4::den << " (期望 -2/15)\n";
// SI 前缀:kilo * milli = 1000/1 * 1/1000 = 1/1
using R5 = std::ratio_multiply<std::kilo, std::milli>;
std::cout << "kilo * milli = " << R5::num << "/" << R5::den << " (期望 1/1)\n";
// 实际用途:乘积结果可作为 chrono duration 的周期单位
using D = std::chrono::duration<int, R>;
std::cout << "duration<int,2/15> 周期=" << D::period::num << "/" << D::period::den << "\n";
static_assert(R2::num == 5 && R2::den == 7, "乘法结果错误");
std::cout << "static_assert 通过\n";
return 0;
}D.6.5 std::ratio_divide模板别名
std::ratio_divide模板别名提供两个std::ratio数在编译时进行相除(使用有理计算)。
定义
cpp
template <class R1, class R2>
using ratio_multiply = std::ratio<see below>;先决条件
R1和R2必须使用std::ratio进行初始化。
效果
ratio_add<R1, R2>被定义为一个别名,如果两数可以计算,且无溢出,该类型可以表示两个std::ratio对象R1和R2的和。如果计算出来的结果溢出了,那么程序里面就有问题了。在算术溢出的情况下,std::ratio_multiply<R1, R2>应该应该与std::ratio<R1::num * R2::num * R2::den, R1::den * R2::den>相同。
例子
cpp
std::ratio_divide<std::ratio<1,3>, std::ratio<2,5> >::num == 5
std::ratio_divide<std::ratio<1,3>, std::ratio<2,5> >::den == 6
std::ratio_divide<std::ratio<1,3>, std::ratio<15,7> >::num == 7
std::ratio_divide<std::ratio<1,3>, std::ratio<15,7> >::den == 45本节可运行示例(覆盖本节要点,
g++ -std=c++17 -pthread可直接编译运行):
cpp
#include <iostream>
#include <ratio>
#include <chrono>
// 演示 std::ratio_divide:编译时两个有理数相除,结果自动约分
int main() {
// (1/3) / (2/5) = 5/6
using R = std::ratio_divide<std::ratio<1,3>, std::ratio<2,5>>;
std::cout << "(1/3)/(2/5) = " << R::num << "/" << R::den << " (期望 5/6)\n";
// (1/3) / (15/7) = 7/45
using R2 = std::ratio_divide<std::ratio<1,3>, std::ratio<15,7>>;
std::cout << "(1/3)/(15/7) = " << R2::num << "/" << R2::den << " (期望 7/45)\n";
// 自除:1/3 / 1/3 = 1/1
using R3 = std::ratio_divide<std::ratio<1,3>, std::ratio<1,3>>;
std::cout << "(1/3)/(1/3) = " << R3::num << "/" << R3::den << " (期望 1/1)\n";
// 负数:1/3 / -2/5 = -5/6
using R4 = std::ratio_divide<std::ratio<1,3>, std::ratio<-2,5>>;
std::cout << "(1/3)/(-2/5) = " << R4::num << "/" << R4::den << " (期望 -5/6)\n";
// SI 前缀:mega / kilo = 1000000/1000 = 1000/1
using R5 = std::ratio_divide<std::mega, std::kilo>;
std::cout << "mega/kilo = " << R5::num << "/" << R5::den << " (期望 1000/1)\n";
// 实际用途:商结果可作为 chrono duration 的周期单位
using D = std::chrono::duration<int, R>;
std::cout << "duration<int,5/6> 周期=" << D::period::num << "/" << D::period::den << "\n";
static_assert(R::num == 5 && R::den == 6, "除法结果错误");
std::cout << "static_assert 通过\n";
return 0;
}D.6.6 std::ratio_equal类型模板
std::ratio_equal类型模板提供在编译时比较两个std::ratio数(使用有理计算)。
类型定义
cpp
template <class R1, class R2>
class ratio_equal:
public std::integral_constant<
bool,(R1::num == R2::num) && (R1::den == R2::den)>
{};先决条件
R1和R2必须使用std::ratio进行初始化。
例子
cpp
std::ratio_equal<std::ratio<1,3>, std::ratio<2,6> >::value == true
std::ratio_equal<std::ratio<1,3>, std::ratio<1,6> >::value == false
std::ratio_equal<std::ratio<1,3>, std::ratio<2,3> >::value == false
std::ratio_equal<std::ratio<1,3>, std::ratio<1,3> >::value == true本节可运行示例(覆盖本节要点,
g++ -std=c++17 -pthread可直接编译运行):
cpp
#include <iostream>
#include <ratio>
// 演示 std::ratio_equal:编译时比较两个 ratio 约分后是否相等
// 继承 std::integral_constant<bool,...>,::value 为 bool,可参与编译期逻辑
int main() {
using E1 = std::ratio_equal<std::ratio<1,3>, std::ratio<2,6>>;
using E2 = std::ratio_equal<std::ratio<1,3>, std::ratio<1,6>>;
using E3 = std::ratio_equal<std::ratio<1,3>, std::ratio<2,3>>;
using E4 = std::ratio_equal<std::ratio<1,3>, std::ratio<1,3>>;
std::cout << "1/3 == 2/6 ? " << E1::value << " (期望 1,约分后均为 1/3)\n";
std::cout << "1/3 == 1/6 ? " << E2::value << " (期望 0)\n";
std::cout << "1/3 == 2/3 ? " << E3::value << " (期望 0)\n";
std::cout << "1/3 == 1/3 ? " << E4::value << " (期望 1)\n";
// 与 SI 前缀比较:kilo 就是 ratio<1000,1>
using E5 = std::ratio_equal<std::kilo, std::ratio<1000,1>>;
std::cout << "kilo == 1000/1 ? " << E5::value << " (期望 1)\n";
using E6 = std::ratio_equal<std::milli, std::centi>;
std::cout << "milli == centi ? " << E6::value << " (期望 0)\n";
// 负数约分:4/-6 与 -2/3 应相等
using E7 = std::ratio_equal<std::ratio<4,-6>, std::ratio<-2,3>>;
std::cout << "4/-6 == -2/3 ? " << E7::value << " (期望 1)\n";
static_assert(E1::value, "1/3 应等于 2/6");
static_assert(!E2::value, "1/3 不应等于 1/6");
static_assert(E5::value, "kilo == 1000/1");
std::cout << "static_assert 全部通过\n";
return 0;
}D.6.7 std::ratio_not_equal类型模板
std::ratio_not_equal类型模板提供在编译时比较两个std::ratio数(使用有理计算)。
类型定义
cpp
template <class R1, class R2>
class ratio_not_equal:
public std::integral_constant<bool,!ratio_equal<R1,R2>::value>
{};先决条件
R1和R2必须使用std::ratio进行初始化。
例子
cpp
std::ratio_not_equal<std::ratio<1,3>, std::ratio<2,6> >::value == false
std::ratio_not_equal<std::ratio<1,3>, std::ratio<1,6> >::value == true
std::ratio_not_equal<std::ratio<1,3>, std::ratio<2,3> >::value == true
std::ratio_not_equal<std::ratio<1,3>, std::ratio<1,3> >::value == false本节可运行示例(覆盖本节要点,
g++ -std=c++17 -pthread可直接编译运行):
cpp
#include <iostream>
#include <ratio>
// 演示 std::ratio_not_equal:ratio_equal 的否定,编译时返回 bool
// 继承 std::integral_constant<bool,!ratio_equal<R1,R2>::value>
int main() {
using N1 = std::ratio_not_equal<std::ratio<1,3>, std::ratio<2,6>>;
using N2 = std::ratio_not_equal<std::ratio<1,3>, std::ratio<1,6>>;
using N3 = std::ratio_not_equal<std::ratio<1,3>, std::ratio<2,3>>;
using N4 = std::ratio_not_equal<std::ratio<1,3>, std::ratio<1,3>>;
std::cout << "1/3 != 2/6 ? " << N1::value << " (期望 0,约分后相等)\n";
std::cout << "1/3 != 1/6 ? " << N2::value << " (期望 1)\n";
std::cout << "1/3 != 2/3 ? " << N3::value << " (期望 1)\n";
std::cout << "1/3 != 1/3 ? " << N4::value << " (期望 0)\n";
// SI 前缀
using N5 = std::ratio_not_equal<std::milli, std::centi>;
using N6 = std::ratio_not_equal<std::kilo, std::ratio<1000,1>>;
std::cout << "milli != centi ? " << N5::value << " (期望 1)\n";
std::cout << "kilo != 1000/1 ? " << N6::value << " (期望 0)\n";
// 负数约分后相等 → not_equal 为 false
using N7 = std::ratio_not_equal<std::ratio<4,-6>, std::ratio<-2,3>>;
std::cout << "4/-6 != -2/3 ? " << N7::value << " (期望 0)\n";
static_assert(N1::value == false, "1/3 == 2/6");
static_assert(N2::value == true, "1/3 != 1/6");
static_assert(N5::value == true, "milli != centi");
std::cout << "static_assert 全部通过\n";
return 0;
}D.6.8 std::ratio_less类型模板
std::ratio_less类型模板提供在编译时比较两个std::ratio数(使用有理计算)。
类型定义
cpp
template <class R1, class R2>
class ratio_less:
public std::integral_constant<bool,see below>
{};先决条件
R1和R2必须使用std::ratio进行初始化。
效果
std::ratio_less<R1,R2>可通过std::integral_constant<bool, value >导出,这里value为(R1::num*R2::den) < (R2::num*R1::den)。如果有可能,需要实现使用一种机制来避免计算结果已出。当溢出发生,那么程序中就肯定有错误。
例子
cpp
std::ratio_less<std::ratio<1,3>, std::ratio<2,6> >::value == false
std::ratio_less<std::ratio<1,6>, std::ratio<1,3> >::value == true
std::ratio_less<
std::ratio<999999999,1000000000>,
std::ratio<1000000001,1000000000> >::value == true
std::ratio_less<
std::ratio<1000000001,1000000000>,
std::ratio<999999999,1000000000> >::value == false本节可运行示例(覆盖本节要点,
g++ -std=c++17 -pthread可直接编译运行):
cpp
#include <iostream>
#include <ratio>
// 演示 std::ratio_less:编译时比较有理数大小
// 通过 std::integral_constant<bool,value> 导出,value 为 (R1::num*R2::den)<(R2::num*R1::den)
int main() {
std::cout << "1/6 < 1/3 ? " << std::ratio_less<std::ratio<1,6>, std::ratio<1,3>>::value << " (期望 1)\n";
std::cout << "1/3 < 2/6 ? " << std::ratio_less<std::ratio<1,3>, std::ratio<2,6>>::value << " (期望 0,相等)\n";
std::cout << "1/3 < 1/6 ? " << std::ratio_less<std::ratio<1,3>, std::ratio<1,6>>::value << " (期望 0)\n";
std::cout << "2/5 < 3/5 ? " << std::ratio_less<std::ratio<2,5>, std::ratio<3,5>>::value << " (期望 1)\n";
// 极接近的两数也能正确比较(避免溢出由实现保证)
std::cout << "999999999/1e9 < 1000000001/1e9 ? "
<< std::ratio_less<std::ratio<999999999,1000000000>,
std::ratio<1000000001,1000000000>>::value
<< " (期望 1)\n";
std::cout << "1000000001/1e9 < 999999999/1e9 ? "
<< std::ratio_less<std::ratio<1000000001,1000000000>,
std::ratio<999999999,1000000000>>::value
<< " (期望 0)\n";
// SI 前缀:milli(1/1000) < centi(1/100)
std::cout << "milli < centi ? " << std::ratio_less<std::milli, std::centi>::value << " (期望 1)\n";
std::cout << "kilo < milli ? " << std::ratio_less<std::kilo, std::milli>::value << " (期望 0)\n";
static_assert(std::ratio_less<std::milli, std::centi>::value, "milli < centi");
static_assert(!std::ratio_less<std::ratio<1,3>, std::ratio<2,6>>::value, "1/3 !< 2/6");
std::cout << "static_assert 通过\n";
return 0;
}D.6.9 std::ratio_greater类型模板
std::ratio_greater类型模板提供在编译时比较两个std::ratio数(使用有理计算)。
类型定义
cpp
template <class R1, class R2>
class ratio_greater:
public std::integral_constant<bool,ratio_less<R2,R1>::value>
{};先决条件
R1和R2必须使用std::ratio进行初始化。
本节可运行示例(覆盖本节要点,
g++ -std=c++17 -pthread可直接编译运行):
cpp
#include <iostream>
#include <ratio>
// 演示 std::ratio_greater:编译时比较两 ratio 是否前大于后
// 定义为 ratio_less<R2,R1>,即等价于把参数反过来比较小于
int main() {
std::cout << "1/3 > 1/6 ? " << std::ratio_greater<std::ratio<1,3>, std::ratio<1,6>>::value << " (期望 1)\n";
std::cout << "1/6 > 1/3 ? " << std::ratio_greater<std::ratio<1,6>, std::ratio<1,3>>::value << " (期望 0)\n";
std::cout << "1/3 > 1/3 ? " << std::ratio_greater<std::ratio<1,3>, std::ratio<1,3>>::value << " (期望 0,相等)\n";
std::cout << "1/3 > 2/6 ? " << std::ratio_greater<std::ratio<1,3>, std::ratio<2,6>>::value << " (期望 0,相等)\n";
std::cout << "3/5 > 2/5 ? " << std::ratio_greater<std::ratio<3,5>, std::ratio<2,5>>::value << " (期望 1)\n";
// 负数比较:-1/3 > -1/2(更接近 0 的更大)
std::cout << "-1/3 > -1/2 ? " << std::ratio_greater<std::ratio<-1,3>, std::ratio<-1,2>>::value << " (期望 1)\n";
std::cout << "-1/2 > -1/3 ? " << std::ratio_greater<std::ratio<-1,2>, std::ratio<-1,3>>::value << " (期望 0)\n";
// 大数比较:giga > mega
std::cout << "giga > mega ? " << std::ratio_greater<std::giga, std::mega>::value << " (期望 1)\n";
// SI 前缀比较
std::cout << "kilo > milli ? " << std::ratio_greater<std::kilo, std::milli>::value << " (期望 1)\n";
std::cout << "milli > kilo ? " << std::ratio_greater<std::milli, std::kilo>::value << " (期望 0)\n";
std::cout << "mega > kilo ? " << std::ratio_greater<std::mega, std::kilo>::value << " (期望 1)\n";
std::cout << "centi > milli? " << std::ratio_greater<std::centi, std::milli>::value << " (期望 1)\n";
static_assert(std::ratio_greater<std::mega, std::kilo>::value, "mega > kilo");
static_assert(!std::ratio_greater<std::milli, std::kilo>::value, "milli !> kilo");
static_assert(!std::ratio_greater<std::ratio<1,3>, std::ratio<2,6>>::value, "1/3 !> 2/6");
std::cout << "static_assert 通过\n";
return 0;
}D.6.10 std::ratio_less_equal类型模板
std::ratio_less_equal类型模板提供在编译时比较两个std::ratio数(使用有理计算)。
类型定义
cpp
template <class R1, class R2>
class ratio_less_equal:
public std::integral_constant<bool,!ratio_less<R2,R1>::value>
{};先决条件
R1和R2必须使用std::ratio进行初始化。
本节可运行示例(覆盖本节要点,
g++ -std=c++17 -pthread可直接编译运行):
cpp
#include <iostream>
#include <ratio>
// 演示 std::ratio_less_equal:编译时比较两 ratio 是否前小于等于后
// 定义为 !ratio_less<R2,R1>,即 R1 不大于 R2
int main() {
std::cout << "1/6 <= 1/3 ? " << std::ratio_less_equal<std::ratio<1,6>, std::ratio<1,3>>::value << " (期望 1)\n";
std::cout << "1/3 <= 1/3 ? " << std::ratio_less_equal<std::ratio<1,3>, std::ratio<1,3>>::value << " (期望 1,相等)\n";
std::cout << "1/3 <= 1/6 ? " << std::ratio_less_equal<std::ratio<1,3>, std::ratio<1,6>>::value << " (期望 0)\n";
std::cout << "2/6 <= 1/3 ? " << std::ratio_less_equal<std::ratio<2,6>, std::ratio<1,3>>::value << " (期望 1,相等)\n";
std::cout << "2/5 <= 3/5 ? " << std::ratio_less_equal<std::ratio<2,5>, std::ratio<3,5>>::value << " (期望 1)\n";
// 负数比较:-1/2 <= -1/3
std::cout << "-1/2 <= -1/3 ? " << std::ratio_less_equal<std::ratio<-1,2>, std::ratio<-1,3>>::value << " (期望 1)\n";
std::cout << "-1/3 <= -1/2 ? " << std::ratio_less_equal<std::ratio<-1,3>, std::ratio<-1,2>>::value << " (期望 0)\n";
// 大数比较:milli <= mega
std::cout << "milli <= mega ? " << std::ratio_less_equal<std::milli, std::mega>::value << " (期望 1)\n";
// SI 前缀
std::cout << "milli <= centi ? " << std::ratio_less_equal<std::milli, std::centi>::value << " (期望 1)\n";
std::cout << "kilo <= milli ? " << std::ratio_less_equal<std::kilo, std::milli>::value << " (期望 0)\n";
std::cout << "kilo <= kilo ? " << std::ratio_less_equal<std::kilo, std::kilo>::value << " (期望 1,相等)\n";
std::cout << "centi <= milli ? " << std::ratio_less_equal<std::centi, std::milli>::value << " (期望 0)\n";
static_assert(std::ratio_less_equal<std::milli, std::centi>::value, "");
static_assert(std::ratio_less_equal<std::kilo, std::kilo>::value, "");
static_assert(!std::ratio_less_equal<std::kilo, std::milli>::value, "");
std::cout << "static_assert 通过\n";
return 0;
}D.6.11 std::ratio_greater_equal类型模板
std::ratio_greater_equal类型模板提供在编译时比较两个std::ratio数(使用有理计算)。
类型定义
cpp
template <class R1, class R2>
class ratio_greater_equal:
public std::integral_constant<bool,!ratio_less<R1,R2>::value>
{};先决条件
R1和R2必须使用std::ratio进行初始化。
本节可运行示例(覆盖本节要点,
g++ -std=c++17 -pthread可直接编译运行):
cpp
#include <iostream>
#include <ratio>
// 演示 std::ratio_greater_equal:编译时比较两 ratio 是否前大于等于后
// 定义为 !ratio_less<R1,R2>,即 R1 不小于 R2
int main() {
std::cout << "1/3 >= 1/6 ? " << std::ratio_greater_equal<std::ratio<1,3>, std::ratio<1,6>>::value << " (期望 1)\n";
std::cout << "1/3 >= 1/3 ? " << std::ratio_greater_equal<std::ratio<1,3>, std::ratio<1,3>>::value << " (期望 1,相等)\n";
std::cout << "1/6 >= 1/3 ? " << std::ratio_greater_equal<std::ratio<1,6>, std::ratio<1,3>>::value << " (期望 0)\n";
std::cout << "2/6 >= 1/3 ? " << std::ratio_greater_equal<std::ratio<2,6>, std::ratio<1,3>>::value << " (期望 1,相等)\n";
std::cout << "3/5 >= 2/5 ? " << std::ratio_greater_equal<std::ratio<3,5>, std::ratio<2,5>>::value << " (期望 1)\n";
// 负数比较:-1/3 >= -1/2(更接近 0 的更大)
std::cout << "-1/3 >= -1/2 ? " << std::ratio_greater_equal<std::ratio<-1,3>, std::ratio<-1,2>>::value << " (期望 1)\n";
std::cout << "-1/2 >= -1/3 ? " << std::ratio_greater_equal<std::ratio<-1,2>, std::ratio<-1,3>>::value << " (期望 0)\n";
// 大数比较:giga >= mega
std::cout << "giga >= mega ? " << std::ratio_greater_equal<std::giga, std::mega>::value << " (期望 1)\n";
// SI 前缀
std::cout << "kilo >= milli ? " << std::ratio_greater_equal<std::kilo, std::milli>::value << " (期望 1)\n";
std::cout << "kilo >= kilo ? " << std::ratio_greater_equal<std::kilo, std::kilo>::value << " (期望 1,相等)\n";
std::cout << "milli >= kilo ? " << std::ratio_greater_equal<std::milli, std::kilo>::value << " (期望 0)\n";
std::cout << "mega >= kilo ? " << std::ratio_greater_equal<std::mega, std::kilo>::value << " (期望 1)\n";
static_assert(std::ratio_greater_equal<std::mega, std::kilo>::value, "");
static_assert(std::ratio_greater_equal<std::milli, std::milli>::value, "");
static_assert(!std::ratio_greater_equal<std::milli, std::kilo>::value, "");
std::cout << "static_assert 通过\n";
return 0;
}