23 #ifndef GUL14_NUM_UTIL_H_
24 #define GUL14_NUM_UTIL_H_
29 #include <type_traits>
52 template<
typename ValueT>
53 constexpr
auto abs(ValueT n) noexcept -> std::enable_if_t<std::is_unsigned<ValueT>::value, ValueT>
61 template<
typename ValueT>
62 constexpr
auto abs(ValueT n) noexcept -> std::enable_if_t<not std::is_unsigned<ValueT>::value, ValueT>
92 template<
typename NumT,
typename OrderT,
93 typename = std::enable_if_t<
94 std::is_arithmetic<NumT>::value
95 and std::is_arithmetic<OrderT>::value
97 bool within_orders(
const NumT a,
const NumT b,
const OrderT orders) noexcept(
false) {
100 if (difference == NumT{ 0 })
103 auto limit =
maximum / std::pow(
static_cast<std::decay_t<NumT>
>(10.0), orders);
104 return difference < limit;
118 template<
typename NumT>
123 if (std::is_floating_point<NumT>::value)
128 if (std::is_floating_point<NumT>::value)
152 template<
typename NumT,
153 typename = std::enable_if_t<std::is_floating_point<NumT>::value>>
162 if (std::isnormal(maxval))
164 return diff <= std::numeric_limits<NumT>::epsilon()
165 *
static_cast<NumT
>(ulp) * maxval;
169 if (std::isfinite(maxval))
170 return diff <= std::numeric_limits<NumT>::denorm_min() *
static_cast<NumT
>(ulp);
208 constexpr
const NumT&
clamp(
const NumT& v,
const NumT& lo,
const NumT& hi) {
209 return std::max(lo, std::min(v, hi));
234 template<
class NumT,
class Compare>
235 constexpr
const NumT&
clamp(
const NumT& v,
const NumT& lo,
const NumT& hi, Compare comp) {
constexpr auto abs(ValueT n) noexcept -> std::enable_if_t< std::is_unsigned< ValueT >::value, ValueT >
Compute the absolute value of a number.
Definition: num_util.h:53
bool within_abs(NumT a, NumT b, NumT tol) noexcept
Determine if two numbers are almost equal, allowing for an absolute difference.
Definition: num_util.h:119
bool within_ulp(NumT a, NumT b, unsigned int ulp)
Determine if two numbers are almost equal, allowing for a difference of a given number of units-in-th...
Definition: num_util.h:154
bool within_orders(const NumT a, const NumT b, const OrderT orders) noexcept(false)
Determine if two numbers are almost equal, comparing only some significant digits.
Definition: num_util.h:97
constexpr const NumT & clamp(const NumT &v, const NumT &lo, const NumT &hi)
Coerce a value to be within a given range.
Definition: num_util.h:208
auto maximum(ContainerT const &container, Accessor accessor=ElementAccessor< ElementT >()) -> DataT
Return the maximum element value in a container.
Definition: statistics.h:314
Definition of macros used internally by GUL.
Namespace gul14 contains all functions and classes of the General Utility Library.
Definition: doxygen.h:26