How to check if C++ compiler uses IEEE 754 floating point standard
Asked Answered
C

2

36

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.

Castiron answered 25/4, 2011 at 10:31 Comment(4)
at runtime you can use std::numeric_limits<double>::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.Edy
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.Tlaxcala
@AlexanderGessler At runtime? Are you sure? en.cppreference.com/w/cpp/types/numeric_limits/is_iec559Hymenium
@cubuspl42: It's constexpr now but you're replying to a comment that predates C++11Centroid
D
44

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++.

Donall answered 25/4, 2011 at 10:46 Comment(6)
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.Angelikaangelina
@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.Donall
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.Angelikaangelina
@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.Hymenium
@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.Angelikaangelina
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 ;-)Angelikaangelina
P
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
Pedate answered 6/10, 2019 at 19:11 Comment(3)
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 <math.h> and then test for the define. Where is it defined in C++?Nashoma
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.Pedate
There is a bit of an explanation in this answer: https://mcmap.net/q/271395/-ensuring-c-doubles-are-64-bitsBroadspectrum

© 2022 - 2024 — McMap. All rights reserved.