31#ifndef ETL_RATIO_INCLUDED
32#define ETL_RATIO_INCLUDED
35#include "static_assert.h"
51 template <
intmax_t Num,
intmax_t Den = 1UL>
54 ETL_STATIC_ASSERT(Num != 0,
"Numerator cannot be zero");
55 ETL_STATIC_ASSERT(Den != 0,
"Denominator cannot be zero");
57 static ETL_CONSTANT intmax_t num = Num / etl::gcd_const<Num, Den>::value;
58 static ETL_CONSTANT intmax_t den = Den / etl::gcd_const<Num, Den>::value;
63 template <
intmax_t Num,
intmax_t Den>
64 ETL_CONSTANT intmax_t ratio<Num, Den>::num;
66 template <
intmax_t Num,
intmax_t Den>
67 ETL_CONSTANT intmax_t ratio<Num, Den>::den;
73 template <
typename TRatio1,
typename TRatio2>
74 using ratio_add =
etl::ratio<(TRatio1::num * TRatio2::den) + (TRatio2::num * TRatio1::den), TRatio1::den * TRatio2::den>;
79 template <
typename TRatio1,
typename TRatio2>
80 using ratio_subtract =
etl::ratio<(TRatio1::num * TRatio2::den) - (TRatio2::num * TRatio1::den), TRatio1::den * TRatio2::den>;
85 template <
typename TRatio1,
typename TRatio2>
91 template <
typename TRatio1,
typename TRatio2>
98 template <
typename TRatio1,
typename TRatio2>
106 template <
typename TRatio1,
typename TRatio2>
114 template <
typename TRatio1,
typename TRatio2>
115 struct ratio_less :
etl::bool_constant<(TRatio1::num * TRatio2::den) < (TRatio2::num * TRatio1::den)>
122 template <typename TRatio1, typename TRatio2>
123 struct ratio_less_equal : etl::bool_constant<!etl::ratio_less<TRatio2, TRatio1>::value>
130 template <typename TRatio1, typename TRatio2>
131 struct ratio_greater : etl::bool_constant<etl::ratio_less<TRatio2, TRatio1>::value>
138 template <typename TRatio1, typename TRatio2>
139 struct ratio_greater_equal : etl::bool_constant<!etl::ratio_less<TRatio1, TRatio2>::value>
144 template<typename R1, typename R2>
145 inline constexpr bool ratio_equal_v = ratio_equal<R1, R2>::value;
147 template<typename R1, typename R2>
148 inline constexpr bool ratio_not_equal_v = ratio_not_equal<R1, R2>::value;
150 template<typename R1, typename R2>
151 inline constexpr bool ratio_less_v = ratio_less<R1, R2>::value;
153 template<typename R1, typename R2>
154 inline constexpr bool ratio_less_equal_v = ratio_less_equal<R1, R2>::value;
156 template<typename R1, typename R2>
157 inline constexpr bool ratio_greater_v = ratio_greater<R1, R2>::value;
159 template<typename R1, typename R2>
160 inline constexpr bool ratio_greater_equal_v = ratio_greater_equal<R1, R2>::value;
166 #if INT_MAX > INT32_MAX
167 typedef
ratio<1, 1000000000000000000> atto;
168 typedef
ratio<1, 1000000000000000> femto;
169 typedef
ratio<1, 1000000000000> pico;
172 #if (INT_MAX >= INT32_MAX)
173 typedef
ratio<1, 1000000000> nano;
174 typedef
ratio<1, 1000000> micro;
177 #if (INT_MAX >= INT16_MAX)
178 typedef
ratio<1, 1000> milli;
179 typedef
ratio<1, 100> centi;
180 typedef
ratio<1, 10> deci;
181 typedef
ratio<10, 1> deca;
182 typedef
ratio<100, 1> hecto;
183 typedef
ratio<1000, 1> kilo;
186 #if (INT_MAX >= INT32_MAX)
187 typedef
ratio<1000000, 1> mega;
188 typedef
ratio<1000000000, 1> giga;
191 #if INT_MAX > INT32_MAX
192 typedef
ratio<1000000000000, 1> tera;
193 typedef
ratio<1000000000000000, 1> peta;
194 typedef
ratio<1000000000000000000, 1> exa;
198 typedef
ratio<355, 113> ratio_pi;
201 typedef
ratio<239, 169> ratio_root2;
204 typedef
ratio<169, 239> ratio_1_over_root2;
207 typedef
ratio<326, 120> ratio_e;
210 namespace private_ratio
213 template <
typename T, T Value1, T Value2,
bool = (Value2 == 0)>
217 template <
typename T, T Value1, T Value2>
218 struct ratio_gcd<T, Value1, Value2, false>
220 static constexpr T value = ratio_gcd<T, Value2, Value1 % Value2>::value;
224 template <
typename T, T Value1, T Value2>
225 struct ratio_gcd<T, Value1, Value2, true>
227 static constexpr T value = (Value1 < 0) ? -Value1 : Value1;
231 template <
typename T, T Value1, T Value2>
236 static constexpr T product = ((Value1 * Value2) < 0) ? -(Value1 * Value2) : Value1 * Value2;
240 static constexpr T value = product / ratio_gcd<T, Value1, Value2>::value;
243 template<
typename R1>
248 static ETL_CONSTEXPR11 intmax_t gcd = etl::private_ratio::ratio_gcd<intmax_t, R1::num, R1::den>::value;
252 using type = ratio<R1::num / gcd, R1::den / gcd>;
255 template<
typename R1,
typename R2>
260 static ETL_CONSTEXPR11 intmax_t lcm = etl::private_ratio::ratio_lcm<intmax_t, R1::den, R2::den>::value;
264 using type =
typename ratio_reduce<ratio<R1::num * lcm / R1::den + R2::num * lcm / R2::den, lcm>>::type;
267 template<
typename R1,
typename R2>
268 struct ratio_subtract
271 using type =
typename ratio_add<R1, ratio<-R2::num, R2::den>>::type;
274 template<
typename R1,
typename R2>
275 struct ratio_multiply
278 static ETL_CONSTEXPR11 intmax_t gcd1 = etl::private_ratio::ratio_gcd<intmax_t, R1::num, R2::den>::value;
279 static ETL_CONSTEXPR11 intmax_t gcd2 = etl::private_ratio::ratio_gcd<intmax_t, R2::num, R1::den>::value;
282 using type = ratio<(R1::num / gcd1) * (R2::num / gcd2), (R1::den / gcd2) * (R2::den / gcd1)>;
285 template<
typename R1,
typename R2>
289 using type =
typename ratio_multiply<R1, ratio<R2::den, R2::num>>::type;
bitset_ext
Definition absolute.h:39
Definition type_traits_generator.h:912
ratio_equal
Definition ratio.h:100
ratio_not_equal
Definition ratio.h:108
ratio
Definition ratio.h:53