Allium  0.1.0
numeric.hpp
1 // Copyright 2020 Hannah Rittich
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef ALLIUM_UTIL_NUMERIC_HPP
16 #define ALLIUM_UTIL_NUMERIC_HPP
17 
18 #include <complex>
19 #include "warnings.hpp"
20 
21 namespace allium {
23  template <typename T> struct real_part {};
24  template <> struct real_part<float> { typedef float type; };
25  template <> struct real_part<double> { typedef double type; };
26  template <> struct real_part<int> { typedef int type; };
27  template <> struct real_part<long> { typedef long type; };
28  template <typename T> struct real_part<std::complex<T>> { typedef T type; };
29 
30 
31  template <typename N> struct is_complex : public std::false_type {};
32  template <typename N> struct is_complex<std::complex<N>> : public std::true_type {};
34 
35  template <typename T>
36  using real_part_t = typename real_part<T>::type;
37 
45  template <typename To, typename From, typename Enabled = void>
46  struct narrow_number {};
47 
49  template <>
50  struct narrow_number<float, double> {
51  float operator() (double x) { return x; }
52  };
53 
54  template <typename T>
55  struct narrow_number<T, T> {
56  T operator() (T x) { return x; }
57  };
58 
59  template <>
60  struct narrow_number<std::complex<float>, std::complex<double>> {
61  std::complex<float> operator() (std::complex<double> x) {
62  return std::complex<float>(x.real(), x.imag());
63  }
64  };
65 
66  template <typename To, typename From>
67  struct narrow_number<To, std::complex<From>,
68  std::enable_if_t<std::is_floating_point<To>::value>>
69  {
70  To operator() (std::complex<From> x) {
71  allium_assert(x.imag() == 0);
72  return narrow_number<To, From>()(x.real());
73  }
74  };
76 
81  template <typename T1, typename T2>
82  std::enable_if_t<std::is_signed<T1>::value && std::is_unsigned<T2>::value, bool>
83  safe_le(T1 a, T2 b) {
84  // b is always >= 0, hence a <= b if a < 0, otherwise a comparison is safe
85  return (a < 0) || (static_cast<std::make_unsigned_t<T1>>(a) <= b);
86  }
87  template <typename T1, typename T2>
88  std::enable_if_t<std::is_unsigned<T1>::value && std::is_signed<T2>::value, bool>
89  safe_le(T1 a, T2 b) {
90  // a is always >= 0, hence a <= b requires b >= 0, then a comparison is safe
91  return (b >= 0) && (a <= static_cast<std::make_unsigned_t<T2>>(b));
92  }
93  template <typename T1, typename T2>
94  std::enable_if_t<std::is_signed<T1>::value == std::is_signed<T2>::value, bool>
95  safe_le(T1 a, T2 b) {
96  // same signage, comparison is safe
97  return a <= b;
98  }
99 
104  template <typename T1, typename T2>
105  bool safe_ge(T1 a, T2 b) {
106  return safe_le(b, a);
107  }
108 
113  template <typename T1, typename T2>
114  std::enable_if_t<std::is_signed<T1>::value && std::is_unsigned<T2>::value, bool>
115  safe_lt(T1 a, T2 b) {
116  // b is always >= 0, hence a < b if a < 0, otherwise a comparison is safe
117  return (a < 0) || (static_cast<std::make_unsigned_t<T1>>(a) < b);
118  }
119  template <typename T1, typename T2>
120  std::enable_if_t<std::is_unsigned<T1>::value && std::is_signed<T2>::value, bool>
121  safe_lt(T1 a, T2 b) {
122  // a is always >= 0, hence a < b requires b >= 0, then a comparison is safe
123  return (b >= 0) && (a < static_cast<std::make_unsigned_t<T2>>(b));
124  }
125  template <typename T1, typename T2>
126  std::enable_if_t<std::is_signed<T1>::value == std::is_signed<T2>::value, bool>
127  safe_lt(T1 a, T2 b) {
128  // same signage, comparison is safe
129  return a < b;
130  }
131 
136  template <typename T1, typename T2>
137  bool safe_gt(T1 a, T2 b) {
138  return safe_lt(b, a);
139  }
140 
141 }
142 
143 #endif
Narrows a number type, which can result in a loss of information or accuracy.
Definition: numeric.hpp:46