Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions include/boost/histogram/axis/variable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,9 @@ class variable : public iterator_mixin<variable<Value, MetaData, Options, Alloca
if (options_type::test(option::circular)) {
auto shift = std::floor(i / size());
i -= shift * size();
double z;
const auto k = static_cast<index_type>(std::modf(i, &z));
double whole;
const double z = std::modf(i, &whole);
const auto k = static_cast<index_type>(whole);
const auto a = vec_[0];
const auto b = vec_[size()];
return (1.0 - z) * vec_[k] + z * vec_[k + 1] + shift * (b - a);
Expand Down
15 changes: 15 additions & 0 deletions test/axis_variable_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<double, axis::null_type, op::circular_t> 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<axis::index_type, axis::index_type>;
Expand Down
Loading