18 #ifndef vtkMathUtilities_h
19 #define vtkMathUtilities_h
21 #include "vtkABINamespace.h"
28 #include <type_traits>
32 VTK_ABI_NAMESPACE_BEGIN
39 bool FuzzyCompare(A a, A b, A epsilon = std::numeric_limits<A>::epsilon())
41 return fabs(a - b) < epsilon;
57 if ((a == static_cast<A>(0)) ||
58 ((b > static_cast<A>(1)) && (a < b * std::numeric_limits<A>::min())))
60 return static_cast<A
>(0);
73 bool NearlyEqual(A a, A b, A tol = std::numeric_limits<A>::epsilon())
75 A absdiff = fabs(a - b);
76 A d1 = vtkMathUtilities::SafeDivision<A>(absdiff, fabs(a));
77 A d2 = vtkMathUtilities::SafeDivision<A>(absdiff, fabs(b));
79 return ((d1 <= tol) || (d2 <= tol));
108 if constexpr (std::is_floating_point_v<A>)
120 else if (value > max)
122 min = std::min(min, value);
135 if constexpr (std::is_floating_point_v<A>)
147 else if (value > max)
149 min = std::min(min, value);
162 if constexpr (std::is_floating_point_v<A>)
164 return static_cast<A
>(
value);
168 constexpr
double typeMin =
static_cast<double>(std::numeric_limits<A>::lowest());
178 return static_cast<A
>(
value);
186 return std::numeric_limits<A>::lowest();
190 VTK_ABI_NAMESPACE_END
193 #endif // vtkMathUtilities_h
bool FuzzyCompare(A a, A b, A epsilon=std::numeric_limits< A >::epsilon())
Perform a fuzzy compare of floats/doubles, specify the allowed tolerance NB: this uses an absolute to...
void UpdateRangeFinite(A &min, A &max, const A &value)
Update an existing min - max range with a new prospective value.
bool NearlyEqual(A a, A b, A tol=std::numeric_limits< A >::epsilon())
A slightly different fuzzy comparator that checks if two values are "nearly" equal based on Knuth...
#define VTK_UNLIKELY(cond)
Hint that cond is expected to be false most of the time.
void UpdateRange(A &min, A &max, const A &value)
Update an existing min - max range with a new prospective value.
#define VTK_LIKELY(cond)
Hint that cond is expected to be true most of the time.
Compiler hint macros for assumptions and branch prediction.
A SafeDivision(A a, A b)
Performs safe division that catches overflow and underflow.
A SafeCastFromDouble(double value)
Cast from double, and clamp to output type limits to avoid overlow.