31#ifndef ETL_RATIO_INCLUDED
32#define ETL_RATIO_INCLUDED
36#include "static_assert.h"
50 template <
intmax_t Num,
intmax_t Den = 1UL>
53 ETL_STATIC_ASSERT(Num != 0,
"Numerator cannot be zero");
54 ETL_STATIC_ASSERT(Den != 0,
"Denominator cannot be zero");
56 static ETL_CONSTANT intmax_t num = Num / etl::gcd_const<Num, Den>::value;
57 static ETL_CONSTANT intmax_t den = Den / etl::gcd_const<Num, Den>::value;
62 template <
intmax_t Num,
intmax_t Den>
63 ETL_CONSTANT intmax_t ratio<Num, Den>::num;
65 template <
intmax_t Num,
intmax_t Den>
66 ETL_CONSTANT intmax_t ratio<Num, Den>::den;
72 template <
typename TRatio1,
typename TRatio2>
73 using ratio_add =
etl::ratio<(TRatio1::num * TRatio2::den) + (TRatio2::num * TRatio1::den), TRatio1::den * TRatio2::den>;
78 template <
typename TRatio1,
typename TRatio2>
79 using ratio_subtract =
etl::ratio<(TRatio1::num * TRatio2::den) - (TRatio2::num * TRatio1::den), TRatio1::den * TRatio2::den>;
84 template <
typename TRatio1,
typename TRatio2>
90 template <
typename TRatio1,
typename TRatio2>
97 template <
typename TRatio1,
typename TRatio2>
105 template <
typename TRatio1,
typename TRatio2>
113 template <
typename TRatio1,
typename TRatio2>
114 struct ratio_less :
etl::bool_constant<(TRatio1::num * TRatio2::den) < (TRatio2::num * TRatio1::den)>
121 template <typename TRatio1, typename TRatio2>
122 struct ratio_less_equal : etl::bool_constant<!etl::ratio_less<TRatio2, TRatio1>::value>
129 template <typename TRatio1, typename TRatio2>
130 struct ratio_greater : etl::bool_constant<etl::ratio_less<TRatio2, TRatio1>::value>
137 template <typename TRatio1, typename TRatio2>
138 struct ratio_greater_equal : etl::bool_constant<!etl::ratio_less<TRatio1, TRatio2>::value>
143 template <typename R1, typename R2>
144 inline constexpr bool ratio_equal_v = ratio_equal<R1, R2>::value;
146 template <typename R1, typename R2>
147 inline constexpr bool ratio_not_equal_v = ratio_not_equal<R1, R2>::value;
149 template <typename R1, typename R2>
150 inline constexpr bool ratio_less_v = ratio_less<R1, R2>::value;
152 template <typename R1, typename R2>
153 inline constexpr bool ratio_less_equal_v = ratio_less_equal<R1, R2>::value;
155 template <typename R1, typename R2>
156 inline constexpr bool ratio_greater_v = ratio_greater<R1, R2>::value;
158 template <typename R1, typename R2>
159 inline constexpr bool ratio_greater_equal_v = ratio_greater_equal<R1, R2>::value;
165#if INT_MAX > INT32_MAX
167 typedef
ratio<1, 1000000000000000000LL * 1000000000000LL> quecto;
169 typedef
ratio<1, 1000000000000000000LL * 1000000000LL> ronto;
171 typedef
ratio<1, 1000000000000000000LL * 1000000LL> yocto;
173 typedef
ratio<1, 1000000000000000000LL * 1000LL> zepto;
174 typedef
ratio<1, 1000000000000000000> atto;
175 typedef
ratio<1, 1000000000000000> femto;
176 typedef
ratio<1, 1000000000000> pico;
179#if (INT_MAX >= INT32_MAX)
180 typedef
ratio<1, 1000000000> nano;
181 typedef
ratio<1, 1000000> micro;
184#if (INT_MAX >= INT16_MAX)
185 typedef
ratio<1, 1000> milli;
186 typedef
ratio<1, 100> centi;
187 typedef
ratio<1, 10> deci;
188 typedef
ratio<10, 1> deca;
189 typedef
ratio<100, 1> hecto;
190 typedef
ratio<1000, 1> kilo;
193#if (INT_MAX >= INT32_MAX)
194 typedef
ratio<1000000, 1> mega;
195 typedef
ratio<1000000000, 1> giga;
198#if INT_MAX > INT32_MAX
199 typedef
ratio<1000000000000, 1> tera;
200 typedef
ratio<1000000000000000, 1> peta;
201 typedef
ratio<1000000000000000000, 1> exa;
203 typedef
ratio<1000000000000000000LL * 1000LL, 1> zetta;
205 typedef
ratio<1000000000000000000LL * 1000000LL, 1> yotta;
207 typedef
ratio<1000000000000000000LL * 1000000000LL, 1> ronna;
209 typedef
ratio<1000000000000000000LL * 1000000000000LL, 1> quetta;
213 typedef
ratio<355, 113> ratio_pi;
216 typedef
ratio<239, 169> ratio_root2;
219 typedef
ratio<169, 239> ratio_1_over_root2;
222 typedef
ratio<326, 120> ratio_e;
225 namespace private_ratio
228 template <
typename T, T Value1, T Value2,
bool = (Value2 == 0)>
232 template <
typename T, T Value1, T Value2>
233 struct ratio_gcd<T, Value1, Value2, false>
235 static constexpr T value = ratio_gcd<T, Value2, Value1 % Value2>::value;
239 template <
typename T, T Value1, T Value2>
240 struct ratio_gcd<T, Value1, Value2, true>
242 static constexpr T value = (Value1 < 0) ? -Value1 : Value1;
246 template <
typename T, T Value1, T Value2>
251 static constexpr T product = ((Value1 * Value2) < 0) ? -(Value1 * Value2) : Value1 * Value2;
255 static constexpr T value = product / ratio_gcd<T, Value1, Value2>::value;
258 template <
typename R1>
263 static ETL_CONSTEXPR11 intmax_t gcd = etl::private_ratio::ratio_gcd<intmax_t, R1::num, R1::den>::value;
267 using type = ratio<R1::num / gcd, R1::den / gcd>;
270 template <
typename R1,
typename R2>
275 static ETL_CONSTEXPR11 intmax_t lcm = etl::private_ratio::ratio_lcm<intmax_t, R1::den, R2::den>::value;
279 using type =
typename ratio_reduce< ratio<R1::num * lcm / R1::den + R2::num * lcm / R2::den, lcm>>::type;
282 template <
typename R1,
typename R2>
283 struct ratio_subtract
287 using type =
typename ratio_add<R1, ratio<-R2::num, R2::den>>::type;
290 template <
typename R1,
typename R2>
291 struct ratio_multiply
295 static ETL_CONSTEXPR11 intmax_t gcd1 = etl::private_ratio::ratio_gcd<intmax_t, R1::num, R2::den>::value;
296 static ETL_CONSTEXPR11 intmax_t gcd2 = etl::private_ratio::ratio_gcd<intmax_t, R2::num, R1::den>::value;
300 using type = ratio<(R1::num / gcd1) * (R2::num / gcd2), (R1::den / gcd2) * (R2::den / gcd1)>;
303 template <
typename R1,
typename R2>
308 using type =
typename ratio_multiply<R1, ratio<R2::den, R2::num>>::type;
Definition type_traits.h:97
ratio_equal
Definition ratio.h:99
ratio_not_equal
Definition ratio.h:107
ratio
Definition ratio.h:52