mirror of
https://github.com/catchorg/Catch2.git
synced 2025-08-03 13:55:39 +02:00
Add Wfloat-equal to default-enabled warnings
This is kinda messy, because there is no good way to signal to the compiler that some code uses direct comparison of floating point numbers intentionally, so instead we have to use diagnostic pragmas. We also have to over-suppress the test files, because Clang (and possibly GCC) still issue warnings from template instantiation even if the instantion site is under warning suppression, so the template definition has to be under warning suppression as well. Closes #2406
This commit is contained in:
@@ -137,6 +137,15 @@ namespace Catch {
|
||||
namespace Benchmark {
|
||||
namespace Detail {
|
||||
|
||||
#if defined( __GNUC__ ) || defined( __clang__ )
|
||||
# pragma GCC diagnostic push
|
||||
# pragma GCC diagnostic ignored "-Wfloat-equal"
|
||||
#endif
|
||||
bool directCompare( double lhs, double rhs ) { return lhs == rhs; }
|
||||
#if defined( __GNUC__ ) || defined( __clang__ )
|
||||
# pragma GCC diagnostic pop
|
||||
#endif
|
||||
|
||||
double weighted_average_quantile(int k, int q, std::vector<double>::iterator first, std::vector<double>::iterator last) {
|
||||
auto count = last - first;
|
||||
double idx = (count - 1) * k / static_cast<double>(q);
|
||||
@@ -144,7 +153,9 @@ namespace Catch {
|
||||
double g = idx - j;
|
||||
std::nth_element(first, first + j, last);
|
||||
auto xj = first[j];
|
||||
if (g == 0) return xj;
|
||||
if ( directCompare( g, 0 ) ) {
|
||||
return xj;
|
||||
}
|
||||
|
||||
auto xj1 = *std::min_element(first + (j + 1), last);
|
||||
return xj + g * (xj1 - xj);
|
||||
|
@@ -24,6 +24,10 @@ namespace Catch {
|
||||
namespace Detail {
|
||||
using sample = std::vector<double>;
|
||||
|
||||
// Used when we know we want == comparison of two doubles
|
||||
// to centralize warning suppression
|
||||
bool directCompare( double lhs, double rhs );
|
||||
|
||||
double weighted_average_quantile(int k, int q, std::vector<double>::iterator first, std::vector<double>::iterator last);
|
||||
|
||||
template <typename Iterator>
|
||||
@@ -103,7 +107,9 @@ namespace Catch {
|
||||
long n = static_cast<long>(resample.size());
|
||||
double prob_n = std::count_if(resample.begin(), resample.end(), [point](double x) { return x < point; }) / static_cast<double>(n);
|
||||
// degenerate case with uniform samples
|
||||
if (prob_n == 0) return { point, point, point, confidence_level };
|
||||
if ( directCompare( prob_n, 0. ) ) {
|
||||
return { point, point, point, confidence_level };
|
||||
}
|
||||
|
||||
double bias = normal_quantile(prob_n);
|
||||
double z1 = normal_quantile((1. - confidence_level) / 2.);
|
||||
|
@@ -10,6 +10,7 @@
|
||||
|
||||
#include <catch2/internal/catch_polyfills.hpp>
|
||||
|
||||
#include <cassert>
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <utility>
|
||||
@@ -24,6 +25,15 @@ namespace Catch {
|
||||
} // end namespace Detail
|
||||
|
||||
|
||||
|
||||
#if defined( __GNUC__ ) || defined( __clang__ )
|
||||
# pragma GCC diagnostic push
|
||||
// We do a bunch of direct compensations of floating point numbers,
|
||||
// because we know what we are doing and actually do want the direct
|
||||
// comparison behaviour.
|
||||
# pragma GCC diagnostic ignored "-Wfloat-equal"
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Calculates the ULP distance between two floating point numbers
|
||||
*
|
||||
@@ -83,6 +93,11 @@ namespace Catch {
|
||||
return lc - rc;
|
||||
}
|
||||
|
||||
#if defined( __GNUC__ ) || defined( __clang__ )
|
||||
# pragma GCC diagnostic pop
|
||||
#endif
|
||||
|
||||
|
||||
} // end namespace Catch
|
||||
|
||||
#endif // CATCH_FLOATING_POINT_HELPERS_HPP_INCLUDED
|
||||
|
Reference in New Issue
Block a user