`distance` returns incorrect results for descending intervals

Open Beginner friendly
#64 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
72/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
cpp
Domain
backend

Research direction

The issue points to include/boost/icl/concept/interval.hpp, lines 1453–1493, where distance subtracts the closest interval ends. Read that implementation and check how it handles is_increasing<DomainT, Compare>::value; reproduce the examples for descending discrete and continuous intervals. Done means both return the expected distances (2 and 3) while existing increasing interval behavior remains correct.

Written by the indexing model from the issue text.

Description

As shown at https://godbolt.org/z/dfEzPx1rf:

#include <boost/icl/interval.hpp>
#include <functional>
#include <iostream>

int main()
{
  using discrete = boost::icl::discrete_interval<int, std::greater>;
  discrete x = discrete::closed(9, 8), y = discrete::closed(5, 3);
  std::cout << distance(x, y) << "\n"; // outputs -4, should be 2

  using continuous = boost::icl::continuous_interval<double, std::greater>;
  continuous a = continuous::closed(9, 8), b = continuous::closed(5, 3);
  std::cout << distance(a, b) << "\n"; // outputs -3, should be 3
}

Both discrete and continuous intervals are affected. This is not solved, not even partially, by (pending) #60. The offending code simply subtracts the closest ends of the intervals without checking is_increasing<DomainT, Compare>::value:
https://github.com/boostorg/icl/blob/b587925c05ab626436822a5d5e3317a5da97f320/include/boost/icl/concept/interval.hpp#L1453-L1493

Dominant language
C++
Stars
15
Forks
51
Avg merge
8d 6h
Merged PRs (30d)
1

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from boostorg/icl

All issues in boostorg/icl

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.