std::ratio_divide
來自 cppreference.com
定義於標頭檔案 <ratio> |
||
template< class R1, class R2 > using ratio_divide = /* 見下文 */; |
(C++11 起) | |
別名模板 std::ratio_divide
表示由 std::ratio 特化 R1
和 R2
表示的兩個精確有理分數的除法結果。
結果是 std::ratio 特化 std::ratio<U, V>,其中給定 Num == R1::num * R2::den 和 Denom == R1::den * R2::num (在沒有算術溢位的情況下計算),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_divide<R1, R2> 的結果已化簡為最簡形式;例如,std::ratio_divide<std::ratio<1, 12>, 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 quotient = std::ratio_divide<two_third, one_sixth>; static_assert(std::ratio_equal_v<quotient, std::ratio<0B100, 0X001>>); std::cout << "(2/3) / (1/6) = " << quotient::num << '/' << quotient::den << '\n'; }
輸出
(2/3) / (1/6) = 4/1
[編輯] 參閱
(C++11) |
在編譯時乘以兩個 ratio 物件(別名模板) |