IEEE 754 / iec 559

Is the IEEE 754 floating point format well defined on different platforms? In terms of both bit format and judgment?

I want to add the following to my code (for the initial version):

static_assert(std::numeric_limits<float>::is_iec559, "Only support IEC 559 (IEEE 754) float"); static_assert(sizeof(float) * CHAR_BIT == 32, "Only support float => Single Precision IEC 559 (IEEE 754)"); static_assert(std::numeric_limits<double>::is_iec559, "Only support IEC 559 (IEEE 754) double"); static_assert(sizeof(float) * CHAR_BIT == 64, "Only support double => Double Precision IEC 559 (IEEE 754)"); static_assert(std::numeric_limits<long double>::is_iec559, "Only support IEC 559 (IEEE 754) long double"); static_assert(sizeof(float) * CHAR_BIT == 128, "Only support long double => Exteneded Precision IEC 559 (IEEE 754)"); // More asserts if required. // I noticed my current system has a sizeof(long double) => 128 // But numeric_limits<long double>::digits => 63 // So we are not storing quad precision floats only extended. 

If I write my float / double / long double bit in binary format, they can be moved between systems without further interpretation. i.e...

 void write(std::ostream& stream, double value) { stream.write(reinterpret_cast<char const*>(&value), 8); } .... double read(std::istream& stream) { double value; stream.read(reinterpret_cast<char*>(&value), 8); return value; } 

Or do I need to split the double into integer components for the transport (as suggested by this answer ):

The difference is that I am ready to limit my supported IEEE-754 representation, will it basically solve my floating point binary memory or do I need to take further steps?

Note. For non-compliant platforms (when I find them) I’m ready for a special occasion to use the code so that they read / write IEEE-754 in a local view. But I want to know if the bit / endian specific cross platform is enough to support storage / transport.

+6
source share
3 answers

The bit format is clearly defined, but not all machines are not very similar. The IEEE standard does not require floating point numbers to be specified. You can run the following program to see the double 42.0 byte pattern:

 #include <stdio.h> #include <numeric> #include <limits> using namespace std; int main() { double d = 42; printf("%i\n", std::numeric_limits<double>::is_iec559); for (char *c = (char *)&d; c != (char *)(&d+1); c++) { printf("%02hhx ", *c); } printf("\n"); } 

On an old, unloaded Sun machine using g ++ 3.4.5, this prints

 1 40 45 00 00 00 00 00 00 

The x86_64 machine runs much more recent g ++:

 1 00 00 00 00 00 00 45 40 
+3
source

First of all, you can change your code so that it correctly checks the font sizes ...

 static_assert(std::numeric_limits<float>::is_iec559, "Only support IEC 559 (IEEE 754) float"); static_assert(sizeof(float) * CHAR_BIT == 32, "Only support float => Single Precision IEC 559 (IEEE 754)"); static_assert(std::numeric_limits<double>::is_iec559, "Only support IEC 559 (IEEE 754) double"); static_assert(sizeof(double) * CHAR_BIT == 64, "Only support double => Double Precision IEC 559 (IEEE 754)"); static_assert(std::numeric_limits<long double>::is_iec559, "Only support IEC 559 (IEEE 754) long double"); static_assert(sizeof(long double) * CHAR_BIT == 128, "Only support long double => Exteneded Precision IEC 559 (IEEE 754)"); 

The fact is that IEEE-754 does not require a long double bit with a length of 128 bits. Depending on the compiler and platform, this type of length may vary. However, it indicates binary128, which may or may not be supported by the compiler, depending on the platform and implementation (for this, gcc has the non-standard type __float128). The standard only requires double extension to be at least exact like double, which is usually 80 bits (gcc) or 64 (VS).

If you are limiting your supported IEEE-754 performance, you should not run into any problems.

+1
source

To read and write IEEE 754 portable, use these procedures. If the platform is not IEEE 754, you may lose a couple of bits, but you will still get the highest possible performance.

https://github.com/MalcolmMcLean/ieee754

0
source

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


All Articles