16 #ifdef BITS_MATH_UTILS_H
29 assert(tol >= zero<T>() &&
"positive_tolerance");
30 return std::abs(a - b) <= tol;
38 if (std::abs(a - b) < maxAbsError)
41 if (std::abs(b) > std::abs(a))
42 relativeError = std::abs((a - b) / b);
44 relativeError = std::abs((a - b) / a);
45 if (relativeError <= maxRelError)
52 typename __impl::TypeTraits<T>::IntegralType_t maxDistInts,
55 typedef typename __impl::TypeTraits<T> Traits_t;
56 typedef typename __impl::TypeTraits<T>::IntegralType_t TraitsInt_t;
58 assert(0 < maxDistInts &&
"positive_and_small_enough");
61 if ((checkFlags &
NoCheck) == 0) {
83 TraitsInt_t aAsInt = *(TraitsInt_t*)(&a);
86 aAsInt = Traits_t::twoComplementValue() - aAsInt;
88 TraitsInt_t bAsInt = *(TraitsInt_t*)(&b);
91 bAsInt = Traits_t::twoComplementValue() - bAsInt;
93 return std::abs(static_cast<double>(aAsInt - bAsInt)) <= maxDistInts;
97 T
clamped(
const T& v,
const T& min,
const T& max)
99 return v < min ? min : (v > max ? max : v);
107 return (static_cast<double>(angle) * 180.) /
math::pi;
113 return (
math::pi * static_cast<double>(angle)) / 180.;
123 typedef typename __impl::TypeTraits<T> Traits_t;
124 typedef typename __impl::TypeTraits<T>::IntegralType_t TraitsInt_t;
125 if (Traits_t::isNativeIntegralType())
126 return v > 0 ? 1 : -1;
128 return ((*(TraitsInt_t*)&v) & Traits_t::twoComplementValue()) >= 0
141 return static_cast<T
>(0);
170 #endif // BITS_MATH_UTILS_H
double degreeToRadian(const T &angle)
T clamped(const T &v, const T &min, const T &max)
bool equalByIntDiff(const T &a, const T &b, typename __impl::TypeTraits< T >::IntegralType_t maxDistInts=10, ComparisonCheckFlags checkFlags=NoCheck)
const double pi
Definition: consts.h:20
bool equalByRelError(const T &a, const T &b, const T &maxRelError=static_cast< T >(1e-5), const T &maxAbsError=std::numeric_limits< T >::epsilon())
bool equalByAbsError(const T &a, const T &b, const T &tol=static_cast< T >(1e-6))
double radianToDegree(const T &angle)
ComparisonCheckFlags
Definition: utils.h:28