Boost logo

Boost :

Subject: Re: [boost] [ICL] cardinality of float interval
From: Joachim Faulhaber (afojgo_at_[hidden])
Date: 2011-02-17 19:01:14


2011/2/17 John Reid <j.reid_at_[hidden]>:
> cardinality( interval< float >( 1, 5 ) ) == 18446744073709551615
>
> Can this be true?

No, the only truth is 42 ;)

> Is this by design?

Yes it is.

cardinality(x) gives the number of elements in a set, container or
interval. In the STL the number of elements of a container is
represented by the type std::size_t. For an interval representing a
set we face the situation that the number of elements can be infinite
if the element type is continuous.

Since float represents a real number it is assumed to be continuous.
So the cardinality of interval<float>::closed(1,5) is infinite. Since
std::size_t has no INF value, I chose
(std::numeric_limits<std::size_t>::max)() as representation for
infinity. You can use icl::infinity<T>::value() to check for this
representation of infinity.

typedef size_type_of<interval<float>::type>::type itv_float_size_type;
BOOST_CHECK( (is_same<itv_float_size_type, std::size_t>::value) );

BOOST_CHECK_EQUAL(cardinality(interval<float>::closed(5,5)), 1);

BOOST_CHECK_EQUAL(
  cardinality(interval<float>::closed(1,5)),
  icl::infinity<itv_float_size_type>::value()
);

I know ... this design is questionable.

I could write a function template

template<class IntervalT>
boost is_infinite(IntervalT const&);

and make the cardinality function partial instead.

Are you using the cardinality function on float intervals in real use cases?

Best regards,
Joachim

-- 
Interval Container Library [Boost.Icl]
http://www.joachim-faulhaber.de

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk