Boost logo

Boost :

From: Robert Kawulak (kawulak_at_[hidden])
Date: 2007-09-15 10:17:22


> From: Martin Dowie
> Strange, I've been doing it in Ada for 15+ years and it's
> been of /great/ assistance there!! ;-)

I don't know the exact details of Ada's floats, but knowing Ada's approach in
general I'd suspect it probably doesn't allow for surprises like those described
below (excerpt from http://hal.archives-ouvertes.fr/hal-00128124/en/):

[qoute]

[...] there exist a number of myths of what IEEE-compliance really entails from
the
point of view of programsemantics. We shall discuss the following myths, among
others:
. "Since C's float (resp. double) type is mapped to IEEE single (resp.
double) precision arithmetic, arithmetic operations have a uniquely de-
fined meaning across platforms."
. "Arithmetic operations are deterministic; that is, if I do z=x+y in two
places in the same program and my program never touches x and y in the
meantime, then the results should be the same."
. A variant: "If x < 1 tests true at one point, then x < 1 stays true later
if I never modify x."
. "The same program, strictly compliant with the C standard with no "un-
defined behaviours", should yield identical results if compiled on the same
IEEE-compliant platform by different compliant compilers."

[end qoute]

The 3rd point is crucial WRT using floats with constrained<>.

> /But/ you have that with plain-old-code checks anyway, and
> this library is
> really just about allowing people to replace these tiresome
> and repetitive
> checks with something "automagic".

Right, but checking a float value like this is simply wrong:

float f = 1.5;
assert(f == 1.5);

Constrained types depend on exact comparison, and this is why they're not
compatible with built-in floats.

> Given that most of the ranges I'm interested in are real
> numbers this is a "must have" in my book.

Then an exact floating point type should be used as the underlying type instead
of the built-in ones. Something like decimal or fixed-point or I don't know what
else, anyway a type for which 1.5. == 1.5 for sure. ;-) I'm not aware of any
particular class of this kind, but I suppose there should exist several C++
libraries offering this functionality.

> Ok, I'll go and dig deeper. It would be nice to be able to say:
>
> bounded_int<int, 0, 100>::type b;
>
> std::cout << "first = " << b.first() << "\n"
> << "last = " << b.last() << std::endl;

Then write:

std::cout << "first = " << b.constraint().lower_bound() << "\n"
        << "last = " << b.constraint().upper_bound() << std::endl;

Note, that bounded_int<>::type is only a typedef of constrained<>. It doesn't
contain first() and last(), because bounds are specific only to within_bounds
constraint, while constrained<> supports any constraint. For most of other
constraints first() and last() simply doesn't make sense (e.g. is_even).

Best regards,
Robert


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