Fix broken NaN check in MovingFunctions#stdDev() (#31888)

The initial check will never be true, because of the special semantics of NaN,
where no value is equal to Nan, including NaN. Thus, x == Double.NaN always
evaluates to false. The method still works correct because later computations
will also return NaN if the avg argument is NaN, but the intended shortcut
doesn't work.
This commit is contained in:
Christoph Büscher 2018-07-10 09:34:17 +02:00 committed by GitHub
parent 1c32497c44
commit 2ac7e49924
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 1 deletions

View File

@ -85,7 +85,7 @@ public class MovingFunctions {
* The average is based on the count of non-null, non-NaN values.
*/
public static double stdDev(double[] values, double avg) {
if (avg == Double.NaN) {
if (Double.isNaN(avg)) {
return Double.NaN;
} else {
long count = 0;

View File

@ -313,6 +313,10 @@ public class MovFnWhitelistedFunctionTests extends ESTestCase {
assertThat(actual, equalTo(Double.NaN));
}
public void testStdDevNaNAvg() {
assertThat(MovingFunctions.stdDev(new double[] { 1.0, 2.0, 3.0 }, Double.NaN), equalTo(Double.NaN));
}
public void testLinearMovAvg() {
int numValues = randomIntBetween(1, 100);