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 "type_traits.h"
36#include "limits.h"
37#include "iterator.h"
38
39#if ETL_USING_STL
40 #include <iterator>
41#endif
42
45
46namespace etl
47{
48 //***************************************************************************
55 //***************************************************************************
56 template <typename TIterator, typename T>
57 ETL_CONSTEXPR14 void iota(TIterator first, TIterator last, T value)
58 {
59 while (first != last)
60 {
61 *first++ = value++;
62 }
63 }
64
65 //***************************************************************************
68 //***************************************************************************
69 template <typename T>
70 ETL_CONSTEXPR14 typename etl::enable_if<!etl::is_pointer<T>::value &&
73 midpoint(T a, T b) ETL_NOEXCEPT
74 {
77
78 return ((abs(a) <= hi) && (abs(b) <= hi)) ?
79 (a + b) / T(2) :
80 (abs(a) < lo) ?
81 a + (b / T(2)) :
82 (abs(b) < lo) ?
83 ((a / T(2)) + b) :
84 (a / T(2)) + (b / T(2));
85 }
86
87 //***************************************************************************
90 //***************************************************************************
91 template <typename T>
92 ETL_CONSTEXPR14 typename etl::enable_if<!etl::is_pointer<T>::value &&
96 midpoint(T a, T b) ETL_NOEXCEPT
97 {
98 if (a > b)
99 {
100 return a - ((a - b) >> 1);
101 }
102 else
103 {
104 return a + ((b - a) >> 1);
105 }
106 }
107
108 //***************************************************************************
111 //***************************************************************************
112 template <typename T>
113 ETL_CONSTEXPR14 typename etl::enable_if<!etl::is_pointer<T>::value&&
117 midpoint(T a, T b) ETL_NOEXCEPT
118 {
119 typedef typename etl::make_unsigned<T>::type utype;
120
121 if (a > b)
122 {
123 return a - T(utype(utype(a) - utype(b)) >> 1);
124 }
125 else
126 {
127 return a + T((utype(b) - utype(a)) >> 1);
128 }
129 }
130
131 //***************************************************************************
134 //***************************************************************************
135 template <typename T>
136 ETL_CONSTEXPR14 typename etl::enable_if<etl::is_pointer<T>::value&&
139 midpoint(T a, T b) ETL_NOEXCEPT
140 {
141 if (a > b)
142 {
143 return b + (etl::distance(b, a) / 2U);
144 }
145 else
146 {
147 return a + (etl::distance(a, b) / 2U);
148 }
149 }
150
151 //***************************************************************************
154 //***************************************************************************
155 template <typename T>
156 ETL_CONSTEXPR14 T midpoint(T a, T b, typename etl::enable_if<!etl::is_pointer<T>::value &&
159 etl::is_same<typename etl::iterator_traits<T>::iterator_category, ETL_OR_STD::random_access_iterator_tag>::value , int>::type = 0)
160 {
161 if (a > b)
162 {
163 return b + (etl::distance(b, a) / 2U);
164 }
165 else
166 {
167 return a + (etl::distance(a, b) / 2U);
168 }
169 }
170
171 //***************************************************************************
175 //***************************************************************************
176 template <typename T>
177 ETL_CONSTEXPR14 T midpoint(T a, T b, typename etl::enable_if<(!etl::is_pointer<T>::value &&
180 (etl::is_same<typename etl::iterator_traits<T>::iterator_category, ETL_OR_STD::forward_iterator_tag>::value ||
181 etl::is_same<typename etl::iterator_traits<T>::iterator_category, ETL_OR_STD::bidirectional_iterator_tag>::value)), int>::type = 0)
182 {
183 etl::advance(a, etl::distance(a, b) / 2U);
184 return a;
185 }
186
187 //***************************************************************************
190 //***************************************************************************
191 template <typename T>
192 ETL_CONSTEXPR typename etl::enable_if<etl::is_floating_point<T>::value, T>::type
193 lerp(T a, T b, T t) ETL_NOEXCEPT
194 {
195 return a + (t * (b - a));
196 }
197
198 //***************************************************************************
201 //***************************************************************************
202 template <typename TArithmetic1, typename TArithmetic2, typename TArithmetic3>
207 etl::is_same<TArithmetic3, long double>::value, long double, double>::type>::type
216}
217
218#endif
Definition limits.h:1175
ETL_CONSTEXPR14 void iota(TIterator first, TIterator last, T value)
Definition numeric.h:57
enable_if
Definition type_traits_generator.h:1186
is_floating_point
Definition type_traits_generator.h:1026
is_integral
Definition type_traits_generator.h:996
is_pointer
Definition type_traits_generator.h:1096
is_same
Definition type_traits_generator.h:1036
is_signed
Definition type_traits_generator.h:1006
is_unsigned
Definition type_traits_generator.h:1016
make_unsigned
Definition type_traits_generator.h:1176
bitset_ext
Definition absolute.h:38
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:193
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:73
pair holds two objects of arbitrary type
Definition utility.h:164