diff --git a/include/boost/histogram/axis/variable.hpp b/include/boost/histogram/axis/variable.hpp index 6be785e7..45b79345 100644 --- a/include/boost/histogram/axis/variable.hpp +++ b/include/boost/histogram/axis/variable.hpp @@ -210,8 +210,9 @@ class variable : public iterator_mixin(std::modf(i, &z)); + double whole; + const double z = std::modf(i, &whole); + const auto k = static_cast(whole); const auto a = vec_[0]; const auto b = vec_[size()]; return (1.0 - z) * vec_[k] + z * vec_[k + 1] + shift * (b - a); diff --git a/test/axis_variable_test.cpp b/test/axis_variable_test.cpp index 1216bb77..2f938144 100644 --- a/test/axis_variable_test.cpp +++ b/test/axis_variable_test.cpp @@ -123,6 +123,21 @@ int main() { BOOST_TEST_EQ(a.index(4), 1); // 4 - 3 = 1 } + // axis::variable circular with size > 2 and non-equidistant bins; + // the error in value() cancels for size == 2 and integer arguments + { + axis::variable a{0, 1, 3, 4}; + BOOST_TEST_EQ(a.value(-1), -1); // vec_[2] - period + BOOST_TEST_EQ(a.value(0), 0); + BOOST_TEST_EQ(a.value(1), 1); + BOOST_TEST_EQ(a.value(2), 3); + BOOST_TEST_EQ(a.value(3), 4); + BOOST_TEST_EQ(a.value(4), 5); + BOOST_TEST_EQ(a.value(0.5), 0.5); + BOOST_TEST_EQ(a.value(1.5), 2); + BOOST_TEST_EQ(a.value(2.5), 3.5); + } + // axis::regular with growth { using pii_t = std::pair;