23 #ifndef EDSP_NUMERIC_HPP 24 #define EDSP_NUMERIC_HPP 31 namespace edsp {
inline namespace math {
70 const auto tmp =
static_cast<std::int32_t
>(x);
71 return tmp != 0 && !(tmp & (tmp - 1));
81 return std::fpclassify(x) == FP_SUBNORMAL;
91 return std::fpclassify(x) == FP_NORMAL;
101 return std::fpclassify(x) == FP_ZERO;
109 template <
typename T>
111 return std::isnan(x);
119 template <
typename T>
121 return std::fpclassify(x) == FP_INFINITE;
129 template <
typename T>
131 auto n =
static_cast<std::uint64_t
>(x);
142 if ((n % r) == 0 || (n % (r + 2)) == 0)
154 template <
typename T>
155 constexpr T
sign(T x) noexcept {
156 return is_negative(x) ?
static_cast<T
>(-1) : static_cast<T>(1);
164 template <
typename T>
171 auto _x =
static_cast<int32_t
>(x);
178 return static_cast<T
>(_x + 1);
187 template <
typename T>
197 template <
typename T>
207 template <
typename T>
209 return static_cast<T
>(1) / x;
217 template <
typename T>
230 template <
typename T>
231 inline typename std::enable_if<std::is_floating_point<T>::value, T>::type
rand(T
min, T
max) {
232 std::random_device rd;
233 std::mt19937 eng(rd());
234 std::uniform_real_distribution<T> distribution(min, max);
235 return distribution(eng);
243 template <
typename T>
244 inline typename std::enable_if<std::is_floating_point<T>::value, T>::type
rand() {
245 std::random_device rd;
246 std::mt19937 eng(rd());
248 return distribution(eng);
259 template <
typename T>
260 inline typename std::enable_if<std::is_integral<T>::value, T>::type
rand(T
min, T
max) {
261 std::random_device rd;
262 std::mt19937 eng(rd());
263 std::uniform_int_distribution<T> distribution(min, max);
264 return distribution(eng);
272 template <
typename T>
273 inline typename std::enable_if<std::is_integral<T>::value, T>::type
rand() {
274 std::random_device rd;
275 std::mt19937 eng(rd());
277 return distribution(eng);
289 template <distances d>
294 template <
typename T>
295 constexpr T operator()(T x, T y) noexcept {
302 template <
typename T>
303 constexpr T operator()(T x, T y) noexcept {
311 template <
typename T>
312 constexpr T operator()(T x, T y) noexcept {
313 return std::log(std::abs(x) / std::abs(y));
327 template <distances d,
typename T>
329 return internal::distance<d>{}(x, y);
336 #endif //EDSP_NUMERIC_HPP constexpr meta::value_type_t< ForwardIt > max(ForwardIt first, ForwardIt last)
Computes the maximum value of the range [first, last)
Definition: max.hpp:38
constexpr T half(T x)
Computes the half value of the input number.
Definition: numeric.hpp:218
constexpr bool is_even(T x)
Determines if the number is even.
Definition: numeric.hpp:59
constexpr bool is_denormal(T x)
Determines if the number is denormal floating-point.
Definition: numeric.hpp:80
constexpr auto is_inf(const std::complex< T > &z) noexcept
Determines if the given real or imaginary part of the complex number is .
Definition: complex.hpp:100
distances
Definition: numeric.hpp:280
constexpr void floor(InputIt first, InputIt last, OutputIt d_first)
For each element in the range [first, last) computes the largest integer value not greater than the e...
Definition: floor.hpp:40
constexpr bool is_power_two(T x)
Determines if the number is power of two.
Definition: numeric.hpp:69
constexpr T next_power_two(T x)
Computes the closest next higher power of 2 of the input number.
Definition: numeric.hpp:165
constexpr bool is_normal(T x)
Determines if the number is normal floating-point.
Definition: numeric.hpp:90
constexpr T inv(T x)
Computes the inverse value of the input number.
Definition: numeric.hpp:208
std::enable_if< std::is_floating_point< T >::value, T >::type rand(T min, T max)
Computes a random number in the range [min, max].
Definition: numeric.hpp:231
constexpr bool is_odd(T x)
Determines if the number is odd.
Definition: numeric.hpp:49
constexpr T distance(T x, T y) noexcept
Computes the distance between x and y.
Definition: numeric.hpp:328
Definition: numeric.hpp:281
constexpr bool is_zero(T x)
Determines if the number is zero.
Definition: numeric.hpp:100
constexpr T square(T x)
Computes the square value of the input number.
Definition: numeric.hpp:188
Definition: numeric.hpp:283
constexpr bool is_negative(T x)
Determines if the number is negative.
Definition: numeric.hpp:39
constexpr auto is_nan(const std::complex< T > &z) noexcept
Determines if the given real or imaginary part of the complex number is a not-a-number (NaN) value...
Definition: complex.hpp:90
constexpr meta::value_type_t< ForwardIt > min(ForwardIt first, ForwardIt last)
Computes the minimum value of the range [first, last)
Definition: min.hpp:38
constexpr T fract(T x)
Computes the fractional part of the input number.
Definition: numeric.hpp:198
Definition: numeric.hpp:282
constexpr T sign(T x) noexcept
Determines the sign of the input number.
Definition: numeric.hpp:155
Definition: amplifier.hpp:29
constexpr bool is_prime(T x)
Determines if the number is prime.
Definition: numeric.hpp:130