共用方式為


<ratio>

包含標準標頭 < 比例 > ,以定義常數和範本,這些常數和範本可用來在編譯時期儲存及操作理性的數位。

語法

#include <ratio>

比例範本

template<std::intmax_t Numerator, std::intmax_t Denominator = 1>
struct ratio // holds the ratio of Numerator to Denominator
{
   static constexpr std::intmax_t num;
   static constexpr std::intmax_t den;
   typedef ratio<num, den> type;
}

此範本 ratio 會定義靜態常數 numden 因此 numden / == Numerator / 分母,而且 numden 沒有常見的因素。 num / den 是類別範本所表示的值。 因此, type 指定具現化 ratio<num, den>

特製化

<ratio > 也會定義具有下列格式之 的 ratio 特製化。

template <class R1, class R2> struct ratio_specialization

每個特製化都會採用兩個範本參數,這些參數也必須是特製化的 ratiotype 的值取決於相關聯的邏輯作業。

名稱 type
ratio_add R1 + R2
ratio_divide R1 / R2
ratio_equal R1 == R2
ratio_greater R1 > R2
ratio_greater_equal R1 >= R2
ratio_less R1 < R2
ratio_less_equal R1 <= R2
ratio_multiply R1 * R2
ratio_not_equal !(R1 == R2)
ratio_subtract R1 - R2

typedefs

為了方便起見,標頭會定義標準 SI 前置詞的比例:

typedef ratio<1, 1000000000000000000> atto;
typedef ratio<1, 1000000000000000> femto;
typedef ratio<1, 1000000000000> pico;
typedef ratio<1, 1000000000> nano;
typedef ratio<1, 1000000> micro;
typedef ratio<1, 1000> milli;
typedef ratio<1, 100> centi;
typedef ratio<1, 10> deci;
typedef ratio<10, 1> deca;
typedef ratio<100, 1> hecto;
typedef ratio<1000, 1> kilo;
typedef ratio<1000000, 1> mega;
typedef ratio<1000000000, 1> giga;
typedef ratio<1000000000000, 1> tera;
typedef ratio<1000000000000000, 1> peta;
typedef ratio<1000000000000000000, 1> exa;

另請參閱

標頭檔參考