Skip to content

Commit

Permalink
Fix broken NaN check in MovingFunctions#stdDev() (#31888)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Christoph Büscher authored Jul 10, 2018
1 parent 1c32497 commit 2ac7e49
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public static double unweightedAvg(double[] values) {
* 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,10 @@ public void testEmptySimpleStdDev() {
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);
Expand Down

0 comments on commit 2ac7e49

Please sign in to comment.