From 75889f405d4e88ffd2e1776ce48536fb94d3bc0f Mon Sep 17 00:00:00 2001 From: Hans Dembinski Date: Wed, 14 Sep 2016 17:34:07 +0200 Subject: [PATCH] fixing a bug and adding a test for it --- include/boost/histogram/axis.hpp | 5 +---- test/axis_test.cpp | 2 ++ 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/include/boost/histogram/axis.hpp b/include/boost/histogram/axis.hpp index b568f711..f4183478 100644 --- a/include/boost/histogram/axis.hpp +++ b/include/boost/histogram/axis.hpp @@ -122,10 +122,7 @@ public: inline int index(double x) const { const double z = (x - min_) / delta_; - const int i = static_cast(z); - if (i > bins()) - return bins(); - return z >= 0.0 ? i : -1; + return z < 0.0 ? -1 : (z > bins() ? bins() : static_cast(z)); } double operator[](int idx) const diff --git a/test/axis_test.cpp b/test/axis_test.cpp index f26529b4..b2e4ae46 100644 --- a/test/axis_test.cpp +++ b/test/axis_test.cpp @@ -32,6 +32,8 @@ BOOST_AUTO_TEST_CASE(regular_axis_operators) { BOOST_CHECK_EQUAL(a.index(0.99), 2); BOOST_CHECK_EQUAL(a.index(1.0), 3); BOOST_CHECK_EQUAL(a.index(10.), 3); + BOOST_CHECK_EQUAL(a.index(std::numeric_limits::infinity()), 3); + BOOST_CHECK_EQUAL(a.index(-std::numeric_limits::infinity()), -1); } BOOST_AUTO_TEST_CASE(polar_axis_operators) {