示例代码:
#include <iostream>
#include <cmath>
#include <stdint.h>
using namespace std;
static bool my_isnan(double val) {
union { double f; uint64_t x; } u = { val };
return (u.x << 1) > 0x7ff0000000000000u;
}
int main() {
cout << std::isinf(std::log(0.0)) << endl;
cout << std::isnan(std::sqrt(-1.0)) << endl;
cout << my_isnan(std::sqrt(-1.0)) << endl;
cout << __isnan(std::sqrt(-1.0)) << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
使用-ffast-math,该代码打印"0,0,1,1" - 没有,它打印"1,1,1,1".
那是对的吗?在这些情况下,我认为std::isinf/ std::isnan应该仍然可以使用-ffast-math.
另外,我如何检查无限/ NaN -ffast-math?您可以看到my_isnan这样做,它确实有效,但该解决方案当然非常依赖于架构.另外,为什么在 …