6

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.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
user3171859
  • 83
  • 2
  • 8
  • See PARANOIA: http://people.sc.fsu.edu/~%20jburkardt/c_src/paranoia/paranoia.c – Pascal Cuoq Jan 28 '14 at 11:28
  • I think you have an issue. it is the word *simple* – UmNyobe Jan 28 '14 at 11:35
  • I have seen paranoia but i need something more simple because the code cannot be compiled on my platform which runs freertos.Half of my time will be wasted in porting .Something more simple please. – user3171859 Jan 28 '14 at 11:35
  • what porting issues are you facing? The main effort is in changing the test conditions – UmNyobe Jan 28 '14 at 11:38
  • Read the docs of the compiler that you are using. – David Heffernan Jan 28 '14 at 11:48
  • Now paranoia is an interactive program which i will be not able to acheive on my platform (There is no shell) . I am looking all over it again and seeing if there is a portable piece of code that i can use it to my ends. I am also realising the fact that indeed there cannot be any simple solution.Let me llok into paranoia as well .By the way i also found out this cool link on the internet [http://www.math.utah.edu/~beebe/software/ieee/#c-with-fortran] – user3171859 Jan 28 '14 at 11:49
  • And I am using C99 @selarer – user3171859 Jan 28 '14 at 12:00

4 Answers4

7

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.
Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • My system does not conform to the standards i found out. Only exhaustive testing can be conclusive as you have said. Also My system implements some features and does not some others. – user3171859 Jan 29 '14 at 11:03
  • Do you have more details about "sqrt does not return a correctly rounded value in every case.""? – Z boson Mar 23 '14 at 12:03
3

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.

Filipe Gonçalves
  • 20,783
  • 6
  • 53
  • 70
  • The #ifdef returns as false can anything be inferred from that ? – user3171859 Jan 28 '14 at 12:00
  • @user3171859 Yes, it means the implementation is not using IEEE-754 floats. Otherwise, the macro is required to be defined – Filipe Gonçalves Jan 28 '14 at 12:03
  • 6
    The C standard does not require that implementations that conform to IEEE 754 (as epic field in Annex F) define `__STDC_IEC_559__`. It requires that an implementation that defines `__STDC_IEC_559__` conform to IEEE 754. – Eric Postpischil Jan 28 '14 at 12:15
  • 2
    Many C implementations use the IEEE 754 floating-point formats and perform many operations as specified by IEEE 754 but do not completely conform and do not define `__STD_IEC_559__`. – Eric Postpischil Jan 28 '14 at 12:16
  • Do you absolutely know this stuff , If you do i will mark ur answer as the right one ?The macro is failing for sure. So can i infer that my system does not support IEEE 754 floats. – user3171859 Jan 28 '14 at 12:17
  • 2
    @user3171859 No, you can't, that's why the macro is useless. If the macro is defined, **then** you can infer that you have IEEE 754. If it isn't, then you can't say anything - it may or may not have it. It's not an equivalence relation, which (like many other things in the standard) makes it useless in real life. Basically, if A implies B, then "not B" implies "not A", but B need not imply A :) – Thomas Apr 05 '14 at 07:51
  • thanks for the reply though but the issue has been long sorted.Thanks again. @Thomas – user3171859 Apr 07 '14 at 16:22
3

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

pablo1977
  • 4,281
  • 1
  • 15
  • 41
  • I think `FLT_EVAL_METHOD` only applies to `float_f` and `double_f`, not `float` and `double`. See also: N1256 7.12/2 and http://stackoverflow.com/questions/5390011/whats-the-point-of-float-t-and-when-should-it-be-used – Ciro Santilli OurBigBook.com Jun 15 '15 at 15:26
  • @CiroSantilli六四事件法轮功纳米比亚胡海峰: Look at [Standard C11](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf), in clause 5.2.4.2.2/8, where it defines the values for `FLT_EVAL_METHOD`. Clearly is talking about `float, double, long double` and in general is referred to any floating point type. – pablo1977 Jun 15 '15 at 22:26
  • True! Same for C99. Thanks. – Ciro Santilli OurBigBook.com Jun 16 '15 at 03:53
0

No, Standard C18, p. 373 specifies that IEC 60559 is used for float, double...

Why do you think IEEE 754 is used?

71GA
  • 1,132
  • 6
  • 36
  • 69