Embedded Template Library 1.0
Loading...
Searching...
No Matches
ratio.h
Go to the documentation of this file.
1
2
3/******************************************************************************
4The MIT License(MIT)
5
6Embedded Template Library.
7https://github.com/ETLCPP/etl
8https://www.etlcpp.com
9
10Copyright(c) 2016 John Wellbelove
11
12Permission is hereby granted, free of charge, to any person obtaining a copy
13of this software and associated documentation files(the "Software"), to deal
14in the Software without restriction, including without limitation the rights
15to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16copies of the Software, and to permit persons to whom the Software is
17furnished to do so, subject to the following conditions :
18
19The above copyright notice and this permission notice shall be included in all
20copies or substantial portions of the Software.
21
22THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28SOFTWARE.
29******************************************************************************/
30
31#ifndef ETL_RATIO_INCLUDED
32#define ETL_RATIO_INCLUDED
33
34#include "platform.h"
35#include "static_assert.h"
36#include "gcd.h"
37
38#include "type_traits.h"
39
40#include <stddef.h>
41#include <stdint.h>
42
45
46namespace etl
47{
48 //***********************************************************************
50 //***********************************************************************
51 template <intmax_t Num, intmax_t Den = 1UL>
52 struct ratio
53 {
54 ETL_STATIC_ASSERT(Num != 0, "Numerator cannot be zero");
55 ETL_STATIC_ASSERT(Den != 0, "Denominator cannot be zero");
56
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;
59
60 typedef etl::ratio<num, den> type;
61 };
62
63 template <intmax_t Num, intmax_t Den>
64 ETL_CONSTANT intmax_t ratio<Num, Den>::num;
65
66 template <intmax_t Num, intmax_t Den>
67 ETL_CONSTANT intmax_t ratio<Num, Den>::den;
68
69#if ETL_USING_CPP11
70 //***********************************************************************
72 //***********************************************************************
73 template <typename TRatio1, typename TRatio2>
74 using ratio_add = etl::ratio<(TRatio1::num * TRatio2::den) + (TRatio2::num * TRatio1::den), TRatio1::den * TRatio2::den>;
75
76 //***********************************************************************
78 //***********************************************************************
79 template <typename TRatio1, typename TRatio2>
80 using ratio_subtract = etl::ratio<(TRatio1::num * TRatio2::den) - (TRatio2::num * TRatio1::den), TRatio1::den * TRatio2::den>;
81
82 //***********************************************************************
84 //***********************************************************************
85 template <typename TRatio1, typename TRatio2>
87
88 //***********************************************************************
90 //***********************************************************************
91 template <typename TRatio1, typename TRatio2>
93#endif
94
95 //***********************************************************************
97 //***********************************************************************
98 template <typename TRatio1, typename TRatio2>
99 struct ratio_equal : etl::bool_constant<(TRatio1::num == TRatio2::num) && (TRatio1::den == TRatio2::den)>
100 {
101 };
102
103 //***********************************************************************
105 //***********************************************************************
106 template <typename TRatio1, typename TRatio2>
107 struct ratio_not_equal : etl::bool_constant<!etl::ratio_equal<TRatio1, TRatio2>::value>
108 {
109 };
110
111 //***********************************************************************
113 //***********************************************************************
114 template <typename TRatio1, typename TRatio2>
115 struct ratio_less : etl::bool_constant<(TRatio1::num * TRatio2::den) < (TRatio2::num * TRatio1::den)>
116 {
117 };
118
119 //***********************************************************************
121 //***********************************************************************
122 template <typename TRatio1, typename TRatio2>
123 struct ratio_less_equal : etl::bool_constant<!etl::ratio_less<TRatio2, TRatio1>::value>
124 {
125 };
126
127 //***********************************************************************
129 //***********************************************************************
130 template <typename TRatio1, typename TRatio2>
131 struct ratio_greater : etl::bool_constant<etl::ratio_less<TRatio2, TRatio1>::value>
132 {
133 };
134
135 //***********************************************************************
137 //***********************************************************************
138 template <typename TRatio1, typename TRatio2>
139 struct ratio_greater_equal : etl::bool_constant<!etl::ratio_less<TRatio1, TRatio2>::value>
140 {
141 };
142
143#if ETL_USING_CPP17
144 template<typename R1, typename R2>
145 inline constexpr bool ratio_equal_v = ratio_equal<R1, R2>::value;
146
147 template<typename R1, typename R2>
148 inline constexpr bool ratio_not_equal_v = ratio_not_equal<R1, R2>::value;
149
150 template<typename R1, typename R2>
151 inline constexpr bool ratio_less_v = ratio_less<R1, R2>::value;
152
153 template<typename R1, typename R2>
154 inline constexpr bool ratio_less_equal_v = ratio_less_equal<R1, R2>::value;
155
156 template<typename R1, typename R2>
157 inline constexpr bool ratio_greater_v = ratio_greater<R1, R2>::value;
158
159 template<typename R1, typename R2>
160 inline constexpr bool ratio_greater_equal_v = ratio_greater_equal<R1, R2>::value;
161#endif
162
163 //***********************************************************************
165 //***********************************************************************
166 #if INT_MAX > INT32_MAX
167 typedef ratio<1, 1000000000000000000> atto;
168 typedef ratio<1, 1000000000000000> femto;
169 typedef ratio<1, 1000000000000> pico;
170 #endif
171
172 #if (INT_MAX >= INT32_MAX)
173 typedef ratio<1, 1000000000> nano;
174 typedef ratio<1, 1000000> micro;
175 #endif
176
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;
184 #endif
185
186 #if (INT_MAX >= INT32_MAX)
187 typedef ratio<1000000, 1> mega;
188 typedef ratio<1000000000, 1> giga;
189 #endif
190
191 #if INT_MAX > INT32_MAX
192 typedef ratio<1000000000000, 1> tera;
193 typedef ratio<1000000000000000, 1> peta;
194 typedef ratio<1000000000000000000, 1> exa;
195 #endif
196
198 typedef ratio<355, 113> ratio_pi;
199
201 typedef ratio<239, 169> ratio_root2;
202
204 typedef ratio<169, 239> ratio_1_over_root2;
205
207 typedef ratio<326, 120> ratio_e;
208
209#if ETL_USING_CPP11
210 namespace private_ratio
211 {
212 // Primary template for GCD calculation
213 template <typename T, T Value1, T Value2, bool = (Value2 == 0)>
214 struct ratio_gcd;
215
216 // Specialisation for the case when Value2 is not zero
217 template <typename T, T Value1, T Value2>
218 struct ratio_gcd<T, Value1, Value2, false>
219 {
220 static constexpr T value = ratio_gcd<T, Value2, Value1 % Value2>::value;
221 };
222
223 // Specialisation for the case when Value2 is zero
224 template <typename T, T Value1, T Value2>
225 struct ratio_gcd<T, Value1, Value2, true>
226 {
227 static constexpr T value = (Value1 < 0) ? -Value1 : Value1;
228 };
229
230 // Primary template for LCM calculation
231 template <typename T, T Value1, T Value2>
232 struct ratio_lcm
233 {
234 private:
235
236 static constexpr T product = ((Value1 * Value2) < 0) ? -(Value1 * Value2) : Value1 * Value2;
237
238 public:
239
240 static constexpr T value = product / ratio_gcd<T, Value1, Value2>::value;
241 };
242
243 template<typename R1>
244 struct ratio_reduce
245 {
246 private:
247
248 static ETL_CONSTEXPR11 intmax_t gcd = etl::private_ratio::ratio_gcd<intmax_t, R1::num, R1::den>::value;
249
250 public:
251
252 using type = ratio<R1::num / gcd, R1::den / gcd>;
253 };
254
255 template<typename R1, typename R2>
256 struct ratio_add
257 {
258 private:
259
260 static ETL_CONSTEXPR11 intmax_t lcm = etl::private_ratio::ratio_lcm<intmax_t, R1::den, R2::den>::value;
261
262 public:
263
264 using type = typename ratio_reduce<ratio<R1::num * lcm / R1::den + R2::num * lcm / R2::den, lcm>>::type;
265 };
266
267 template<typename R1, typename R2>
268 struct ratio_subtract
269 {
270 public:
271 using type = typename ratio_add<R1, ratio<-R2::num, R2::den>>::type;
272 };
273
274 template<typename R1, typename R2>
275 struct ratio_multiply
276 {
277 private:
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;
280
281 public:
282 using type = ratio<(R1::num / gcd1) * (R2::num / gcd2), (R1::den / gcd2) * (R2::den / gcd1)>;
283 };
284
285 template<typename R1, typename R2>
286 struct ratio_divide
287 {
288 public:
289 using type = typename ratio_multiply<R1, ratio<R2::den, R2::num>>::type;
290 };
291 }
292
293#endif
294}
295
296#endif
297
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