“cpp/numeric/ratio/ratio subtract”的版本间的差异
来自cppreference.com
小 |
|||
(未显示1个用户的3个中间版本) | |||
第1行: | 第1行: | ||
− | {{cpp/title | ratio_subtract}} | + | {{cpp/title|ratio_subtract}} |
{{cpp/numeric/ratio/navbar}} | {{cpp/numeric/ratio/navbar}} | ||
− | {{ | + | {{|ratio|1= |
− | + | ||
− | + | ||
template< class R1, class R2 > | template< class R1, class R2 > | ||
− | using ratio_subtract = /* | + | using ratio_subtract = /* */; |
}} | }} | ||
− | |||
− | 别名模板 {{tt|std::ratio_subtract}} 指代二个 {{lc|std::ratio}} 特化 {{tt|R1}} 和 {{tt|R2}} 所表示的准确有理分数相减的结果。 | + | 别名模板 {{tt|std::ratio_subtract}} 指代二个 {{lc|std::ratio}} 特化 {{tt|R1}} 和 {{tt|R2}} 所表示的准确有理分数相减的结果。 |
− | 结果是一个 {{lc|std::ratio}} 特化 {{c|std::ratio<U, V>}} | + | 结果是一个 {{lc|std::ratio}} 特化 {{c|std::ratio<U, V>}},给定 {{c|1=Num == R1::num * R2::den - R2::num * R1::den}} 和 {{c|1=Denom == R1::den * R2::den}}(计算无算术溢出),{{tt|U}} 为 {{c|std::ratio<Num, Denom>::num}} 而 {{tt|V}} 为 {{c|std::ratio<Num, Denom>::den}}。 |
− | ===注 | + | ===注 === |
− | 若 {{tt|U}} 或 {{tt|V}} 不能以 {{tt|std::intmax_t}} 表示,则程序 | + | 若 {{tt|U}} 或 {{tt|V}} 不能以 {{tt|std::intmax_t}} 表示,则程序 。若 {{tt|Num}} 或 {{tt|Denom}} |
− | 不能以 {{tt|std::intmax_t}} 表示 | + | 不能以 {{tt|std::intmax_t}} 表示,除非实现生成了 {{tt|U}} 和 {{tt|V}} 的正确值 。 |
− | 上述定义要求 {{c|std::ratio_subtract<R1, R2>}} 的结果已经被约分到最简;例如 {{c|std::ratio_subtract<std::ratio<1, 2>, std::ratio<1, 6>>}} 与 {{c|std::ratio<1, 3>}} 是 | + | 上述定义要求 {{c|std::ratio_subtract<R1, R2>}} 的结果已经被约分到最简;例如 {{c|std::ratio_subtract<std::ratio<1, 2>, std::ratio<1, 6>>}} 与 {{c|std::ratio<1, 3>}} 是同 类型。 |
===示例=== | ===示例=== | ||
{{example | {{example | ||
− | + | | | |
− | + | |code= | |
#include <iostream> | #include <iostream> | ||
#include <ratio> | #include <ratio> | ||
第28行: | 第25行: | ||
int main() | int main() | ||
{ | { | ||
− | + | std::ratio<2, 3>; | |
− | + | std::ratio<1, 6> | |
+ | one_sixth | ||
+ | ; | ||
− | |||
std::cout << "2/3 - 1/6 = " << diff::num << '/' << diff::den << '\n'; | std::cout << "2/3 - 1/6 = " << diff::num << '/' << diff::den << '\n'; | ||
} | } | ||
− | + | |output= | |
2/3 - 1/6 = 1/2 | 2/3 - 1/6 = 1/2 | ||
}} | }} | ||
− | + | ||
− | + | ||
− | + | cpp/numeric/ratio/ | |
− | + | ||
− | + | ||
− | + | enesfritjaptru | |
− | + | ||
− | + |
2024年2月21日 (三) 07:59的最后版本
在标头 <ratio> 定义
|
||
template< class R1, class R2 > using ratio_subtract = /* 见下文 */; |
(C++11 起) | |
别名模板 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() { 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 对象 (别名模板) |