Skip to content

Commit 284a20a

Browse files
author
Christoph Büscher
committed
Fix broken NaN check in MovingFunctions#stdDev() (elastic#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.
1 parent 74fddb3 commit 284a20a

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

server/src/main/java/org/elasticsearch/search/aggregations/pipeline/movfn/MovingFunctions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public static double unweightedAvg(double[] values) {
8585
* The average is based on the count of non-null, non-NaN values.
8686
*/
8787
public static double stdDev(double[] values, double avg) {
88-
if (avg == Double.NaN) {
88+
if (Double.isNaN(avg)) {
8989
return Double.NaN;
9090
} else {
9191
long count = 0;

server/src/test/java/org/elasticsearch/search/aggregations/pipeline/movfn/MovFnWhitelistedFunctionTests.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,10 @@ public void testEmptySimpleStdDev() {
313313
assertThat(actual, equalTo(Double.NaN));
314314
}
315315

316+
public void testStdDevNaNAvg() {
317+
assertThat(MovingFunctions.stdDev(new double[] { 1.0, 2.0, 3.0 }, Double.NaN), equalTo(Double.NaN));
318+
}
319+
316320
public void testLinearMovAvg() {
317321

318322
int numValues = randomIntBetween(1, 100);

0 commit comments

Comments
 (0)