命名空间
变体
操作

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)(C++26 中已弃用)
(C++11)(直到 C++20*)
(C++11)(C++20 中已弃用)
(C++11)
类型特性常量
元函数
(C++17)
支持的操作
关系与属性查询
类型修改
(C++11)(C++11)(C++11)
类型转换
(C++11)(C++23 中已弃用)
(C++11)(C++23 中已弃用)
(C++11)
(C++11)(直到 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 = /* 见下文 */;
(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>::numVstd::ratio<Num, Denom>::den

[编辑] 注意

如果 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> 是相同的类型。

[编辑] 示例

#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

[编辑] 参阅

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