Does boost have a float-to-integer conversion?
Does boost have a float-to-integer conversion? I need a routine to convert a 32 bit single precision float (1S 8E 23M) to integer, with the end goal being that I can take the integer break it down into bytes... i.e.bits 0-7, 8-15,16-23,24-31.
linux@celticblues.com wrote:
Does boost have a float-to-integer conversion?
There is Boost.NumericConversion http://www.boost.org/libs/numeric/conversion/doc/index.html which provides: boost::numeric_cast<int>(your_float) That conversion funtion will round even and throw an exception if the float is out of range.
I need a routine to convert a 32 bit single precision float (1S 8E 23M) to integer, with the end goal being that I can take the integer break it down into bytes... i.e.bits 0-7, 8-15,16-23,24-31.
Then I suspect you don't really want to "convert" the float to int but to
get at the value-representation of the float.
If that's the case, you can do this:
char* float_value_rep =
static_cast
On Thursday 07 February 2008 07:40 am, Fernando Cacciola wrote:
Then I suspect you don't really want to "convert" the float to int but to get at the value-representation of the float. If that's the case, you can do this:
char* float_value_rep = static_cast
(static_cast (&your_float));
Isn't this what reinterpret_cast is for? It wouldn't require bouncing through a void*. Also, to the original poster, don't succumb to the temptation to cast to anything other than a char* (like an int*) or you'll end up in strict aliasing violation hell. -- Frank
On Feb 7, 2008 1:31 PM, Frank Mori Hess
On Thursday 07 February 2008 07:40 am, Fernando Cacciola wrote:
Then I suspect you don't really want to "convert" the float to int but to get at the value-representation of the float. If that's the case, you can do this:
char* float_value_rep = static_cast
(static_cast (&your_float)); Isn't this what reinterpret_cast is for? It wouldn't require bouncing through a void*. Also, to the original poster, don't succumb to the temptation to cast to anything other than a char* (like an int*) or you'll end up in strict aliasing violation hell.
For most uses of reinterpret_cast, it invokes implementation specific behavior. static_cast is portable though. So the latter is preferrable.
-- Frank
Regards, -- Felipe Magno de Almeida
Actually, I was hoping Boost had an float-to-integer conversion so I could look at the implementation. I need to convert a float to int, but not in OpenGL Shading Language, which is "C like" but it doesn't have pointers, bit-shifts etc. Ed Frank Mori Hess wrote:
On Thursday 07 February 2008 07:40 am, Fernando Cacciola wrote:
Then I suspect you don't really want to "convert" the float to int but to get at the value-representation of the float. If that's the case, you can do this:
char* float_value_rep = static_cast
(static_cast (&your_float)); Isn't this what reinterpret_cast is for? It wouldn't require bouncing through a void*. Also, to the original poster, don't succumb to the temptation to cast to anything other than a char* (like an int*) or you'll end up in strict aliasing violation hell.
------------------------------------------------------------------------
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
On Feb 7, 2008 9:23 AM, linux
Actually, I was hoping Boost had an float-to-integer conversion so I could look at the implementation. I need to convert a float to int, but not in OpenGL Shading Language, which is "C like" but it doesn't have pointers, bit-shifts etc.
Ed
On Thursday 07 February 2008 07:40 am, Fernando Cacciola wrote:
Then I suspect you don't really want to "convert" the float to int but to get at the value-representation of the float. If that's the case, you can do this:
char* float_value_rep = static_cast
(static_cast (&your_float)); Isn't this what reinterpret_cast is for? It wouldn't require bouncing
Frank Mori Hess wrote: through
a void*. Also, to the original poster, don't succumb to the temptation to cast to anything other than a char* (like an int*) or you'll end up in strict aliasing violation hell.
------------------------------------------------------------------------
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- To follow the Path, Look to the master, Walk with the master, See through the master, Become the master
participants (6)
-
Felipe Magno de Almeida
-
Fernando Cacciola
-
Frank Mori Hess
-
Johnathan Bunn
-
linux
-
linux@celticblues.com