7#ifndef _MATH_UTILS_HPP_
8#define _MATH_UTILS_HPP_
20template <
class T>
inline int sgn(T v) {
21 return (v > T(0)) - (v < T(0));
25nearly_equal(
const double& a,
const double& b,
double epsilon = 1e-5) {
26 return std::fabs(a - b) < epsilon;
32template <
typename _Float>
33constexpr std::enable_if_t<
34 std::is_floating_point_v<_Float> &&
35 __cplusplus <= 201703L,
37lerp(_Float __a, _Float __b, _Float __t) {
38 if (std::isnan(__a) || std::isnan(__b) || std::isnan(__t))
39 return std::numeric_limits<_Float>::quiet_NaN();
40 else if ((__a <= _Float{0} && __b >= _Float{0}) ||
41 (__a >= _Float{0} && __b <= _Float{0}))
44 return __t * __b + (_Float{1} - __t) * __a;
46 return std::fma(__t, __b, (_Float{1} - __t) * __a);
48 else if (__t == _Float{1})
52 const auto __x = __a + __t * (__b - __a);
54 const auto __x = std::fma(__t, __b - __a, __a);
56 return (__t > _Float{1}) == (__b > __a) ? std::max(__b, __x)
Copyright 2020 Jonathan Bayless.
int sgn(T v)
Returns the sign value of the given value.
bool nearly_equal(const double &a, const double &b, double epsilon=1e-5)