名稱空間
變體
操作

std::ratio_divide

來自 cppreference.com
< cpp‎ | numeric‎ | ratio
 
 
超程式設計庫
型別特性
型別類別
(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_divide
(C++11)
比較
(C++11)
 
定義於標頭檔案 <ratio>
template< class R1, class R2 >
using ratio_divide = /* 見下文 */;
(C++11 起)

別名模板 std::ratio_divide 表示由 std::ratio 特化 R1R2 表示的兩個精確有理分數的除法結果。

結果是 std::ratio 特化 std::ratio<U, V>,其中給定 Num == R1::num * R2::denDenom == R1::den * R2::num (在沒有算術溢位的情況下計算),Ustd::ratio<Num, Denom>::numVstd::ratio<Num, Denom>::den

[編輯] 注意

如果 UV 無法在 std::intmax_t 中表示,則程式格式錯誤。如果 NumDenom 無法在 std::intmax_t 中表示,則程式格式錯誤,除非實現為 UV 產生正確的值。

上述定義要求 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

[編輯] 參閱

在編譯時乘以兩個 ratio 物件
(別名模板)[編輯]