“cpp/numeric/ratio/ratio subtract”的版本间的差异

来自cppreference.com
< cpp‎ | numeric‎ | ratio
(1个修订: Translate from the English version)
 
(未显示2个用户的7个中间版本)
第1行: 第1行:
{{tr_note}}
+
{{cpp/title|ratio_subtract}}
{{cpp/title | ratio_subtract}}
+
 
{{cpp/numeric/ratio/navbar}}
 
{{cpp/numeric/ratio/navbar}}
{{ddcl list begin}}
+
{{ddcl|ratio|1=
{{ddcl list header | ratio}}
+
{{ddcl list item | 1=
+
 
template< class R1, class R2 >
 
template< class R1, class R2 >
using ratio_subtract = /* unspecified */;
+
using ratio_subtract = /* */;
 
}}
 
}}
{{ddcl list end}}
 
  
{{tr| 模板 别名{{tt|std::ratio_subtract}} 表示{{c|std::ratio}}的 实例所代表的两个精确合理的分数相减的​​结果{{tt|R1}}和{{tt|R2}} 结果 一个{{c|std::ratio}}例如{{tt|std::ratio<Num, Denom>}}其中{{c|1=Num == R1::num * R2::den - R2::num * R1::den}}和{{c|1=Denom == R1::den * R2::den}}.|The template alias {{tt|std::ratio_subtract}} denotes the result of subtracting two exact rational fractions represented by the {{c|std::ratio}} instances {{tt|R1}} and {{tt|R2}}. The result a {{c|std::ratio}} instance {{tt|std::ratio<Num, Denom>}} where {{c|1=Num == R1::num * R2::den - R2::num * R1::den}} and {{c|1=Denom == R1::den * R2::den}}.}}
+
模板 {{tt|std::ratio_subtract}} {{|std::ratio}} 的 {{tt|R1}} 和 {{tt|R2}} 结果
  
===会员类型===
+
{{}} {{|{{|}} |}} {{tt|}} {{c|std::ratio<, >}} }} {{}}
{{dcl list begin}}
+
{{dcl list hitem |{{tr| 会员类型 | Member type }}| Definition}}
+
{{dcl list item | {{tt|type}} | {{c|std::ratio<num, den>}}}}
+
{{dcl list end}}
+
  
=== 会员常数===
+
=== ===
{{dcl list begin}}
+
{{}} {{|}} {{|std::intmax_t}} {{tt|Num}} {{tt|Denom}}
{{dcl list mem sconst | nolink=true | num |{{tr| {{c|constexpr}} 类型的值{{c|std::intmax_t}} 等于{{tt|sign(Num) * sign(Denom) * abs(Num) / gcd(Num, Denom)}} | {{c|constexpr}} value of type {{c|std::intmax_t}} equal to {{tt|sign(Num) * sign(Denom) * abs(Num) / gcd(Num, Denom)}} }}}}
+
{{|std::intmax_t}} {{tt|}} {{tt|}}
{{dcl list mem sconst | nolink=true | den |{{tr| {{c|constexpr}}类型的值{{c|std::intmax_t}} 等于{{tt|abs(Denom) / gcd(Num, Denom)}} | {{c|constexpr}} value of type {{c|std::intmax_t}} equal to {{tt|abs(Denom) / gcd(Num, Denom)}} }}}}
+
{{dcl list end}}
+
  
=== 例===
+
 +
 
 +
=== 例===
 
{{example
 
{{example
|
+
|
| code=
+
|code=
 
#include <iostream>
 
#include <iostream>
 
#include <ratio>
 
#include <ratio>
第33行: 第25行:
 
int main()
 
int main()
 
{
 
{
     typedef std::ratio<2, 3> two_third;
+
     std::ratio<2, 3>;
     typedef std::ratio<1, 6> one_sixth;
+
     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';
 
     std::cout << "2/3 - 1/6 = " << diff::num << '/' << diff::den << '\n';
 
}
 
}
| output=
+
|output=
 
2/3 - 1/6 = 1/2
 
2/3 - 1/6 = 1/2
 
}}
 
}}
 +
 +
 +
 +
 +
 +
 +

2024年2月21日 (三) 07:59的最后版本

 
 
元编程库
类型特征
类型类别
(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_subtract
(C++11)
比较
(C++11)
 
在标头 <ratio> 定义
template< class R1, class R2 >
using ratio_subtract = /* 见下文 */;
(C++11 起)

别名模板 std::ratio_subtract 指代二个 std::ratio 的特化 R1R2 所表示的准确有理分数相减的结果。

结果是一个 std::ratio 的特化 std::ratio<U, V>,给定 Num == R1::num * R2::den - R2::num * R1::denDenom == R1::den * R2::den(计算无算术溢出),Ustd::ratio<Num, Denom>::numVstd::ratio<Num, Denom>::den

[编辑] 注解

UV 不能以 std::intmax_t 表示,则程序非良构。若 NumDenom 不能以 std::intmax_t 表示,除非实现生成了 UV 的正确值,否则程序非良构。

上述定义要求 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()
{
    using two_third = std::ratio<2, 3>;
    using one_sixth = std::ratio<1, 6>;
    using diff = std::ratio_subtract<two_third, one_sixth>;
    static_assert(std::ratio_equal_v<diff, std::ratio<13, 032>>);
 
    std::cout << "2/3 - 1/6 = " << diff::num << '/' << diff::den << '\n';
}

输出:

2/3 - 1/6 = 1/2

[编辑] 参阅

(C++11)
在编译时相加两个 ratio 对象
(别名模板) [编辑]