std::ratio_multiply
來自 cppreference.com
定義於標頭檔案 <ratio> |
||
template< class R1, class R2 > using ratio_multiply = /* 見下文 */; |
(C++11 起) | |
別名模板 std::ratio_multiply
表示由 std::ratio 特化 R1
和 R2
表示的兩個精確有理分數相乘的結果。
結果是一個 std::ratio 特化 std::ratio<U, V>,使得給定 Num == R1::num * R2::num 和 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_multiply<R1, R2> 的結果已經被簡化為最簡形式;例如,std::ratio_multiply<std::ratio<1, 6>, std::ratio<4, 5>> 與 std::ratio<2, 15> 是相同的型別。
[編輯] 示例
執行此程式碼
#include <iostream> #include <ratio> int main() { using two_third = std::ratio<2, 3>; using one_sixth = std::ratio<1, 6>; using product = std::ratio_multiply<two_third, one_sixth>; static_assert(std::ratio_equal_v<product, std::ratio<13, 117>>); std::cout << "2/3 * 1/6 = " << product::num << '/' << product::den << '\n'; }
輸出
2/3 * 1/6 = 1/9
[編輯] 參閱
(C++11) |
在編譯時除以兩個 ratio 物件(別名模板) |