[ICL] cardinality of float interval

cardinality( interval< float >( 1, 5 ) ) == 18446744073709551615 Can this be true? Is this by design?

John Reid wrote:
cardinality( interval< float >( 1, 5 ) ) == 18446744073709551615
Can this be true? Is this by design?
Clearly not true, since there are only 32 bits of unique float values possible and a fraction of those aren't normal. How many unique values of float are there between 1 and 5? Do you know how to calculate it? We would have to assume float conforms to the IEEE floating point standard (pretty safe) and then unpack the bits. What would you do with that information if you could get it? I dimly recall that this issue came up during the review. I think that the author may be viewing floating point as a continuous interval rather than discreet, so asking for the cardinality wouldn't make sense from that point of view. I think it would be nicer for cardinality to generate a syntax error than return a very large number when instantiated for a continuous interval type. I would use SFINAE protection on its regurn value. A STATIC_ASSERT would work too, but give much more verbose error messages. Regards, Luke

2011/2/17 John Reid <j.reid@mail.cryst.bbk.ac.uk>:
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

On 18/02/11 00:01, Joachim Faulhaber wrote:
I know ... this design is questionable.
It is well-defined though.
Are you using the cardinality function on float intervals in real use cases?
No, I'm writing a wrapper around icl using boost.python so I can use the functionality in some genomic applications I'm working on. I imagine int intervals will be the only ones I use. I'm trying to make the wrapper reasonably complete though. I did expect to have a compile error on this one but it is no biggie. Regards, John.
participants (3)
-
Joachim Faulhaber
-
John Reid
-
Simonson, Lucanus J