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
36#include "type_traits.h"
37
38#include <stddef.h>
39#include <stdint.h>
40
43
44namespace etl
45{
46 template <intmax_t NUM, intmax_t DEN = 1UL>
47 struct ratio
48 {
49 static ETL_CONSTANT intmax_t num = NUM;
50 static ETL_CONSTANT intmax_t den = DEN;
51 };
52
53 template <intmax_t NUM, intmax_t DEN>
54 ETL_CONSTANT intmax_t ratio<NUM, DEN>::num;
55
56 template <intmax_t NUM, intmax_t DEN>
57 ETL_CONSTANT intmax_t ratio<NUM, DEN>::den;
58
59 #if INT_MAX > INT32_MAX
65 #endif
66
67 #if (INT_MAX >= INT32_MAX)
70 #endif
71
72 #if (INT_MAX >= INT16_MAX)
73 typedef ratio<1, 1000> milli;
74 typedef ratio<1, 100> centi;
75 typedef ratio<1, 10> deci;
76 typedef ratio<10, 1> deca;
77 typedef ratio<100, 1> hecto;
78 typedef ratio<1000, 1> kilo;
79 #endif
80
81 #if (INT_MAX >= INT32_MAX)
82 typedef ratio<1000000, 1> mega;
84 #endif
85
86 #if INT_MAX > INT32_MAX
92 #endif
93
96
99
102
103#if ETL_USING_CPP11
104 namespace private_ratio
105 {
106 // Primary template for GCD calculation
107 template <typename T, T A, T B, bool = (B == 0)>
108 struct ratio_gcd;
109
110 // Specialisation for the case when B is not zero
111 template <typename T, T A, T B>
112 struct ratio_gcd<T, A, B, false>
113 {
114 static constexpr T value = ratio_gcd<T, B, A % B>::value;
115 };
116
117 // Specialisation for the case when B is zero
118 template <typename T, T A, T B>
119 struct ratio_gcd<T, A, B, true>
120 {
121 static constexpr T value = (A < 0) ? -A : A;
122 };
123
124 // Primary template for LCM calculation
125 template <typename T, T A, T B>
126 struct ratio_lcm
127 {
128 private:
129
130 static constexpr T product = ((A * B) < 0) ? -(A * B) : A * B;
131
132 public:
133
134 static constexpr T value = product / ratio_gcd<T, A, B>::value;
135 };
136
137 template<typename R1>
138 struct ratio_reduce
139 {
140 private:
141
142 static ETL_CONSTEXPR11 intmax_t gcd = etl::private_ratio::ratio_gcd<intmax_t, R1::num, R1::den>::value;
143
144 public:
145
146 using type = ratio<R1::num / gcd, R1::den / gcd>;
147 };
148
149 template<typename R1, typename R2>
150 struct ratio_add
151 {
152 private:
153
154 static ETL_CONSTEXPR11 intmax_t lcm = etl::private_ratio::ratio_lcm<intmax_t, R1::den, R2::den>::value;
155
156 public:
157
158 using type = typename ratio_reduce<ratio<R1::num * lcm / R1::den + R2::num * lcm / R2::den, lcm>>::type;
159 };
160
161 template<typename R1, typename R2>
162 struct ratio_subtract
163 {
164 public:
165 using type = typename ratio_add<R1, ratio<-R2::num, R2::den>>::type;
166 };
167
168 template<typename R1, typename R2>
169 struct ratio_multiply
170 {
171 private:
172 static ETL_CONSTEXPR11 intmax_t gcd1 = etl::private_ratio::ratio_gcd<intmax_t, R1::num, R2::den>::value;
173 static ETL_CONSTEXPR11 intmax_t gcd2 = etl::private_ratio::ratio_gcd<intmax_t, R2::num, R1::den>::value;
174
175 public:
176 using type = ratio<(R1::num / gcd1) * (R2::num / gcd2), (R1::den / gcd2) * (R2::den / gcd1)>;
177 };
178
179 template<typename R1, typename R2>
180 struct ratio_divide
181 {
182 public:
183 using type = typename ratio_multiply<R1, ratio<R2::den, R2::num>>::type;
184 };
185 }
186
187 template<typename R1, typename R2>
188 using ratio_add = typename private_ratio::ratio_add<R1, R2>::type;
189
190 template<typename R1, typename R2>
191 using ratio_subtract = typename private_ratio::ratio_subtract<R1, R2>::type;
192
193 template<typename R1, typename R2>
194 using ratio_multiply = typename private_ratio::ratio_multiply<R1, R2>::type;
195
196 template<typename R1, typename R2>
197 using ratio_divide = typename private_ratio::ratio_divide<R1, R2>::type;
198
199 template<typename R1, typename R2>
200 struct ratio_equal : etl::integral_constant<bool, (R1::num == R2::num && R1::den == R2::den)>
201 {
202 };
203
204 template<typename R1, typename R2>
205 struct ratio_not_equal : etl::integral_constant<bool, (R1::num != R2::num || R1::den != R2::den)>
206 {
207 };
208
209 template<typename R1, typename R2>
210 struct ratio_less : etl::integral_constant<bool, (R1::num * R2::den < R2::num * R1::den)>
211 {
212 };
213
214 template<typename R1, typename R2>
215 struct ratio_less_equal : etl::integral_constant<bool, (R1::num * R2::den <= R2::num * R1::den)>
216 {
217 };
218
219 template<typename R1, typename R2>
220 struct ratio_greater : etl::integral_constant<bool, (R1::num * R2::den > R2::num * R1::den)>
221 {
222 };
223
224 template<typename R1, typename R2>
225 struct ratio_greater_equal: etl::integral_constant<bool, (R1::num * R2::den >= R2::num * R1::den)>
226 {
227 };
228
229#if ETL_USING_CPP14
230 template<typename R1, typename R2>
231 ETL_CONSTEXPR14 bool ratio_equal_v = ratio_equal<R1, R2>::value;
232
233 template<typename R1, typename R2>
234 ETL_CONSTEXPR14 bool ratio_not_equal_v = ratio_not_equal<R1, R2>::value;
235
236 template<typename R1, typename R2>
237 ETL_CONSTEXPR14 bool ratio_less_v = ratio_less<R1, R2>::value;
238
239 template<typename R1, typename R2>
240 ETL_CONSTEXPR14 bool ratio_less_equal_v = ratio_less_equal<R1, R2>::value;
241
242 template<typename R1, typename R2>
243 ETL_CONSTEXPR14 bool ratio_greater_v = ratio_greater<R1, R2>::value;
244
245 template<typename R1, typename R2>
246 ETL_CONSTEXPR14 bool ratio_greater_equal_v = ratio_greater_equal<R1, R2>::value;
247#endif
248#endif
249}
250
251#endif
252
integral_constant
Definition type_traits_generator.h:827
bitset_ext
Definition absolute.h:38
ratio< 355, 113 > ratio_pi
An approximation of PI to 6 digits.
Definition ratio.h:95
ratio< 326, 120 > ratio_e
An approximation of e.
Definition ratio.h:101
ratio< 239, 169 > ratio_root2
An approximation of root 2.
Definition ratio.h:98
pair holds two objects of arbitrary type
Definition utility.h:164
Definition ratio.h:48