Embedded Template Library 1.0
Loading...
Searching...
No Matches
numeric.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) 2014 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_NUMERIC_INCLUDED
32#define ETL_NUMERIC_INCLUDED
33
34#include "platform.h"
35#include "iterator.h"
36#include "limits.h"
37#include "type_traits.h"
38
39#if ETL_USING_STL
40 #include <iterator>
41#endif
42
45
46namespace etl
47{
48 //***************************************************************************
56 //***************************************************************************
57 template <typename TIterator, typename T>
58 ETL_CONSTEXPR14 void iota(TIterator first, TIterator last, T value)
59 {
60 while (first != last)
61 {
62 *first++ = value++;
63 }
64 }
65
66 //***************************************************************************
69 //***************************************************************************
70 template <typename T>
71 ETL_CONSTEXPR14 typename etl::enable_if< !etl::is_pointer<T>::value && !etl::is_integral<T>::value && etl::is_floating_point<T>::value, T>::type
72 midpoint(T a, T b) ETL_NOEXCEPT
73 {
74 T lo = etl::numeric_limits<T>::min() * T(2);
75 T hi = etl::numeric_limits<T>::max() * T(2);
76
77 return ((abs(a) <= hi) && (abs(b) <= hi)) ? (a + b) / T(2)
78 : (abs(a) < lo) ? a + (b / T(2))
79 : (abs(b) < lo) ? ((a / T(2)) + b)
80 : (a / T(2)) + (b / T(2));
81 }
82
83 //***************************************************************************
86 //***************************************************************************
87 template <typename T>
88 ETL_CONSTEXPR14 typename etl::enable_if<
89 !etl::is_pointer<T>::value && etl::is_integral<T>::value && !etl::is_floating_point<T>::value && etl::is_unsigned<T>::value, T>::type
90 midpoint(T a, T b) ETL_NOEXCEPT
91 {
92 if (a > b)
93 {
94 return a - ((a - b) >> 1);
95 }
96 else
97 {
98 return a + ((b - a) >> 1);
99 }
100 }
101
102 //***************************************************************************
105 //***************************************************************************
106 template <typename T>
107 ETL_CONSTEXPR14 typename etl::enable_if<
108 !etl::is_pointer<T>::value && etl::is_integral<T>::value && !etl::is_floating_point<T>::value && etl::is_signed<T>::value, T>::type
109 midpoint(T a, T b) ETL_NOEXCEPT
110 {
111 typedef typename etl::make_unsigned<T>::type utype;
112
113 if (a > b)
114 {
115 return a - T(utype(utype(a) - utype(b)) >> 1);
116 }
117 else
118 {
119 return a + T((utype(b) - utype(a)) >> 1);
120 }
121 }
122
123 //***************************************************************************
126 //***************************************************************************
127 template <typename T>
128 ETL_CONSTEXPR14 typename etl::enable_if< etl::is_pointer<T>::value && !etl::is_integral<T>::value && !etl::is_floating_point<T>::value, T>::type
129 midpoint(T a, T b) ETL_NOEXCEPT
130 {
131 if (a > b)
132 {
133 return b + (etl::distance(b, a) / 2U);
134 }
135 else
136 {
137 return a + (etl::distance(a, b) / 2U);
138 }
139 }
140
141 //***************************************************************************
144 //***************************************************************************
145 template <typename T>
146 ETL_CONSTEXPR14 T midpoint(
147 T a, T b,
148 typename etl::enable_if< !etl::is_pointer<T>::value && !etl::is_integral<T>::value && !etl::is_floating_point<T>::value
149 && etl::is_same<typename etl::iterator_traits<T>::iterator_category, ETL_OR_STD::random_access_iterator_tag>::value,
150 int>::type = 0)
151 {
152 if (a > b)
153 {
154 return b + (etl::distance(b, a) / 2U);
155 }
156 else
157 {
158 return a + (etl::distance(a, b) / 2U);
159 }
160 }
161
162 //***************************************************************************
166 //***************************************************************************
167 template <typename T>
168 ETL_CONSTEXPR14 T midpoint(T a, T b,
169 typename etl::enable_if<
170 (!etl::is_pointer<T>::value && !etl::is_integral<T>::value && !etl::is_floating_point<T>::value
171 && (etl::is_same<typename etl::iterator_traits<T>::iterator_category, ETL_OR_STD::forward_iterator_tag>::value
172 || etl::is_same<typename etl::iterator_traits<T>::iterator_category, ETL_OR_STD::bidirectional_iterator_tag>::value)),
173 int>::type = 0)
174 {
175 etl::advance(a, etl::distance(a, b) / 2U);
176 return a;
177 }
178
179 //***************************************************************************
182 //***************************************************************************
183 template <typename T>
184 ETL_CONSTEXPR typename etl::enable_if<etl::is_floating_point<T>::value, T>::type lerp(T a, T b, T t) ETL_NOEXCEPT
185 {
186 return a + (t * (b - a));
187 }
188
189 //***************************************************************************
192 //***************************************************************************
193 template <typename TArithmetic1, typename TArithmetic2, typename TArithmetic3>
194 ETL_CONSTEXPR typename etl::enable_if<
195 !etl::is_floating_point<TArithmetic1>::value || !etl::is_floating_point<TArithmetic2>::value || !etl::is_floating_point<TArithmetic3>::value,
196 typename etl::conditional< etl::is_same<TArithmetic1, long double>::value || etl::is_same<TArithmetic2, long double>::value
197 || etl::is_same<TArithmetic3, long double>::value,
198 long double, double>::type>::type
199 lerp(TArithmetic1 a, TArithmetic2 b, TArithmetic3 t) ETL_NOEXCEPT
200 {
201 typedef typename etl::conditional<etl::is_integral<TArithmetic1>::value, double, TArithmetic1>::type typecast_a;
202 typedef typename etl::conditional<etl::is_integral<TArithmetic2>::value, double, TArithmetic2>::type typecast_b;
203 typedef typename etl::conditional<etl::is_integral<TArithmetic3>::value, double, TArithmetic3>::type typecast_t;
204
205 return typecast_a(a) + (typecast_t(t) * (typecast_b(b) - typecast_a(a)));
206 }
207} // namespace etl
208
209#endif
Definition limits.h:1715
ETL_CONSTEXPR14 void iota(TIterator first, TIterator last, T value)
Definition numeric.h:58
bitset_ext
Definition absolute.h:40
ETL_CONSTEXPR14 etl::chrono::duration< TRep, TPeriod > abs(etl::chrono::duration< TRep, TPeriod > d) ETL_NOEXCEPT
Returns the absolute value of a duration.
Definition duration.h:688
ETL_CONSTEXPR etl::enable_if< etl::is_floating_point< T >::value, T >::type lerp(T a, T b, T t) ETL_NOEXCEPT
Definition numeric.h:184
ETL_CONSTEXPR14 etl::enable_if<!etl::is_pointer< T >::value &&!etl::is_integral< T >::value &&etl::is_floating_point< T >::value, T >::type midpoint(T a, T b) ETL_NOEXCEPT
Definition numeric.h:72