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:
parent
1c32497c44
commit
2ac7e49924
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue