6

I have scientific data dumped into files. At the moment I've just dumped them with the same representation as they are in memory. I have documented that they are IEEE754 but I would love to have this asserted in the code so that if it gets ported to a weird architecture and separated from my documentation (research codes get passed around) it errors on compilation. At the moment I have

static_assert(sizeof(double)==8), "message");

Is there a way to test IEEE754? And can that be static asserted?

chqrlie
  • 131,814
  • 10
  • 121
  • 189
Cramer
  • 1,785
  • 1
  • 12
  • 20
  • 3
    Unless the data sets are huge then consider storing the data as text - much more useful, portable and backward-compatible. – Paul R May 20 '15 at 06:03
  • First of all: is that a C or C++ problem? Solutions are different. – LPs May 20 '15 at 06:09
  • In theory there's `__STDC_IEC_559__` to check, but in practice almost no compiler that I know of do defines it; I would settle for checking the binary representation of a couple corner cases. Anyhow, more than not having IEEE754 (which is relatively rare) I would double-check the endianness. – Matteo Italia May 20 '15 at 06:10
  • @PaulR: good idea, but worth noting some compilers (like Visual C++) historically couldn't (and still can't AFAIK) even stream back *their own* output for special values like NaN and Infinity, and the Standard doesn't dictate how they should be represented to ensure portability. – Tony Delroy May 20 '15 at 06:12
  • @Cramer: any reason not to consider this a duplicate? – Tony Delroy May 20 '15 at 06:13
  • 1
    Better to use a production-grade approach file format, such as HDF5, and associated libraries. Better than text files, better than just tossing the bytes into a file. See https://www.hdfgroup.org, and many questions and answers here on SO. HDF5 is already widely used in research, anyone receiving such a file will be better pleased than if offered a bucket-of-bytes. – High Performance Mark May 20 '15 at 06:15
  • @TonyD No, I did a search (both google and here) and neither came up with that question. – Cramer May 20 '15 at 06:29
  • 1
    This question may be a duplicate, but not of the question in reference. The OP specifically asks about storage representation, not just semantics. The implications of storage requirements are specific and not really addressed in the answers to the previous questions. I have updated the question title to reflect this. – chqrlie May 20 '15 at 06:36

3 Answers3

4

In C, check if this is defined:

__STDC_IEC_559__

In C++, check if this is true:

std::numeric_limits<double>::is_iec559()

IEC559, short for International Electrotechnical Commission standard 559,is the same as IEEE 754.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
3

The answer for C++ is here: How to check if C++ compiler uses IEEE 754 floating point standard

For C, Annex F of the current C Standard specifies that the preprocessor constant __STDC_IEC_559__ will be pre-defined to the value 1 if the platform conforms to the IEEE 754 specification for floating point arithmetic. But older C compilers may not pre-define it even if floats are indeed IEEE 754.

Yet this is not enough for either language: this conformity only guarantees the semantics of IEEE 754, not the binary representation, and since you are dumping the binary representation to a file, you would also need to handle the endianness issue. It becomes even more complicated as endianness for integers may differ from the endianness for floats.

In the end, it is much better to use a textual representation to store your floating point values if you wish to achieve portability between various platforms, present and future. Of course, you will need to use the maximum precision for this representation.

Another solution is provided by http://hdfgroup.org that handle this very problem efficiently for large quantities of data.

Community
  • 1
  • 1
chqrlie
  • 131,814
  • 10
  • 121
  • 189
1

You could use std::numeric_limits<double>'s is_iec559 - see here

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252