Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support boost::rational as value type for axis::regular #337

Open
HDembinski opened this issue Sep 27, 2021 · 2 comments
Open

Support boost::rational as value type for axis::regular #337

HDembinski opened this issue Sep 27, 2021 · 2 comments

Comments

@HDembinski
Copy link
Collaborator

#include <boost/histogram.hpp>
#include <boost/rational.hpp>

namespace bh = boost::histogram;

int main() {
    using R = bh::axis::regular<boost::rational<int>>;

    auto h = bh::make_histogram(R(10, 0, 1));
}

This currently triggers a static_assert, but should be supported.

@HDembinski
Copy link
Collaborator Author

Related #336

@HDembinski
Copy link
Collaborator Author

HDembinski commented Jan 10, 2022

Prototype for experimenting

#include <boost/histogram.hpp>
#include <boost/histogram/ostream.hpp>
#include <boost/rational.hpp>
#include <iostream>

int main() {
    namespace bh = boost::histogram;

    struct axis {
        using value_type = boost::rational<int>;
        using index_type = bh::axis::index_type;

        index_type num_;
        value_type offset_;
        value_type scale_;

        axis(index_type n, value_type a, value_type b) :
            num_{n}, offset_{a}, scale_{rat{n} / (b - a)} {}

        index_type index(const value_type& x) const {
            const auto i = boost::rational_cast<index_type>(scale_ * (x - offset_));
            return i >= -1 ? (i < num_ ? i : num_) : -1;
        }

        value_type value(index_type i) const {
            return i / scale_ + offset_;
        }

        index_type size() const { return num_; }

        static constexpr auto options() { return bh::axis::option::underflow | bh::axis::option::overflow; }
    };

    auto h = bh::make_histogram(axis{10, 0, 5});

    h(-1);
    h(1);
    h(2);

    std::cout << h << std::endl;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant