X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fbinfloat.h;h=d16570fc057fa296a349f2b69bb167d68a960ceb;hb=HEAD;hp=2c74441ad333888f1d47f98b1c3e449b40f98f6f;hpb=0d925901d1446de6fe76616b9b2e731d7702571e;p=libs%2Fdatafile.git diff --git a/source/binfloat.h b/source/binfloat.h index 2c74441..db3e924 100644 --- a/source/binfloat.h +++ b/source/binfloat.h @@ -20,8 +20,8 @@ struct BinFloat { struct Bits { - unsigned exponent; - unsigned mantissa; + unsigned exponent = 8; + unsigned mantissa = 23; Bits(unsigned); }; @@ -29,13 +29,10 @@ struct BinFloat template struct MatchingInt; - template - union Conversion; - - bool sign; - bool infinity; - int exponent; - std::uint64_t mantissa; + bool sign = false; + bool infinity = false; + int exponent = 0; + std::uint64_t mantissa = 0; static BinFloat explode(std::uint64_t, const Bits &); @@ -54,27 +51,21 @@ struct BinFloat::MatchingInt { typedef std::uint32_t Type; }; template<> struct BinFloat::MatchingInt { typedef std::uint64_t Type; }; -template -union BinFloat::Conversion -{ - T f; - typename MatchingInt::Type i; -}; - template inline BinFloat BinFloat::explode_iec559(T v) { - Conversion c; - c.f = v; - return explode(c.i, sizeof(T)*CHAR_BIT); + typename MatchingInt::Type i; + memcpy(&i, &v, sizeof(T)); + return explode(i, sizeof(T)*CHAR_BIT); } template inline T BinFloat::compose_iec559() { - Conversion c; - c.i = compose(sizeof(T)*CHAR_BIT); - return c.f; + typename MatchingInt::Type i = compose(sizeof(T)*CHAR_BIT); + T v; + memcpy(&v, &i, sizeof(T)); + return v; } } // namespace DataFile