Consider this code (file testnan.cxx)
#include <iostream>
#include <limits>
#include <cfenv>
void test(double d) {
std::feclearexcept(FE_ALL_EXCEPT);
double dd = d+1;
std::cout << std::fetestexcept(FE_ALL_EXCEPT) << '\n';
}
void nantest() {
test(std::numeric_limits<double>::quiet_NaN());
test(std::numeric_limits<double>::signaling_NaN());
}
int main(){nantest();}
Depending on how I execute it, I can get 3 different results:
a)
$ clang++-3.9 -std=c++14 ~/test/nantest.cxx && ./a.out
0
1
b) in ROOT (From heads/master@v6-09-02-1807-g616eba4, Jul 27 2017)
$ root -b -q ~/test/nantest.cxx
Processing ~/test/nantest.cxx...
0
0
c) interactive:
root [0] double d=numeric_limits<double>::quiet_NaN();
root [1] feclearexcept(FE_ALL_EXCEPT);
root [2] double dd = d+1;
root [3] fetestexcept(FE_ALL_EXCEPT)
(int) 33
// 33 = FE_INVALID + FE_INEXACT
// the same for signaling, however the value seems to come from elsewhere
root [4] feclearexcept(FE_ALL_EXCEPT);
root [5] fetestexcept(FE_ALL_EXCEPT)
(int) 32
// 32 = FE_INEXACT
I can understand that c) is different, probably the state is changed between the commands. However, shouldn’t a) and b) give identical results?