How can I check the floating point representation for storing files?

I have scientific data dumped to files. At the moment, I just dumped them with the same idea as in memory. I documented that they are IEEE754, but I would like this to be confirmed in the code, so that if it is ported to a strange architecture and separated from my documentation (research codes passed), these are compilation errors. At the moment I have

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

Is there any way to check the IEEE754? And can it be static?

+6
source share
3 answers

In C, check if this is defined:

 __STDC_IEC_559__ 

In C ++, check if this is true :

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

IEC559, abbreviated for International Electrotechnical Commission Standard 559, is the same as IEEE 754.

+4
source

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

For C, Appendix F of the current C standard states that the preprocessor constant __STDC_IEC_559__ will be predefined with a value of 1 if the platform complies with the IEEE 754 specification for floating point arithmetic. But older C compilers cannot predefine it, even if the float is indeed IEEE 754.

However, this is not enough for any language: this correspondence only guarantees the semantics of IEEE 754, not the binary representation, and since you are dumping the binary representation into a file, you will also need to handle the content problem. This becomes even more complicated since propositions for integers may differ from continents for floats.

In the end, it is much better to use a textual representation for storing floating point values ​​if you want to achieve portability between different platforms, present and future. Of course, you will need to use maximum precision for this view.

Another solution is provided by http://hdfgroup.org , which effectively handles this problem for large amounts of data.

+3
source

You can use std::numeric_limits<double> is_iec559 - see here

+1
source

Source: https://habr.com/ru/post/987552/


All Articles