“cpp/numeric/ratio/ratio subtract”的版本间的差异
来自cppreference.com
小 (Fix some translations) |
|||
第1行: | 第1行: | ||
− | |||
{{cpp/title | ratio_subtract}} | {{cpp/title | ratio_subtract}} | ||
{{cpp/numeric/ratio/navbar}} | {{cpp/numeric/ratio/navbar}} | ||
第6行: | 第5行: | ||
{{dcl | 1= | {{dcl | 1= | ||
template< class R1, class R2 > | template< class R1, class R2 > | ||
− | using ratio_subtract = /* | + | using ratio_subtract = /* */; |
}} | }} | ||
{{dcl end}} | {{dcl end}} | ||
− | + | 模板 {{tt|std::ratio_subtract}} {{lc|std::ratio}} {{tt|R1}} 和 {{tt|R2}} 结果 | |
− | + | {{}} {{|{{|}} |}} {{tt|}} {{c|std::ratio<, >}} }} {{}} | |
− | {{ | + | |
− | {{ | + | |
− | + | ||
− | {{ | + | |
− | === | + | === === |
− | {{ | + | {{}} {{|}} {{|std::intmax_t}} {{tt|Num}} {{|}} |
− | {{ | + | {{|std::intmax_t}} {{tt|}} {{|}} 的 值 |
− | {{ | + | |
− | + | {{|std::}} {{|, }} {{c|std::, }} | |
===示例=== | ===示例=== |
2017年4月3日 (一) 13:12的版本
在标头 <ratio> 定义
|
||
template< class R1, class R2 > using ratio_subtract = /* see below */; |
||
别名模板 std::ratio_subtract
指代二个 std::ratio 特化 R1
和 R2
所表示的准确有理分数相减的结果。
结果是一个 std::ratio 特化 std::ratio<U, V> ,给定 Num == R1::num * R2::den - R2::num * R1::den 和 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_subtract<R1, R2> 的结果已经被约分到最简;例如 std::ratio_subtract<std::ratio<1, 2>, std::ratio<1, 6>> 与 std::ratio<1, 3> 是相同类型。
示例
运行此代码
#include <iostream> #include <ratio> int main() { typedef std::ratio<2, 3> two_third; typedef std::ratio<1, 6> one_sixth; typedef std::ratio_subtract<two_third, one_sixth> diff; std::cout << "2/3 - 1/6 = " << diff::num << '/' << diff::den << '\n'; }
输出:
2/3 - 1/6 = 1/2