[numeric_conversion] rounding

I'm wondering how to use the numeric_conversion library to perform rounding based on types. Assume I have a double value of 3.7. If I convert it to integer, the result should be 4. If I convert it to float, the result should be 3.7. The types I want to convert from and to are both specified in a template, i.e. they form an open set of types.
From reading the numeric_conversion docs I tend to think that the library can do what I want, but I'm unable to find out how.
Can anybody help? Peter

Hi Peter, Peter wrote:
I'm wondering how to use the numeric_conversion library to perform rounding based on types.
Assume I have a double value of 3.7. If I convert it to integer, the result should be 4. If I convert it to float, the result should be 3.7. The types I want to convert from and to are both specified in a template, i.e. they form an open set of types.
From reading the numeric_conversion docs I tend to think that the library can do what I want, but I'm unable to find out how.
Can anybody help?
the default behaviour of numeric_cast seems to be to truncate so 3.7 will be casted to 3. I asked nearly the same question some time ago and got a useful answer from Fernanco Cacciola. Look at his post from 23.04.08 (don't know how to link it here) Maybe I should just paste it, so here it comes:
numeric_cast<> uses the default policies, but you an easily create you own version with any other policy, like the one which rounds floats to the nearest integer: RoundEven<>
#include
template
inline Target my_numeric_cast ( Source arg ) { typedef boost::numeric::conversion_traits Traits ; typedef boost::numeric::converter
Converter ;
return Converter::convert(arg); }
HTH
-- Fernando Cacciola
Regards, Rainer

Rainer,
this works, thanks a lot.
However, as I could see by single stepping with the debugger, this seems to
do much more than what I want, so I suspect it has efficiency issues. I will
be doing this for pixels of images, so it better be as fast as possible.
It seems to do overflow checking, which I don't need (I know it won't
overflow). How can I disable this? I tried silent_overflow_handler, but this
seems still to do the checking.
Also I'm not sure if the rounding couldn't be more efficient (I only need to
add 0.5, no need to be more elaborate). Is there a way to plug in my own
rounding?
Thanks
Peter
"Rainer Thaden"
Hi Peter,
Peter wrote:
I'm wondering how to use the numeric_conversion library to perform rounding based on types.
Assume I have a double value of 3.7. If I convert it to integer, the result should be 4. If I convert it to float, the result should be 3.7. The types I want to convert from and to are both specified in a template, i.e. they form an open set of types.
From reading the numeric_conversion docs I tend to think that the library can do what I want, but I'm unable to find out how.
Can anybody help?
the default behaviour of numeric_cast seems to be to truncate so 3.7 will be casted to 3. I asked nearly the same question some time ago and got a useful answer from Fernanco Cacciola. Look at his post from 23.04.08 (don't know how to link it here)
Maybe I should just paste it, so here it comes:
numeric_cast<> uses the default policies, but you an easily create you own version with any other policy, like the one which rounds floats to the nearest integer: RoundEven<>
#include
template
inline Target my_numeric_cast ( Source arg ) { typedef boost::numeric::conversion_traits Traits ; typedef boost::numeric::converter
Converter ;
return Converter::convert(arg); }
HTH
-- Fernando Cacciola
Regards,
Rainer

Hi Peter,
this works, thanks a lot.
Thanks to Fernando!
However, as I could see by single stepping with the debugger, this seems to do much more than what I want, so I suspect it has efficiency issues. I will be doing this for pixels of images, so it better be as fast as possible.
It seems to do overflow checking, which I don't need (I know it won't overflow). How can I disable this? I tried silent_overflow_handler, but this seems still to do the checking.
Also I'm not sure if the rounding couldn't be more efficient (I only need to add 0.5, no need to be more elaborate). Is there a way to plug in my own rounding?
I experienced quite the same and solved it ... by adding 0,5 ;-) You may get some other problems with that but as a first step it works. Regards, Rainer
participants (2)
-
Peter
-
Rainer Thaden