Basic use of function "contains" in Boost ICL: Are some combinations of interval types and functions not implemented?

StackOverflow https://stackoverflow.com/questions/22540364

  •  18-06-2023
  •  | 
  •  

Domanda

I started to use Boost ICL and I stumbled upon very basic stuff. For example the function contains should return true or false depending if a given element is in the interval or not. However that works for [right,left]_open_intervals but not for [open,closed]_inteval (see example below).

This seems to be too obvious to be an oversight. I am using the library in the intended way?

For example (using gcc 4.8 or clang 3.3 and Boost 1.54):

#include <boost/concept_check.hpp> //needed to make this MWE work, boost icl should include it internally

#include<boost/icl/right_open_interval.hpp>
#include<boost/icl/closed_interval.hpp>
#include<boost/icl/open_interval.hpp>
int main(){
    boost::icl::right_open_interval<double> roi(6.,7.);
    assert(boost::icl::contains(roi, 6.) == true);  //ok
    assert(boost::icl::contains(roi, 6.) == false); //ok

    boost::icl::closed_interval<double> oi(4.,5.); // or open_interval
    assert(boost::icl::contains( oi, 4.) == false); //error: "candidate template ignored"
    assert(boost::icl::contains( oi, 5.) == false); //error: "candidate template ignored"
}

Note: The above are called "static" intervals (because their bound properties are part of the type). Dynamic intervals work as expected.

È stato utile?

Soluzione

I would guess it comes down to the relative uselessness of floating point equality testing.

Have you ever tried to do assert(0.1 + 0.2 == 0.3)?

Try it. I'll wait.

In case you already know the answer, it'll be clear why a closed interval is not easy to implement correctly. Backgrounders:

Also, if you have two consecutive closed intervals [a,b][b,c]. in which interval does b belong?

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top