c++ - Serializing floats to bytes, when already assuming __STDC_IEC_559__ -


if test code following:

#ifndef __stdc_iec_559__     #error warning: __stdc_iec_559__ not defined. code assumes we're using ieee 754 floating point binary serialization of floats , doubles. #endif 

...such described here, guaranteed 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 float writing file or network packet?

if not, how can safely serialize floats , doubles?

also, who's responsible byte ordering - code or operating system?

to clarifiy question: can cast floats 4 bytes , doubles 8 bytes, , safely serialize , files or across networks, if i:

  1. assert we're using iec 559
  2. convert resulting to/from standard byte order (such network byte order).

__stdc_iec_559__ macro defined c99/c11, didn't find reference whether c++ guarantees support it.

a better solution 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 , if type adheres iec 559 standard.210)

53 meaningful floating point types.

in footnote:

210) international electrotechnical commission standard 559 same ieee 754.

about second assumption, don't think can byte order "standard", if byte order same between machines(little or big endian), yes, think can serialize that.


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

html - Unable to style the color of bullets in a list -

c# - must be a non-abstract type with a public parameterless constructor in redis -