- double df = pair->n -2;
- double correlation_t = (pair->correlation * sqrt (df) /
- sqrt (1 - pow2 (pair->correlation)));
+
+ /* corr2 will mathematically always be in the range [0, 1.0]. Inaccurate
+ calculations sometimes cause it to be slightly greater than 1.0, so
+ force it into the correct range to avoid NaN from sqrt(). */
+ double corr2 = MIN (1.0, pow2 (pair->correlation));
+ double correlation_t = pair->correlation * sqrt (df) / sqrt (1 - corr2);