Boost logo

Boost Users :

Subject: Re: [Boost-users] Convert big endian to little endian (floating point values)
From: Agustín K-ballo Bergé (kaballo86_at_[hidden])
Date: 2015-12-09 06:40:34


On 12/8/2015 11:32 PM, Paolo Bolzoni wrote:
> On Wed, Dec 9, 2015 at 7:18 AM, Martin Vymazal <martin.vymazal_at_[hidden]> wrote:
>> What would be a possible approach to save the data without 'relying on the
>> platform specific bit representation of primitive data types' as you put it?
>
> What about using boost integer to find an unsigned integer (let's call
> its type Lint) longer or equal than the float (type Float) you have
> and make an union?

Ugh, this again? Third time this week... just no.

> union Lint_Float_t {
> Lint store;
> Float read;
> };
> static_assert(sizeof(Lint) >= sizeof(Float), "");
>
> To store you copy the float in the union, and save the integer. To
> load you load the integer and copy from the union the float.
>
> It is definitely not portable (reading and writing different types in
> a union is at most implementation dependant) and you keep the original
> bits, but it should work at least on machine of the same type.

Reading from an inactive member of a union is plain and simply undefined
behavior. To read and write the bits of the `float` in a well defined
way use `std::memcpy`.

Regards,

-- 
Agustín K-ballo Bergé.-
http://talesofcpp.fusionfenix.com

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