Hello. I want to find numbers lies in between (x, y) and [x, y]. I Googled for this and saw that it can be done using boost library. But I can not understand the synopsis. Please help me.

Dani AI

Generated

Quick answer: you don't need Boost to list the integers inside an open or closed floating-point interval — compute the integer bounds, then iterate. As asked, the key functions are std::ceil and std::floor. As noted, Boost can do interval math, but for extracting integers it's simpler to transform the interval endpoints to integer bounds, handle inclusivity/exclusivity, and then iterate or feed the bounds to a range helper.

A robust approach (handles exclusive endpoints and tiny floating-point rounding errors) is to nudge endpoints with std::nextafter, then take ceil for the lower bound and floor for the upper bound. If start > end there are no integers in the interval.

Example implementation:

#include <cmath>
#include <limits>
#include <vector>

std::vector<long long> integers_between(double low, double high, bool low_closed, bool high_closed)
{
    if (low > high) return {};
    double l = low;
    double h = high;
    if (!low_closed)  l = std::nextafter(l,  std::numeric_limits<double>::infinity());
    if (!high_closed) h = std::nextafter(h, -std::numeric_limits<double>::infinity());
    long long start = static_cast<long long>(std::ceil(l));
    long long end   = static_cast<long long>(std::floor(h));
    if (start > end) return {};
    std::vector<long long> out;
    out.reserve(static_cast<size_t>(end - start + 1));
    for (long long i = start; i <= end; ++i) out.push_back(i);
    return out;
}

Notes and cautions: floating-point values above 2^53 cannot represent all integers exactly, so if inputs may be huge use integer arithmetic (scaled integers) or long double. Avoid iterating when the range would produce billions of results — check the count first. If you prefer a Boost-based solution, compute start/end as above and use boost::irange(start, end + 1) (Boost.Range) or represent integer intervals with Boost ICL; both libraries have documentation and helpers for iteration and set operations.

References: std::ceil / std::floor (see C++ reference) and std::nextafter for the tiny-step adjustment: std::ceil, std::floor, std::nextafter. For Boost alternatives see the Boost.Range and Boost ICL documentation.

Recommended Answers

All 2 Replies

You didn't tell us which function you want to use, so we can't tell you the syntax.

You didn't tell us which function you want to use, so we can't tell you the syntax.

I have the interval (-105, -98.87), the integers lying between this interval are -104, -103, -102, -101, -100, -99. This can be done using for loop. But I want to do this using boost.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.