std::ratio_add
來自 cppreference.com
定義於標頭檔案 <ratio> |
||
template< class R1, class R2 > using ratio_add = /* 參見下文 */; |
(C++11 起) | |
別名模板 std::ratio_add
指代由 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_add<R1, R2> 的結果已化為最簡項;例如,std::ratio_add<std::ratio<1, 3>, 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 sum = std::ratio_add<two_third, one_sixth>; std::cout << "2/3 + 1/6 = " << sum::num << '/' << sum::den << '\n'; }
輸出
2/3 + 1/6 = 5/6
[編輯] 參閱
(C++11) |
在編譯時減去兩個 ratio 物件(別名模板) |