YAP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MathUtilities.h
1 /* YAP - Yet another PWA toolkit
2  Copyright 2015, Technische Universitaet Muenchen,
3  Authors: Daniel Greenwald, Johannes Rauch
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef yap_MathUtilities_h
20 #define yap_MathUtilities_h
21 
22 #include <type_traits>
23 
24 namespace yap {
25 
28 
30 constexpr bool is_odd(int val)
31 { return val & 0x1; }
32 
34 constexpr bool is_even(int val)
35 { return not is_odd(val); }
36 
38 template <typename T>
39 typename std::enable_if<std::is_signed<T>::value, T>::type
40 constexpr signum(const T& val)
41 { return (T(0) < val) - (val < T(0)); }
42 
44 constexpr int pow_negative_one(int exponent)
45 { return is_odd(exponent) ? -1 : +1; }
46 
47 }
48 
49 #endif
constexpr bool is_odd(int val)
Math Utilities.
Definition: MathUtilities.h:30
std::enable_if< std::is_signed< T >::value, T >::type constexpr signum(const T &val)
extracts sign from value
Definition: MathUtilities.h:40
constexpr bool is_even(int val)
Definition: MathUtilities.h:34
constexpr int pow_negative_one(int exponent)
optimized function for (-1)^n
Definition: MathUtilities.h:44