/** * Check if two points u and v are the same point on XY dimension. * @param u one point that has member function x() and y(). * @param v one point that has member function x() and y(). * @return sqrt((u.x-v.x)^2 + (u.y-v.y)^2) < epsilon, i.e., the Euclid distance * on XY dimension. */ template <typename U, typename V> boolSamePointXY(const U& u, const V& v) { staticconstexprdouble kMathEpsilonSqr = 1e-8 * 1e-8; return (u.x() - v.x()) * (u.x() - v.x()) < kMathEpsilonSqr && (u.y() - v.y()) * (u.y() - v.y()) < kMathEpsilonSqr; }
// Test whether two float or double numbers are equal. template <typename T> typename std::enable_if<!std::numeric_limits<T>::is_integer, bool>::type IsFloatEqual(T x, T y, int ulp = 2) { // the machine epsilon has to be scaled to the magnitude of the values used // and multiplied by the desired precision in ULPs (units in the last place) return std::fabs(x - y) < std::numeric_limits<T>::epsilon() * std::fabs(x + y) * ulp // unless the result is subnormal || std::fabs(x - y) < std::numeric_limits<T>::min(); }