How to check that IEEE 754 single-precision (32-bi

2020-02-11 06:05发布

I want to test the following things on my target board:

  • Is 'float' implemented with IEEE 754 single-precision (32-bit) floating-point variable?
  • Is 'double' implemented with IEEE 754 double-precision (64-bit) floating-point variable?

What are the ways in which i can test it with a simple C program.

3条回答
甜甜的少女心
2楼-- · 2020-02-11 06:37

First of all, you can find the details about the ISO/IEC/IEEE 60559 (or IEEE 754) in Wikipedia:

Floating point standard types

As F. Goncalvez has told you, the macro __STDC_IEC_559__ brings you information about your compiler, if it conform IEEE 754 or not.

In what follows, we

However, you can obtain additional information with the macro FLT_EVAL_METHOD.

The value of this macro means:

  • 0 All operations and constants are evaluated in the range and precision of the type used.

  • 1 The operations of types float and double are evaluated in the range and precision of double, and long double goes in your own way...

  • 2 The evaluations of all types are done in the precision and range of long double.

  • -1 Indeterminate

  • Other negative values: Implementation defined (it depends on your compiler).

For example, if FLT_EVAL_METHOD == 2, and you hold the result of several calculations in a floating point variable x, then all operations and constants are calculated or processed in the best precition, that is, long double, but only the final result is rounded to the type that x has.

This behaviour reduces the immpact of numerical errors.

In order to know details about the floating point types, you have to watch the constant macros provided by the standard header <float.h>.
For example, see this link:

Çharacteristics of floating point types


In the sad case that your implementation does not conform to the IEEE 754 standard, you can try looking for details in the standard header <float.h>, if it exists.
Also, you have to read the documentation of your compiler.

For example, the compiler GCC explains what does with floating point:

Stadus of C99 features in GCC

查看更多
兄弟一词,经得起流年.
3楼-- · 2020-02-11 06:45

In C99, you can check for __STDC_IEC_559__:

#ifdef __STDC_IEC_559__
/* using IEEE-754 */
#endif

This is because the international floating point standard referenced by C99 is IEC 60559:989 (IEC 559 and IEEE-754 was a previous description). The mapping from the C language to IEC 60559 is optional, but if in use, the implementation defines the macro __STDC_IEC_559__ (Appendix F of the C99 standard), so you can totally rely on that.

Another alternative is to manually check if the values in float.h, such as FLT_MAX, FLT_EPSILON, FLT_MAX_10_EXP, etc, match with the IEEE-754 limits, although theoretically there could be another representation with the same values.

查看更多
贼婆χ
4楼-- · 2020-02-11 06:58

No simple test exists.

The overwhelming majority of systems today use IEEE-754 formats for floating-point. However, most C implementations do not fully conform to IEEE 754 (which is identical to IEC 60559) and do not set the preprocessor identifier __STDC_IEC_559__. In the absence of this identifier, the only way to determine whether a C implementation conforms to IEEE 754 is one or a combination of:

  • Read its documentation.
  • Examine its source code.
  • Test it (which is, of course, difficult when only exhaustive testing can be conclusive).

In many C implementations and software applications, the deviations from IEEE 754 can be ignored or worked around: You may write code as if IEEE 754 were in use, and much code will largely work. However, there are a variety of things that can trip up an unsuspecting programmer; writing completely correct floating-point code is difficult even when the full specification is obeyed.

Common deviations include:

  • Intermediate arithmetic is performed with more precision than the nominal type. E.g., expressions that use double values may be calculated with long double precision.
  • sqrt does not return a correctly rounded value in every case.
  • Other math library routines return values that may be slightly off (a few ULP) from the correctly rounded results. (In fact, nobody has implemented all the math routines recommended in IEEE 754-2008 with both guaranteed correct rounding and guaranteed bound run time.)
  • Subnormal numbers (tiny numbers near the edge of the floating-point format) may be converted to zero instead of handled as specified by IEEE 754.
  • Conversions between decimal numerals (e.g., 3.1415926535897932384626433 in the source code) and binary floating-point formats (e.g., the common double format, IEEE-754 64-bit binary) do not always round correctly, in either conversion direction.
  • Only round-to-nearest mode is supported; the other rounding modes specified in IEEE 754 are not supported. Or they may be available for simple arithmetic but require using machine-specific assembly language to access. Standard math libraries (cos, log, et cetera) rarely support other rounding modes.
查看更多
登录 后发表回答