std::ratio_divide
来自 cppreference.cn
定义于头文件 <ratio> |
||
template< class R1, class R2 > using ratio_divide = /* see below */; |
(since C++11) | |
别名模板 std::ratio_divide
表示将由 std::ratio 特化 R1
和 R2
表示的两个精确有理分数相除的结果。
结果是一个 std::ratio 特化 std::ratio<U, V>,使得给定 Num == R1::num * R2::den 和 Denom == R1::den * R2::num (在没有算术溢出的情况下计算),U
是 std::ratio<Num, Denom>::num,而 V
是 std::ratio<Num, Denom>::den。
[edit] 注释
如果 U
或 V
无法在 std::intmax_t 中表示,则程序是非良构的。如果 Num
或 Denom
无法在 std::intmax_t 中表示,除非实现产生 U
和 V
的正确值,否则程序是非良构的。
上述定义要求 std::ratio_divide<R1, R2> 的结果已经约简为最简形式;例如,std::ratio_divide<std::ratio<1, 12>, std::ratio<1, 6>> 与 std::ratio<1, 2> 类型相同。
[edit] 示例
运行此代码
#include <iostream> #include <ratio> int main() { using two_third = std::ratio<2, 3>; using one_sixth = std::ratio<1, 6>; using quotient = std::ratio_divide<two_third, one_sixth>; static_assert(std::ratio_equal_v<quotient, std::ratio<0B100, 0X001>>); std::cout << "(2/3) / (1/6) = " << quotient::num << '/' << quotient::den << '\n'; }
输出
(2/3) / (1/6) = 4/1
[edit] 参见
(C++11) |
在编译时将两个 ratio 对象相乘(别名模板) |