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