Question

I would like to write a function that checks that float, double or long double are fully compliant to the IEEE-754 format. I mean:

I thought that std::numeric_limits<T>::is_iec559 was corresponding to that, but it returns true for long double on my Linux 64-bits where sizeof(long double) = 16 bytes but internally encoded in a 80-bit Intel format. So how to do that?

Était-ce utile?

La solution

If std::numeric_limits<T>::is_iec559 is true then T adheres to the standard.

If the system uses 80-bit for its internal registers this is ok as long as the final rounded result is the closest to a reference result where an infinite number of bits is used. That is, adding or removing one ulp will get you away from the reference.

It's hard to check this by running all possible operations with all representable numbers and compare against a reference that uses many bits.

You need to have confidence on std::numeric_limits and the library implementing it.

Autres conseils

80-bit long double is actually IEEE 754 compliant because it falls into the "extended format" category

The IEEE 754 floating-point standard recommends that implementations provide extended precision formats. The standard specifies the minimum requirements for an extended format but does not specify an encoding. The encoding is the implementor's choice.

https://en.wikipedia.org/wiki/Extended_precision#IEEE_754_extended_precision_formats

In IEEE 754-1985 apart from the common single and double-precision there are also 2 corresponding extended formats being defined:

  • Single-extended precision: size ⩾ 43 bits with significand ⩾ 32 bits and exponent ⩾ 11 bits
  • Double-extended precision: size ⩾ 79 bits with significand ⩾ 64 bits and exponent ⩾ 15 bits

So the 80-bit format in Intel x86 and Motorola 68k fits exactly in the double-extended format. In fact Itanium's 82-bit float is also a compliant type

IEEE 754-2008 renames single and double-precision formats and add a few more types but the basic requirement looks the same with the extended type's exponent size equal to the next type if available

Parameter binary32 binary64 binary128 decimal64 decimal128
p digits ≥ 32 64 128 22 40
emax ≥ 1023 16383 65535 6144 24576

NOTE 1 — For extended formats, the minimum exponent range is that of the next wider basic format, if there is one, while the minimum precision is intermediate between a given basic format and the next wider basic format.

https://irem.univ-reunion.fr/IMG/pdf/ieee-754-2008.pdf

References & further reading:

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top