
Forgive me if this topic has been discussed before - searching for "casts" and the like in the archives bring out quite a lot of results :) Although numeric_cast allows you to safely cast between numeric types, I find that it can sometimes come in handy to just receive the saturated value in the case of an overflow. A simplified solution would look something like this: template<class Source, class Target> inline Target saturated_cast(Source val) { BOOST_STATIC_ASSERT(std::numeric_limits<Target>::is_specialized); if(val < (std::numeric_limits<Target>::min)()) return (std::numeric_limits<Target>::min)(); if(val > (std::numeric_limits<Target>::max)()) return (std::numeric_limits<Target>::max)(); return static_cast<Target>(val); } I think that this would fit nicely into cast.hpp. Any comments? Kristian Dupont

On Thu, Jan 13, 2005 at 05:02:33PM +0100, Kristian Dupont wrote:
Forgive me if this topic has been discussed before - searching for "casts" and the like in the archives bring out quite a lot of results :)
Although numeric_cast allows you to safely cast between numeric types, I find that it can sometimes come in handy to just receive the saturated value in the case of an overflow. A simplified solution would look something like this:
template<class Source, class Target>
Should that be template<class Target, class Source> because you want Source to be deduced? jon
inline Target saturated_cast(Source val) { BOOST_STATIC_ASSERT(std::numeric_limits<Target>::is_specialized);
if(val < (std::numeric_limits<Target>::min)()) return (std::numeric_limits<Target>::min)();
if(val > (std::numeric_limits<Target>::max)()) return (std::numeric_limits<Target>::max)();
return static_cast<Target>(val); }
I think that this would fit nicely into cast.hpp. Any comments?
Kristian Dupont
-- "Outside of a dog, a man's best friend is a book. Inside of a dog, it's too dark to read." -Groucho Marx

"Kristian Dupont" <secondary@chrylers.com> escribió en el mensaje news:cs662h$k8k$1@sea.gmane.org...
Forgive me if this topic has been discussed before - searching for "casts" and the like in the archives bring out quite a lot of results :)
Although numeric_cast allows you to safely cast between numeric types, I find that it can sometimes come in handy to just receive the saturated value in the case of an overflow. A simplified solution would look something like this:
template<class Source, class Target> inline Target saturated_cast(Source val) { BOOST_STATIC_ASSERT(std::numeric_limits<Target>::is_specialized);
if(val < (std::numeric_limits<Target>::min)()) return (std::numeric_limits<Target>::min)();
if(val > (std::numeric_limits<Target>::max)()) return (std::numeric_limits<Target>::max)();
return static_cast<Target>(val); }
I think that this would fit nicely into cast.hpp. Any comments?
Kristian Dupont
Please, take a look at the new Boost Numeric Conversion Library: http://www.boost.org/libs/numeric/conversion/doc/index.html It replaces numeric_cast<> in cast.hpp (except for BCB and VC6 for which the new code has not been ported yet) Altough what you suggested is not supported by the new code, it would be easy to add a policy to select this saturating behaviour.. I used it in the past and I know that in certain contexts is very useful. Before doing it, however, I would like to see if there's a broader scope in which this particular change fit; that is, to see a bigger picture, if any. Best, Fernando Cacciola

Please, take a look at the new Boost Numeric Conversion Library:
http://www.boost.org/libs/numeric/conversion/doc/index.html
It replaces numeric_cast<> in cast.hpp (except for BCB and VC6 for which the new code has not been ported yet)
You are right. I didn't realize that this library was a replacement for numeric_cast.
Altough what you suggested is not supported by the new code, it would be easy to add a policy to select this saturating behaviour.. I used it in the past and I know that in certain contexts is very useful.
It often is when working with "signal"-kinds of data where performance or memory is more important than accuracy. Kristian Dupont
participants (3)
-
Fernando Cacciola
-
Jonathan Wakely
-
Kristian Dupont