From 66842660c0bad0ce80c6db5fe68c7d7e4ec00be4 Mon Sep 17 00:00:00 2001 From: Hans Dembinski Date: Thu, 25 Apr 2024 12:51:47 +0200 Subject: [PATCH] fix some false positive warnings emitted by msvc (#399) * fix some false positive warnings emitted by msvc * make find_package for Python3 more specific --- include/boost/histogram/axis/integer.hpp | 2 +- .../utility/clopper_pearson_interval.hpp | 10 +-- .../histogram/utility/jeffreys_interval.hpp | 13 ++-- .../boost/histogram/utility/wald_interval.hpp | 3 +- .../histogram/utility/wilson_interval.hpp | 3 +- test/CMakeLists.txt | 4 +- test/accumulators_count_test.cpp | 2 +- test/accumulators_fraction_test.cpp | 36 +++++----- test/algorithm_reduce_test.cpp | 56 +++++++-------- .../utility_clopper_pearson_interval_test.cpp | 2 +- test/utility_jeffreys_interval_test.cpp | 68 +++++++++---------- 11 files changed, 102 insertions(+), 97 deletions(-) diff --git a/include/boost/histogram/axis/integer.hpp b/include/boost/histogram/axis/integer.hpp index 0a1b9698..47fe40ca 100644 --- a/include/boost/histogram/axis/integer.hpp +++ b/include/boost/histogram/axis/integer.hpp @@ -112,7 +112,7 @@ class integer : public iterator_mixin>, /// Returns index and shift (if axis has grown) for the passed argument. auto update(value_type x) noexcept { auto impl = [this](long x) -> std::pair { - const auto i = x - min_; + const auto i = static_cast(x) - min_; if (i >= 0) { const auto k = static_cast(i); if (k < size()) return {k, 0}; diff --git a/include/boost/histogram/utility/clopper_pearson_interval.hpp b/include/boost/histogram/utility/clopper_pearson_interval.hpp index 46285238..947c9464 100644 --- a/include/boost/histogram/utility/clopper_pearson_interval.hpp +++ b/include/boost/histogram/utility/clopper_pearson_interval.hpp @@ -54,13 +54,13 @@ class clopper_pearson_interval : public binomial_proportion_interval @param successes Number of successful trials. @param failures Number of failed trials. */ - interval_type operator()(value_type successes, value_type failures) const noexcept { + interval_type operator()(value_type successes, value_type failures) const noexcept override { // analytical solution when successes or failures are zero // T. Mans (2014), Electronic Journal of Statistics. 8 (1): 817-840. // arXiv:1303.1288. doi:10.1214/14-EJS909. - const value_type total = successes + failures; - if (successes == 0) return {0, 1 - std::pow(alpha_half_, 1 / total)}; - if (failures == 0) return {std::pow(alpha_half_, 1 / total), 1}; + const value_type one{1.0}, zero{0.0}, total{successes + failures}; + if (successes == 0) return {zero, one - std::pow(alpha_half_, one / total)}; + if (failures == 0) return {std::pow(alpha_half_, one / total), one}; // Source: // https://en.wikipedia.org/wiki/ @@ -68,7 +68,7 @@ class clopper_pearson_interval : public binomial_proportion_interval math::beta_distribution beta_a(successes, failures + 1); const value_type a = math::quantile(beta_a, alpha_half_); math::beta_distribution beta_b(successes + 1, failures); - const value_type b = math::quantile(beta_b, 1 - alpha_half_); + const value_type b = math::quantile(beta_b, one - alpha_half_); return {a, b}; } diff --git a/include/boost/histogram/utility/jeffreys_interval.hpp b/include/boost/histogram/utility/jeffreys_interval.hpp index 317bd2a5..fcd0191a 100644 --- a/include/boost/histogram/utility/jeffreys_interval.hpp +++ b/include/boost/histogram/utility/jeffreys_interval.hpp @@ -51,19 +51,20 @@ class jeffreys_interval : public binomial_proportion_interval { @param successes Number of successful trials. @param failures Number of failed trials. */ - interval_type operator()(value_type successes, value_type failures) const noexcept { + interval_type operator()(value_type successes, + value_type failures) const noexcept override { // See L.D. Brown, T.T. Cai, A. DasGupta, Statistical Science 16 (2001) 101-133, // doi:10.1214/ss/1009213286, section 4.1.2. - const value_type half{0.5}; + const value_type half{0.5}, one{1.0}, zero{0.0}; const value_type total = successes + failures; // if successes or failures are 0, modified interval is equal to Clopper-Pearson - if (successes == 0) return {0, 1 - std::pow(alpha_half_, 1 / total)}; - if (failures == 0) return {std::pow(alpha_half_, 1 / total), 1}; + if (successes == 0) return {zero, one - std::pow(alpha_half_, one / total)}; + if (failures == 0) return {std::pow(alpha_half_, one / total), one}; math::beta_distribution beta(successes + half, failures + half); - const value_type a = successes == 1 ? 0 : math::quantile(beta, alpha_half_); - const value_type b = failures == 1 ? 1 : math::quantile(beta, 1 - alpha_half_); + const value_type a = successes == 1 ? zero : math::quantile(beta, alpha_half_); + const value_type b = failures == 1 ? one : math::quantile(beta, one - alpha_half_); return {a, b}; } diff --git a/include/boost/histogram/utility/wald_interval.hpp b/include/boost/histogram/utility/wald_interval.hpp index efea20d2..3c771ce7 100644 --- a/include/boost/histogram/utility/wald_interval.hpp +++ b/include/boost/histogram/utility/wald_interval.hpp @@ -59,7 +59,8 @@ class wald_interval : public binomial_proportion_interval { @param successes Number of successful trials. @param failures Number of failed trials. */ - interval_type operator()(value_type successes, value_type failures) const noexcept { + interval_type operator()(value_type successes, + value_type failures) const noexcept override { // See https://en.wikipedia.org/wiki/ // Binomial_proportion_confidence_interval // #Normal_approximation_interval_or_Wald_interval diff --git a/include/boost/histogram/utility/wilson_interval.hpp b/include/boost/histogram/utility/wilson_interval.hpp index 98fbf123..f2450f96 100644 --- a/include/boost/histogram/utility/wilson_interval.hpp +++ b/include/boost/histogram/utility/wilson_interval.hpp @@ -58,7 +58,8 @@ class wilson_interval : public binomial_proportion_interval { @param successes Number of successful trials. @param failures Number of failed trials. */ - interval_type operator()(value_type successes, value_type failures) const noexcept { + interval_type operator()(value_type successes, + value_type failures) const noexcept override { // See https://en.wikipedia.org/wiki/ // Binomial_proportion_confidence_interval // #Wilson_score_interval diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 7823d897..6368117f 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -2,13 +2,15 @@ # Distributed under the Boost Software License, Version 1.0. # See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt -find_package(Python3) +find_package(Python3 COMPONENTS Interpreter) if (Python3_FOUND) # checks that b2 and cmake are in sync add_test(NAME runpy-${PROJECT_NAME}_check_build_system COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/check_build_system.py) # checks that all headers are included in odr test add_test(NAME runpy-${PROJECT_NAME}_check_odr_test COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/check_odr_test.py) +else() +MESSAGE(WARNING "Python interpreter not found, cannot check for odr violations and build system consistency") endif() include(BoostTest OPTIONAL RESULT_VARIABLE HAVE_BOOST_TEST) diff --git a/test/accumulators_count_test.cpp b/test/accumulators_count_test.cpp index f8389439..66e90be2 100644 --- a/test/accumulators_count_test.cpp +++ b/test/accumulators_count_test.cpp @@ -20,7 +20,7 @@ void run_tests() { { c_t c; ++c; - BOOST_TEST_EQ(c.value(), 1); + BOOST_TEST_EQ(c.value(), static_cast(1)); BOOST_TEST_EQ(str(c), "1"s); BOOST_TEST_EQ(str(c, 2, false), " 1"s); BOOST_TEST_EQ(str(c, 2, true), "1 "s); diff --git a/test/accumulators_fraction_test.cpp b/test/accumulators_fraction_test.cpp index 52885b76..f6118e8f 100644 --- a/test/accumulators_fraction_test.cpp +++ b/test/accumulators_fraction_test.cpp @@ -25,8 +25,8 @@ void run_tests() { { f_t f; - BOOST_TEST_EQ(f.successes(), 0); - BOOST_TEST_EQ(f.failures(), 0); + BOOST_TEST_EQ(f.successes(), static_cast(0)); + BOOST_TEST_EQ(f.failures(), static_cast(0)); BOOST_TEST(std::isnan(f.value())); BOOST_TEST(std::isnan(f.variance())); @@ -38,22 +38,22 @@ void run_tests() { { f_t f; f(true); - BOOST_TEST_EQ(f.successes(), 1); - BOOST_TEST_EQ(f.failures(), 0); + BOOST_TEST_EQ(f.successes(), static_cast(1)); + BOOST_TEST_EQ(f.failures(), static_cast(0)); BOOST_TEST_EQ(str(f), "fraction(1, 0)"s); f(false); - BOOST_TEST_EQ(f.successes(), 1); - BOOST_TEST_EQ(f.failures(), 1); + BOOST_TEST_EQ(f.successes(), static_cast(1)); + BOOST_TEST_EQ(f.failures(), static_cast(1)); BOOST_TEST_EQ(str(f), "fraction(1, 1)"s); BOOST_TEST_EQ(str(f, 20), "fraction(1, 1) "s); } { f_t f(3, 1); - BOOST_TEST_EQ(f.successes(), 3); - BOOST_TEST_EQ(f.failures(), 1); - BOOST_TEST_EQ(f.value(), 0.75); - BOOST_TEST_IS_CLOSE(f.variance(), 0.75 * (1 - 0.75) / 4, eps); + BOOST_TEST_EQ(f.successes(), static_cast(3)); + BOOST_TEST_EQ(f.failures(), static_cast(1)); + BOOST_TEST_EQ(f.value(), 0.75f); + BOOST_TEST_IS_CLOSE(f.variance(), 0.75f * (1.0f - 0.75f) / 4.f, eps); const auto ci = f.confidence_interval(); const auto expected = utility::wilson_interval()(3, 1); @@ -63,10 +63,10 @@ void run_tests() { { f_t f(0, 1); - BOOST_TEST_EQ(f.successes(), 0); - BOOST_TEST_EQ(f.failures(), 1); - BOOST_TEST_EQ(f.value(), 0); - BOOST_TEST_EQ(f.variance(), 0); + BOOST_TEST_EQ(f.successes(), static_cast(0)); + BOOST_TEST_EQ(f.failures(), static_cast(1)); + BOOST_TEST_EQ(f.value(), 0.f); + BOOST_TEST_EQ(f.variance(), 0.f); const auto ci = f.confidence_interval(); const auto expected = utility::wilson_interval()(0, 1); @@ -76,10 +76,10 @@ void run_tests() { { f_t f(1, 0); - BOOST_TEST_EQ(f.successes(), 1); - BOOST_TEST_EQ(f.failures(), 0); - BOOST_TEST_EQ(f.value(), 1); - BOOST_TEST_EQ(f.variance(), 0); + BOOST_TEST_EQ(f.successes(), static_cast(1)); + BOOST_TEST_EQ(f.failures(), static_cast(0)); + BOOST_TEST_EQ(f.value(), 1.f); + BOOST_TEST_EQ(f.variance(), 0.f); const auto ci = f.confidence_interval(); const auto expected = utility::wilson_interval()(1, 0); diff --git a/test/algorithm_reduce_test.cpp b/test/algorithm_reduce_test.cpp index 0e4275c8..49608318 100644 --- a/test/algorithm_reduce_test.cpp +++ b/test/algorithm_reduce_test.cpp @@ -75,39 +75,39 @@ void run_tests() { // - lower edge of shrink: pick bin which contains edge, lower <= x < upper // - upper edge of shrink: pick bin which contains edge + 1, lower < x <= upper { - auto h = make(Tag(), ID(0, 3)); + auto h = make(Tag(), ID(0.0, 3.0)); const auto& ax = h.axis(); - BOOST_TEST_EQ(ax.value(0), 0); - BOOST_TEST_EQ(ax.value(3), 3); - BOOST_TEST_EQ(ax.index(-1), -1); - BOOST_TEST_EQ(ax.index(3), 3); - - BOOST_TEST_EQ(reduce(h, shrink(-1, 5)).axis(), ID(0, 3)); - BOOST_TEST_EQ(reduce(h, shrink(0, 3)).axis(), ID(0, 3)); - BOOST_TEST_EQ(reduce(h, shrink(1, 3)).axis(), ID(1, 3)); - BOOST_TEST_EQ(reduce(h, shrink(1.001, 3)).axis(), ID(1, 3)); - BOOST_TEST_EQ(reduce(h, shrink(1.999, 3)).axis(), ID(1, 3)); - BOOST_TEST_EQ(reduce(h, shrink(2, 3)).axis(), ID(2, 3)); - BOOST_TEST_EQ(reduce(h, shrink(0, 2.999)).axis(), ID(0, 3)); - BOOST_TEST_EQ(reduce(h, shrink(0, 2.001)).axis(), ID(0, 3)); - BOOST_TEST_EQ(reduce(h, shrink(0, 2)).axis(), ID(0, 2)); - BOOST_TEST_EQ(reduce(h, shrink(0, 1.999)).axis(), ID(0, 2)); - - BOOST_TEST_EQ(reduce(h, crop(-1, 5)).axis(), ID(0, 3)); - BOOST_TEST_EQ(reduce(h, crop(0, 3)).axis(), ID(0, 3)); - BOOST_TEST_EQ(reduce(h, crop(1, 3)).axis(), ID(1, 3)); - BOOST_TEST_EQ(reduce(h, crop(1.001, 3)).axis(), ID(1, 3)); - BOOST_TEST_EQ(reduce(h, crop(1.999, 3)).axis(), ID(1, 3)); - BOOST_TEST_EQ(reduce(h, crop(2, 3)).axis(), ID(2, 3)); - BOOST_TEST_EQ(reduce(h, crop(0, 2.999)).axis(), ID(0, 3)); - BOOST_TEST_EQ(reduce(h, crop(0, 2.001)).axis(), ID(0, 3)); - BOOST_TEST_EQ(reduce(h, crop(0, 2)).axis(), ID(0, 2)); - BOOST_TEST_EQ(reduce(h, crop(0, 1.999)).axis(), ID(0, 2)); + BOOST_TEST_EQ(ax.value(0), 0.0); + BOOST_TEST_EQ(ax.value(3), 3.0); + BOOST_TEST_EQ(ax.index(-1.0), -1); + BOOST_TEST_EQ(ax.index(3.0), 3); + + BOOST_TEST_EQ(reduce(h, shrink(-1, 5)).axis(), ID(0.0, 3.0)); + BOOST_TEST_EQ(reduce(h, shrink(0, 3)).axis(), ID(0.0, 3.0)); + BOOST_TEST_EQ(reduce(h, shrink(1, 3)).axis(), ID(1.0, 3.0)); + BOOST_TEST_EQ(reduce(h, shrink(1.001, 3)).axis(), ID(1.0, 3.0)); + BOOST_TEST_EQ(reduce(h, shrink(1.999, 3)).axis(), ID(1.0, 3.0)); + BOOST_TEST_EQ(reduce(h, shrink(2, 3)).axis(), ID(2.0, 3.0)); + BOOST_TEST_EQ(reduce(h, shrink(0, 2.999)).axis(), ID(0.0, 3.0)); + BOOST_TEST_EQ(reduce(h, shrink(0, 2.001)).axis(), ID(0.0, 3.0)); + BOOST_TEST_EQ(reduce(h, shrink(0, 2)).axis(), ID(0.0, 2.0)); + BOOST_TEST_EQ(reduce(h, shrink(0, 1.999)).axis(), ID(0.0, 2.0)); + + BOOST_TEST_EQ(reduce(h, crop(-1, 5)).axis(), ID(0.0, 3.0)); + BOOST_TEST_EQ(reduce(h, crop(0, 3)).axis(), ID(0.0, 3.0)); + BOOST_TEST_EQ(reduce(h, crop(1, 3)).axis(), ID(1.0, 3.0)); + BOOST_TEST_EQ(reduce(h, crop(1.001, 3)).axis(), ID(1.0, 3.0)); + BOOST_TEST_EQ(reduce(h, crop(1.999, 3)).axis(), ID(1.0, 3.0)); + BOOST_TEST_EQ(reduce(h, crop(2, 3)).axis(), ID(2.0, 3.0)); + BOOST_TEST_EQ(reduce(h, crop(0, 2.999)).axis(), ID(0.0, 3.0)); + BOOST_TEST_EQ(reduce(h, crop(0, 2.001)).axis(), ID(0.0, 3.0)); + BOOST_TEST_EQ(reduce(h, crop(0, 2)).axis(), ID(0.0, 2.0)); + BOOST_TEST_EQ(reduce(h, crop(0, 1.999)).axis(), ID(0.0, 2.0)); } // shrink and rebin { - auto h = make_s(Tag(), std::vector(), R(4, 1, 5, "1"), R(3, -1, 2, "2")); + auto h = make_s(Tag(), std::vector(), R(4, 1.0, 5.0, "1"), R(3, -1.0, 2.0, "2")); /* matrix layout: diff --git a/test/utility_clopper_pearson_interval_test.cpp b/test/utility_clopper_pearson_interval_test.cpp index b068e38b..eb645adf 100644 --- a/test/utility_clopper_pearson_interval_test.cpp +++ b/test/utility_clopper_pearson_interval_test.cpp @@ -18,7 +18,7 @@ template void test() { const T atol = 0.001; - clopper_pearson_interval iv(deviation{1.f}); + clopper_pearson_interval iv(deviation{1}); { const auto x = iv(0.f, 1.f); diff --git a/test/utility_jeffreys_interval_test.cpp b/test/utility_jeffreys_interval_test.cpp index 29384151..6c0363e2 100644 --- a/test/utility_jeffreys_interval_test.cpp +++ b/test/utility_jeffreys_interval_test.cpp @@ -25,78 +25,78 @@ void test() { jeffreys_interval iv(confidence_level{0.95}); { - auto p = iv(0, 7); - BOOST_TEST_IS_CLOSE(p.first, 0, atol); - BOOST_TEST_IS_CLOSE(p.second, 0.41, atol); + auto p = iv(0.f, 7.f); + BOOST_TEST_IS_CLOSE(p.first, 0.f, atol); + BOOST_TEST_IS_CLOSE(p.second, 0.41f, atol); } { - auto p = iv(1, 6); - BOOST_TEST_IS_CLOSE(p.first, 0, atol); - BOOST_TEST_IS_CLOSE(p.second, 0.501, atol); + auto p = iv(1.f, 6.f); + BOOST_TEST_IS_CLOSE(p.first, 0.f, atol); + BOOST_TEST_IS_CLOSE(p.second, 0.501f, atol); } { - auto p = iv(2, 5); - BOOST_TEST_IS_CLOSE(p.first, 0.065, atol); - BOOST_TEST_IS_CLOSE(p.second, 0.648, atol); + auto p = iv(2.f, 5.f); + BOOST_TEST_IS_CLOSE(p.first, 0.065f, atol); + BOOST_TEST_IS_CLOSE(p.second, 0.648f, atol); } { - auto p = iv(3, 4); - BOOST_TEST_IS_CLOSE(p.first, 0.139, atol); - BOOST_TEST_IS_CLOSE(p.second, 0.766, atol); + auto p = iv(3.f, 4.f); + BOOST_TEST_IS_CLOSE(p.first, 0.139f, atol); + BOOST_TEST_IS_CLOSE(p.second, 0.766f, atol); } { - auto p = iv(4, 7 - 4); - BOOST_TEST_IS_CLOSE(p.first, 0.234, atol); - BOOST_TEST_IS_CLOSE(p.second, 0.861, atol); + auto p = iv(4.f, 7.f - 4.f); + BOOST_TEST_IS_CLOSE(p.first, 0.234f, atol); + BOOST_TEST_IS_CLOSE(p.second, 0.861f, atol); } // extrapolated from table { - auto p = iv(5, 2); - BOOST_TEST_IS_CLOSE(p.first, 1 - 0.648, atol); - BOOST_TEST_IS_CLOSE(p.second, 1 - 0.065, atol); + auto p = iv(5.f, 2.f); + BOOST_TEST_IS_CLOSE(p.first, 1.f - 0.648f, atol); + BOOST_TEST_IS_CLOSE(p.second, 1.f - 0.065f, atol); } // extrapolated from table { - auto p = iv(6, 1); - BOOST_TEST_IS_CLOSE(p.first, 1 - 0.501, atol); - BOOST_TEST_IS_CLOSE(p.second, 1, atol); + auto p = iv(6.f, 1.f); + BOOST_TEST_IS_CLOSE(p.first, 1.f - 0.501f, atol); + BOOST_TEST_IS_CLOSE(p.second, 1.f, atol); } // extrapolated from table { - auto p = iv(7, 0); - BOOST_TEST_IS_CLOSE(p.first, 1 - 0.41, atol); - BOOST_TEST_IS_CLOSE(p.second, 1, atol); + auto p = iv(7.f, 0.f); + BOOST_TEST_IS_CLOSE(p.first, 1.f - 0.41f, atol); + BOOST_TEST_IS_CLOSE(p.second, 1.f, atol); } // not in table { - auto p = iv(0, 1); - BOOST_TEST_IS_CLOSE(p.first, 0, atol); - BOOST_TEST_IS_CLOSE(p.second, 0.975, atol); + auto p = iv(0.f, 1.f); + BOOST_TEST_IS_CLOSE(p.first, 0.f, atol); + BOOST_TEST_IS_CLOSE(p.second, 0.975f, atol); - fraction f(0, 1); + fraction f(0.f, 1.f); const auto y = iv(f); - BOOST_TEST_IS_CLOSE(y.first, 0.0, atol); - BOOST_TEST_IS_CLOSE(y.second, 0.975, atol); + BOOST_TEST_IS_CLOSE(y.first, 0.f, atol); + BOOST_TEST_IS_CLOSE(y.second, 0.975f, atol); } // not in table { - auto p = iv(1, 0); + auto p = iv(1.f, 0.f); BOOST_TEST_IS_CLOSE(p.first, 0.025, atol); BOOST_TEST_IS_CLOSE(p.second, 1, atol); - fraction f(1, 0); + fraction f(1.f, 0.f); const auto y = iv(f); - BOOST_TEST_IS_CLOSE(y.first, 0.025, atol); - BOOST_TEST_IS_CLOSE(y.second, 1, atol); + BOOST_TEST_IS_CLOSE(y.first, 0.025f, atol); + BOOST_TEST_IS_CLOSE(y.second, 1.f, atol); } }