如何检查 C++ 编译器是否使用 IEEE 754 浮点标准

我想问一个这个之后的问题定义检查编译器是否使用标准很好地回答了.但是,这仅适用于 C.有没有办法在 C++ 中做同样的事情?

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.

推荐答案

其实你有一个更简单的方法可以在 C++ 中实现这一点.从 C++ 标准 18.2.1.1 开始,numeric_limits 类存在于 std 中.为了访问所述静态成员,您只需执行以下操作:

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;

或者:

std::numeric_limits<float>::is_iec559;

如果正在使用 IEEE 754,则应该返回 true,否则返回 false.

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

作为替代方法,亚当的回答 对于 C++ 也应该这样做.

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

相关文章