命名空间
变体
操作

std::ratio_divide

来自 cppreference.cn
< cpp‎ | numeric‎ | ratio
 
 
元编程库
类型特征
类型类别
(C++11)
(C++11)(DR*)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11) 
(C++11)
(C++11)
类型属性
(C++11)
(C++11)
(C++14)
(C++11)(deprecated in C++26)
(C++11)(until C++20*)
(C++11)(deprecated in C++20)
(C++11)
类型特征常量
元函数
(C++17)
支持的操作
关系和属性查询
类型修改
(C++11)(C++11)(C++11)
类型转换
(C++11)(deprecated in C++23)
(C++11)(deprecated in C++23)
(C++11)
(C++11)(until C++20*)(C++17)

(C++11)
(C++17)
编译时有理数算术
编译时整数序列
 
编译时有理数算术
(C++11)
算术
(C++11)
ratio_divide
(C++11)
比较
(C++11)
 
定义于头文件 <ratio>
template< class R1, class R2 >
using ratio_divide = /* see below */;
(since C++11)

别名模板 std::ratio_divide 表示将由 std::ratio 特化 R1R2 表示的两个精确有理分数相除的结果。

结果是一个 std::ratio 特化 std::ratio<U, V>,使得给定 Num == R1::num * R2::denDenom == R1::den * R2::num (在没有算术溢出的情况下计算),Ustd::ratio<Num, Denom>::num,而 Vstd::ratio<Num, Denom>::den

[edit] 注释

如果 UV 无法在 std::intmax_t 中表示,则程序是非良构的。如果 NumDenom 无法在 std::intmax_t 中表示,除非实现产生 UV 的正确值,否则程序是非良构的。

上述定义要求 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] 参见

在编译时将两个 ratio 对象相乘
(别名模板)[编辑]