1

If I test my code with the following:

#ifndef __STDC_IEC_559__
    #error Warning: __STDC_IEC_559__ not defined. The code assumes we're using the IEEE 754 floating point for binary serialization of floats and doubles.
#endif

...such as is described here, am I guaranteed that this:

float myFloat = ...;
unsigned char *data = reinterpret_cast<unsigned char*>(&myFloat)

unsigned char buffer[4];
std::memcpy(&Buffer[0], data, sizeof(float));

...would safely serialize the float for writing to a file or network packet?

If not, how can I safely serialize floats and doubles?

Also, who's responsible for byte ordering - my code or the Operating System?

To clarifiy my question: Can I cast floats to 4 bytes and doubles to 8 bytes, and safely serialize to and from files or across networks, if I:

  1. Assert that we're using IEC 559
  2. Convert the resulting to/from a standard byte order (such as network byte order).
Community
  • 1
  • 1
Jamin Grey
  • 10,151
  • 6
  • 39
  • 52
  • 2
    Regarding byte ordering: there are little-endian floats (typically found on little-endian architectures) and big-endian floats (typically found on big-endian architectures). The reason is that the representation of a `float`, when treated as an integer of the same endianness, has some interesting properties allowing, at the end of the sophistication spectrum, tricks like http://en.wikipedia.org/wiki/Fast_inverse_square_root#Overview_of_the_code . So if you want the data to work across endianesses, you need to pay attention to byte order. – Pascal Cuoq Jul 22 '13 at 09:04

2 Answers2

2

__STDC_IEC_559__ is a macro defined by C99/C11, I didn't find reference about whether C++ guarantees to support it.

A better solution is to use std::numeric_limits< float >::is_iec559 or std::numeric_limits< double >::is_iec559

C++11 18.2.1.1 Class template numeric_limits

static const bool is_iec559 ;

52 True if and only if the type adheres to IEC 559 standard.210)

53 Meaningful for all floating point types.

In the footnote:

210) International Electrotechnical Commission standard 559 is the same as IEEE 754.

About your second assumption, I don't think you can say any byte order is "standard", but if the byte order is the same between machines(little or big endian), then yes, I think you can serialize like that.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • 1
    I thought C99 was included in C++11. Thanks I'll use that other option instead. But, uh, your answer still doesn't answer my question. =) My quetion was whether I can cast a float to bytes and safely serialize to and from files or across networks, if two assumptions are made: 1) That we're using IEC 559, and 2) That the bytes are then converted to/from a standard byte order. – Jamin Grey Jul 22 '13 at 18:53
1

How about considering standard serialization like XDR [used in Unix RPC] or CDR etc ?

http://en.wikipedia.org/wiki/External_Data_Representation

for example : bool_t xdr_float(XDR *xdrs, float *fp); from linux.die.net/man/3/xdr

or a c++ library http://xstream.sourceforge.net/

You might also be intersted in CDR [used by CORBA] , ACE [adaptive communication environment] has CDR classes [But its very heavy library]

Anand Rathi
  • 790
  • 4
  • 11
  • XDR would specify what the external format should be. However, this question asks about converting data from a `float` to the bytes of an external format. It is about the C++ methods for performing the conversion, not about what the external format is. – Eric Postpischil Jul 22 '13 at 11:05
  • I have my own serialization methods I wish to continue using. All I want is to find a safe way to serialize and unserialize floats (to 4 bytes) and doubles (to 8 bytes). I looked into XDR for a few hours, but I couldn't find good information about it for MinGW. Does XDR provide a simple stand-alone Float to Bytes conversion function, or must it be part of a larger XDR system? – Jamin Grey Jul 22 '13 at 18:48
  • @EricPostpischil kindly read this line again "Convert the resulting to/from a standard byte order (such as network byte order)." any XDR implementation does that. – Anand Rathi Jul 22 '13 at 22:01
  • @AnandRathi: You suggested XDR (which is a specification of data formats) not an XDR implementation (which is software to provide functions related to XDR). The question asks about methods for converting data, and this answer does not provide any information about that, not even a name of an XDR implementation or a link to one. Furthermore, the question asks specific questions about C++ and reinterpreting data which would not be answered by an XDR implementation. – Eric Postpischil Jul 22 '13 at 22:05
  • @JaminGrey , I am just trying to help you bool_t xdr_float(XDR *xdrs, float *fp); from http://linux.die.net/man/3/xdr – Anand Rathi Jul 22 '13 at 22:05
  • @EricPostpischil if you feel i havent provided something , Kindly feel free to add more info you know which can help. – Anand Rathi Jul 22 '13 at 22:07
  • 1
    @AnandRathi "I am just trying to help you". Certainly! And I greatly appreciate it! Pardon me if I get too snarky. I just couldn't find relevant XDR documentation (I was getting confused by related libraries as well), despite a few hours of searching yesterday. Sometimes I look in all the wrong places! Thanks for the additional links, I'll pour over those. =) – Jamin Grey Jul 22 '13 at 22:31