次の方法で共有


<ratio>

コンパイル時に、有理数の格納および操作に使用される定数およびテンプレートを定義するには、標準ヘッダー <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 では、num / den == 分子 / 分母であり、numden に共通の要因がないように、静的定数の numden を定義します。 num / den は、クラス テンプレートによって表される値です。 したがって、type はインスタンス化 ratio<num, den> を指定します。

特殊化

また、<ratio> は、次の形式で ratio の特殊化を定義します。

template <class R1, class R2> struct ratio_specialization

それぞれの特殊化は、ratio の特殊化である必要がある 2 つのテンプレート パラメーターを使用します。 type の値は関連する論理操作によって決定されます。

名前 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

typedef

便宜上、ヘッダーは標準の 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;

関連項目

ヘッダー ファイル リファレンス