Prevent corr() from returning the wrong results for negative correlation
authorNeil Conway <[email protected]>
Wed, 19 Sep 2007 22:31:51 +0000 (22:31 +0000)
committerNeil Conway <[email protected]>
Wed, 19 Sep 2007 22:31:51 +0000 (22:31 +0000)
values. The previous coding essentially assumed that x = sqrt(x*x), which
does not hold for x < 0.

Thanks to Jie Zhang at Greenplum and Gavin Sherry for reporting this
issue.

src/backend/utils/adt/float.c

index 6814714419d9b621c8c72cdee54db07802d69d85..692c2ca8148c873c326f40035957cf7455cd9bc4 100644 (file)
@@ -2469,8 +2469,7 @@ float8_corr(PG_FUNCTION_ARGS)
        if (numeratorX <= 0 || numeratorY <= 0)
                PG_RETURN_NULL();
 
-       PG_RETURN_FLOAT8(sqrt((numeratorXY * numeratorXY) /
-                                                 (numeratorX * numeratorY)));
+       PG_RETURN_FLOAT8(numeratorXY / sqrt(numeratorX * numeratorY));
 }
 
 Datum