Boost logo

Boost Users :

From: Johan Råde (rade_at_[hidden])
Date: 2008-03-04 02:18:05


Matt Gruenke wrote:
> To access the native binary representation of floating-point type T,
> isn't it easier & safer to use a union of T and a character array with
> sizeof (T) elements?

1. Doing that is not legal. The C++ standard states that
only one value in a union can be active at the same time time.
And it may not work with some optimizing compilers.

2. For my purposes, I needed to copy the bits, and then
make integer comparisons of the copied bits.
Hence I copy the bits into integers.

3. For other purposes, using an array of char and memcpy might be best.
Note that memcpy is very efficient. Both MSVC and GCC knows the meaning of memcpy
and generates optimized code for the copying.
A memcpy of 4 bytes will result in a single move instruction.

--Johan


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net