quickstats
Quickly compute simple statistics
Loading...
Searching...
No Matches
utils.hpp
Go to the documentation of this file.
1#ifndef QUICKSTATS_UTILS_HPP
2#define QUICKSTATS_UTILS_HPP
3
4#include <type_traits>
5#include <cmath>
6#include <cstddef>
7#include <limits>
8
14namespace quickstats {
15
19template<typename Value_>
20using I = std::remove_cv_t<std::remove_reference_t<Value_> >;
21
22template<typename Value_>
23Value_ interpolate(const Value_ left, const Value_ right, const Value_ right_frac) {
24 if (std::isinf(left)) {
25 // If only left is +/-Inf, the sum will also be the same Inf.
26 // If both are Inf of the same sign, the sum will be the same Inf.
27 // If they are Inf of different signs, the sum will be a NaN.
28 return left + right;
29 } else {
30 // If only right is +/-Inf, this will also yield the same Inf.
31 // This assumes that right_frac > 0 otherwise there wouldn't be any need for interpolation.
32 return left + (right - left) * right_frac;
33 }
34}
35
36template<typename Output_>
37bool check_zeroed(const std::size_t len, const Output_* const ptr) {
38 for (std::size_t i = 0; i < len; ++i) {
39 if (ptr[i] != 0) {
40 return false;
41 }
42 }
43 return true;
44}
45
46template<typename Value_>
47constexpr Value_ inf_if_available_else_max() {
48 if constexpr(std::numeric_limits<Value_>::has_infinity) {
49 return std::numeric_limits<Value_>::infinity();
50 } else {
51 return std::numeric_limits<Value_>::max();
52 }
53}
62template<typename Value_>
63constexpr Value_ nan_if_available_else_zero() {
64 if constexpr(std::numeric_limits<Value_>::has_quiet_NaN) {
65 return std::numeric_limits<Value_>::quiet_NaN();
66 } else {
67 return 0;
68 }
69}
70
71}
72
73#endif
Quickly compute simple statistics.
Definition mad.hpp:15
constexpr Value_ nan_if_available_else_zero()
Definition utils.hpp:63