From 66bf7c8f7194ecd3b9f6426ca3b4807e8a8996a2 Mon Sep 17 00:00:00 2001 From: Eric Fiselier Date: Thu, 12 Mar 2015 20:27:29 -0400 Subject: [PATCH] add floating point comparison warnings --- CMakeLists.txt | 1 + src/benchmark.cc | 4 ++-- src/stat.h | 15 ++++++++++----- test/benchmark_test.cc | 2 +- test/filter_test.cc | 8 ++++---- 5 files changed, 18 insertions(+), 12 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b4a36bd9..947239bc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -39,6 +39,7 @@ add_cxx_compiler_flag(-Wshadow) add_cxx_compiler_flag(-Werror) add_cxx_compiler_flag(-pedantic-errors) add_cxx_compiler_flag(-Wshorten-64-to-32) +add_cxx_compiler_flag(-Wfloat-equal) # TODO(ericwf): enable this for g++ #add_cxx_compiler_flag(-Wzero-as-null-pointer-constant) # Release flags diff --git a/src/benchmark.cc b/src/benchmark.cc index 8b0682e6..78eda597 100644 --- a/src/benchmark.cc +++ b/src/benchmark.cc @@ -665,11 +665,11 @@ void RunBenchmark(const benchmark::internal::Benchmark::Instance& b, (seconds >= FLAGS_benchmark_min_time) || (real_accumulated_time >= 5*FLAGS_benchmark_min_time)) { double bytes_per_second = 0; - if (total.bytes_processed > 0 && seconds != 0.0) { + if (total.bytes_processed > 0 && seconds > 0.0) { bytes_per_second = (total.bytes_processed / seconds); } double items_per_second = 0; - if (total.items_processed > 0 && seconds != 0.0) { + if (total.items_processed > 0 && seconds > 0.0) { items_per_second = (total.items_processed / seconds); } diff --git a/src/stat.h b/src/stat.h index 435743c6..c4ecfe8e 100644 --- a/src/stat.h +++ b/src/stat.h @@ -2,8 +2,10 @@ #define BENCHMARK_STAT_H_ #include -#include #include +#include +#include + namespace benchmark { @@ -13,10 +15,10 @@ class Stat1; template class Stat1MinMax; -typedef Stat1 Stat1_f; -typedef Stat1 Stat1_d; -typedef Stat1MinMax Stat1MinMax_f; -typedef Stat1MinMax Stat1MinMax_d; +typedef Stat1 Stat1_f; +typedef Stat1 Stat1_d; +typedef Stat1MinMax Stat1MinMax_f; +typedef Stat1MinMax Stat1MinMax_d; template class Vector2; @@ -133,6 +135,9 @@ class Stat1 { } private: + static_assert(std::is_integral::value && + !std::is_same::value, + "NumType must be an integral type that is not bool."); // Let i be the index of the samples provided (using +=) // and weight[i],value[i] be the data of sample #i // then the variables have the following meaning: diff --git a/test/benchmark_test.cc b/test/benchmark_test.cc index d44ea319..dd009af1 100644 --- a/test/benchmark_test.cc +++ b/test/benchmark_test.cc @@ -158,7 +158,7 @@ static void BM_LongTest(benchmark::State& state) { while (state.KeepRunning()) for (int i = 0; i < state.range_x(); ++i) tracker += i; - assert(tracker != 0.0); + assert(tracker > 1.0); } BENCHMARK(BM_LongTest)->Range(1<<16,1<<28); diff --git a/test/filter_test.cc b/test/filter_test.cc index 00c29553..c7258acf 100644 --- a/test/filter_test.cc +++ b/test/filter_test.cc @@ -1,8 +1,8 @@ #include "benchmark/benchmark.h" -#include -#include -#include +#include +#include +#include #include #include @@ -69,7 +69,7 @@ BENCHMARK(BM_CalculatePi)->ThreadPerCpu(); int main(int argc, const char* argv[]) { benchmark::Initialize(&argc, argv); - assert(CalculatePi(1) == 0.0); + assert(std::fabs(CalculatePi(1)) < 0.001); TestReporter test_reporter; benchmark::RunSpecifiedBenchmarks(&test_reporter);