Boost logo

Boost Users :

Subject: Re: [Boost-users] Numeric_cast from int to float
From: Fernando Cacciola (fernando.cacciola_at_[hidden])
Date: 2009-01-26 21:22:20

Hi Hermann,

> Hi,
> in my opinion, casting -2147483647 to a float should be
> raising an exception

numeric_cast<> checks for "range" errors, ie overflow, but not for loose
of significant precision.

This is like so by design, and AFAICT the documentation is very clear
about that.

Loose of precision is not checked because (a) it is not trivial to do so
*generically* and (b) looseless numeric conversions are less popular
(that is, *that* should be a different function as it has a
significantly different overhead)

Having said that, the *policy* based conversion utility that is used by
numeric_cast could be extended with an *additional* precision_check
policy (to be called if the range check suceeds) that could be used to
implement a "looseless_numeric_cast<>".

Do you have a concrete idea about how such a policy could be implemented


Fernando Cacciola

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at