From 1c4fdad7bd6ffcb6e6f6fbbc00b01585d71062d3 Mon Sep 17 00:00:00 2001 From: Gael Guennebaud Date: Wed, 11 Apr 2018 15:24:13 +0200 Subject: [PATCH] bug #1520: workaround some -Wfloat-equal warnings by calling std::equal_to --- Eigen/src/Core/MathFunctions.h | 2 +- Eigen/src/Core/arch/CUDA/Half.h | 4 ++-- Eigen/src/Core/util/Meta.h | 20 ++++++++++++++++++++ 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/Eigen/src/Core/MathFunctions.h b/Eigen/src/Core/MathFunctions.h index 0257b3f4f..6eb974d41 100644 --- a/Eigen/src/Core/MathFunctions.h +++ b/Eigen/src/Core/MathFunctions.h @@ -471,7 +471,7 @@ namespace std_fallback { typedef typename NumTraits::Real RealScalar; EIGEN_USING_STD_MATH(log); Scalar x1p = RealScalar(1) + x; - return ( x1p == Scalar(1) ) ? x : x * ( log(x1p) / (x1p - RealScalar(1)) ); + return numext::equal_strict(x1p, Scalar(1)) ? x : x * ( log(x1p) / (x1p - RealScalar(1)) ); } } diff --git a/Eigen/src/Core/arch/CUDA/Half.h b/Eigen/src/Core/arch/CUDA/Half.h index d9b3cb9b1..02ac0c23a 100644 --- a/Eigen/src/Core/arch/CUDA/Half.h +++ b/Eigen/src/Core/arch/CUDA/Half.h @@ -238,10 +238,10 @@ EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half& operator /= (half& a, const half& b) return a; } EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool operator == (const half& a, const half& b) { - return float(a) == float(b); + return numext::equal_strict(float(a),float(b)); } EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool operator != (const half& a, const half& b) { - return float(a) != float(b); + return numext::not_equal_strict(float(a), float(b)); } EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool operator < (const half& a, const half& b) { return float(a) < float(b); diff --git a/Eigen/src/Core/util/Meta.h b/Eigen/src/Core/util/Meta.h index 7f6370755..4e4ea9ab1 100755 --- a/Eigen/src/Core/util/Meta.h +++ b/Eigen/src/Core/util/Meta.h @@ -485,6 +485,26 @@ T div_ceil(const T &a, const T &b) return (a+b-1) / b; } +// The aim of the following functions is to bypass -Wfloat-equal warnings +// when we really want a strict equality comparison on floating points. +template +bool equal_strict(const X& x,const Y& y) { return x == y; } + +template<> +bool equal_strict(const float& x,const float& y) { return std::equal_to()(x,y); } + +template<> +bool equal_strict(const double& x,const double& y) { return std::equal_to()(x,y); } + +template +bool not_equal_strict(const X& x,const Y& y) { return x != y; } + +template<> +bool not_equal_strict(const float& x,const float& y) { return std::not_equal_to()(x,y); } + +template<> +bool not_equal_strict(const double& x,const double& y) { return std::not_equal_to()(x,y); } + } // end namespace numext } // end namespace Eigen