35

I would like to ask a question that follows this one which is pretty well answered by the define check if the compiler uses the standard. However this woks for C only. Is there a way to do the same in C++?

I do not wish to covert floating point types to text or use some pretty complex conversion functions. I just need the compiler check. If you know a list of such compatible compilers please post the link. I could not find it.

Community
  • 1
  • 1
Rusty Horse
  • 2,388
  • 7
  • 26
  • 38
  • 7
    at runtime you can use `std::numeric_limits::is_iec559()` to check if a particular floating point type is represented according to IEEE 754. Of course that says little about whether the compiler's floating point handling is 754 conformant, but it should give you a good hint. – Alexander Gessler Apr 25 '11 at 10:44
  • I think it's less a matter of the compiler supporting it and more a matter of the CPU's FPU supporting it... but I'm not 100% confident of this so I'm making a comment and not an answer. – Max E. Apr 25 '11 at 10:46
  • 2
    @AlexanderGessler At runtime? Are you sure? http://en.cppreference.com/w/cpp/types/numeric_limits/is_iec559 – cubuspl42 Mar 03 '18 at 19:52
  • 1
    @cubuspl42: It's `constexpr` *now* but you're replying to a comment that predates C++11 – Ben Voigt Aug 14 '23 at 16:31

2 Answers2

40

Actually you have an easier way to achieve this in C++. From the C++ standard 18.2.1.1 the class numeric_limits exists within std. In order to access said static member you simply do this:

std::numeric_limits<double>::is_iec559;

Or:

std::numeric_limits<float>::is_iec559;

Which should return true if IEEE 754 is in use, false otherwise.

As an alternative method, the second part of Adam's answer should do it also for C++.

Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • 3
    In practice this isn't wholly reliable, though. Call it non-conforming if you like, but compilers don't necessarily *know* whether their floating-point types are IEEE754, because they produce machine code that runs on multiple variants of an architecture, some of which conform in every detail of ulp-precision and using the right rounding mode, while others don't. – Steve Jessop Apr 25 '11 at 12:22
  • 1
    @Steve a fair point. I don't know of any other reasonably standard/sane way to test this, unfortunately, other than working out which architecture you're on and adding checks based on whether the chip family you're executing on is known to be fully conforming. –  Apr 25 '11 at 12:37
  • 1
    I think it's fine for the purpose, all the questioner seems to actually care about is the format, not conformance to the whole of IEC559. You might get a false negative from a compiler that conservatively says it doesn't implement IEC559, when it does enough for our purposes. Just worth bearing in mind this is actually a difficult thing for compilers to deal with, so be on the lookout for weirdness. – Steve Jessop Apr 25 '11 at 12:41
  • @SteveJessop Could you be more precise? Are you talking about popular architectures like x86 or ARM, or something more exotic? I believe that if the programmer's manual of your CPU architecture doesn't strictly define floating point operations, `is_iec559` should be false. Or, depending on some compiler switch, it should be software emulated and `true` or IEEE 754-incompatible and `false`. It doesn't seem like a conceptual problem to me, but maybe I'm missing something. – cubuspl42 Mar 03 '18 at 19:50
  • 1
    @cubuspl42: I'm talking about the case where `is_iec559` is `false` because the type is only 99.99% compatible with IEEE 754. I can't remember why I thought this at the time, but based on my comments above I thought that what the questioner really need to know was whether IEEE format was in use, not whether IEEE semantics were precisely followed. Hence there will be false negatives, but since what the questioner really wanted to know isn't formally defined there's no way out of that. – Steve Jessop Apr 13 '18 at 12:49
  • Oh, I think the reason I thought that is because the linked question that this OP refers to is only talking about format, not about whether every last ulp on every operation is correct. Anyway, I think I have also seen stone cold bugs where the implementer (usually platform porter) didn't manage to get `is_iec559` false for some nearly-but-not-quite IEEE 754 conformant target. These days popular architectures probably do get it right, kind of as a requirement of remaining popular ;-) – Steve Jessop Apr 13 '18 at 12:51
0

Although this post is a bit old (10 years), you could still try this. It also works in C:

#ifndef __STDC_IEC_559__
#error The following Programm only supports float operations using the IEEE 754 Standard
#endif
AKJ
  • 950
  • 2
  • 13
  • 18
  • Do you have any source for this? Is `__STD_IEC_559__` defined by the compiler itself? In C99 it is defined in math.h, so you must first `#include ` and then test for the define. Where is it defined in C++? – wovano Feb 25 '20 at 10:47
  • It is also defined in stdc-predef.h which is gotten from stdint.h, which is also used in C++ (even from iostream). I suppose, i assumed that the user included one of these libraries. – AKJ Feb 25 '20 at 12:24
  • 1
    There is a bit of an explanation in this answer: https://stackoverflow.com/a/753018/8634018 – Dominik Mokriš Mar 13 '20 at 14:53