0

I would like to make assumptions about the representation of floating points, and I would like to verify at compile time using a static_assert whether the floating point representations are IEEE-754 since I transfer them through the network.

How could I make sure using a static_assert that the float and double types are in the format specified in IEEE-754?

Julius
  • 1,155
  • 9
  • 19
  • 3
    If all you care about is the representation of floating-point numbers, not full conformance to IEEE-754 behavior, then testing the characteristics defined in `` may be useful. `FLT_RADIX` should be 2, `FLT_MANT_DIG` should be 24 (and `DBL_MANT_DIG` should be 53), `FLT_MIN_EXP` should be −125 (−1021 for `DBL`), and `FLT_MAX_EXP` should be 128 (1024 for `DBL`). (The exponents are off by one from IEEE-754 documentation because uses a significand in [1, 2), but C bases it on [½, 1).) – Eric Postpischil Nov 20 '18 at 14:32
  • You may also want to check `FLT_HAS_SUBNORM` and `DBL_HAS_SUBNORM`, which should be 1. However, Apple LLVM 9.1.0 with clang-902.0.39.2 is not setting these, in spite of them being defined to be 1 in ``. The compiler appears to be using some built-in `float.h` instead of the actual file in the SDK. – Eric Postpischil Nov 20 '18 at 14:34
  • Touching a bit on behavior, you can consider `FLT_ROUNDS` (defined to be 1 if the rounding mode is to-nearest) and `FLT_EVAL_METHOD`, which you would like to be 0 for “evaluate all operations and constants just to the range and precision of the type.” That one may be the most troublesome of the above; it is not uncommon for implementations to use −1 (indeterminable) or 1 or 2 (evaluate using `double` or `long double`). There may be compiler switches to adjust this, so you may be able to get `FLT_EVAL_METHOD` 0 with a switch. – Eric Postpischil Nov 20 '18 at 14:37
  • And check that the radix is 2 ;-) – Antti Haapala -- Слава Україні Nov 20 '18 at 14:38

1 Answers1

1

C99 has a macro __STD_IEC_559__ in order to check if the floating point representation comply with IEEE 754.

The issue is that some compilers do not define this macro.

Acording to this wikipedia article you could check like follows:

#ifndef __STDC_IEC_559__ 
puts("Warning: __STDC_IEC_559__ not defined. IEEE 754 floating point not fully supported."); 
// #endif
bogdan tudose
  • 1,064
  • 1
  • 9
  • 21
  • That would be awesome! It could be a clean solution if compilers added it sometime in future. I am afraid it doesn't help me much right now if it doesn't work in practice :-(. – Julius Nov 20 '18 at 13:46
  • 1
    `__STDC_IEC_559__` does not check if the floating-point representation conforms to IEEE 754. It asserts that the C implementation conforms to IEC 60559, effectively IEEE 754—meaning not just the floating-point representation but also the operations and other behavior. Few implementations fully conform, so few implementations define `__STDC_IEC_559__`. But many implementations use IEEE-754 representations (formats). So `__STDC_IEC_559__` is not a useful way of testing what the representation is nor of determining how well an implementation conforms. – Eric Postpischil Nov 20 '18 at 14:11
  • 1
    Also note it is `__STDC_IEC_559__`, not `__STD_IEC_559__`. – Eric Postpischil Nov 20 '18 at 14:11