Consider the following test program:
# include <gsl/gsl_statistics_double.h>
# include <iostream>
using namespace std;
int main()
{
double y = 50.2944, yc = 63.2128;
double pearson_corr = gsl_stats_correlation(&y, 1, &yc, 1, 1);
cout << "pearson_corr = " << pearson_corr << endl;
if (isnan(pearson_corr))
cout << "It is nan" << endl;
else
cout << "Not nan" << endl;
}
In some way, this code is some absurd, but its purpose is to show a subtle error that I am experiencing.
The call to gsl_stats_correlation()
should give an error because the number of samples is 1 and the pearson coefficient has sense for at least two samples.
When I compile thus:
c++ test-r2.cc -lgsl -lgslcblas
the programs prints out -nan
as result and the message "It is nan", which I consider correct because as I said it is not possible to compute the coefficient. The call to isnan()
detects correctly that the result is nan
. However, when I compile thus:
c++ -Ofast test-r2.cc -lgsl -lgslcblas
The program prints out -nan
as result but the message "Not nan", which suggests that the call to isnan()
was unable to detect the invalidity of pearson_corr
variable.
So, my first question es "Why with the -Ofast
flag the call to isnan()
is not able to detect that the variable is nan
. And my second question would be how to solve this problem in a way independent on the optimizations flags given to the compiler?
I am using gnu c++ version 5.4.0 on ubuntu 16.04 and an Intel i5 run at 64 bits mode
Thanks in advance