Boost logo

Boost :

From: helmut.zeisel_at_[hidden]
Date: 2001-11-02 01:22:08


--- In boost_at_y..., "Moore, Paul" <paul.moore_at_a...> wrote:
> From: helmut.zeisel_at_a...
> > The header ratioal.hpp defines rational_cast
> > to convert a rational type to a floatig point type.
> >
> > Do we need a special name for that purpose
> > or might it be more consistent to
> > use an overload of numeric_cast instead of rational_cast?
>
> According to the documentation, numeric_cast is to act like
static_cast, but
> to detect loss of numeric range. Specifically, "An exception is
thrown when
> a runtime value-preservation check fails." The rational_cast
function
> doesn't do anything like this,

To be more exact:
the documentation of rational_cast
does not say what rational_cast
should do in case when the value is not preserved.
Clearly this will not happen for built-in types.
It might, happen, however,
for a user defined int
or an overloaded version of rational_cast.
(Although there is still the open problem
that rational_cast cannot be overloaded, cf.

http://groups.yahoo.com/group/boost/message/15728 )

> so I don't think it's a valid variant of
> numeric_cast.
>

So we might ask the opposite question:
Should numeric_cast be a valid variant of rational_cast?

> And besides, the numeric_cast code looks scary, compared to
rational_cast. I
> don't relish trying to integrate the code (for no real benefit).
>

I did expect this answer ;-)
Anyway, there are three options where
this "error_checked_rational_cast" could be:

in rational.hpp, in (numeric_)cast.hpp
or in a completely new file.

Just to make the benefit of a name change clear:

Consider an algorithm that should work with int and rational:

template<typname Float, typename T> void algorithm(T& t)
{
   ...
   Float F = xxx_cast<F>(t)
   ...
}

If T is either int or rational<int>, what should
"xxx_cast" be?

Helmut


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