From 3d93011e32011a6300c73eeff24de37b17acd5bd Mon Sep 17 00:00:00 2001 From: Igor Motov Date: Tue, 19 Feb 2019 16:02:11 -0500 Subject: [PATCH] Fix median calculation in MedianAbsoluteDeviationAggregatorTests (#38979) Fixes an error in median calculation in MedianAbsoluteDeviationAggregatorTests for odd number of sample points, which causes some rare test failures. Fixes #38937 --- .../MedianAbsoluteDeviationAggregatorTests.java | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/metrics/MedianAbsoluteDeviationAggregatorTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/metrics/MedianAbsoluteDeviationAggregatorTests.java index 55cf9b16e16..a422c41700b 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/metrics/MedianAbsoluteDeviationAggregatorTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/metrics/MedianAbsoluteDeviationAggregatorTests.java @@ -260,9 +260,15 @@ public class MedianAbsoluteDeviationAggregatorTests extends AggregatorTestCase { private static double calculateMedian(double[] sample) { final double[] sorted = Arrays.copyOf(sample, sample.length); Arrays.sort(sorted); - - final int halfway = (int) Math.ceil(sorted.length / 2d); - final double median = (sorted[halfway - 1] + sorted[halfway]) / 2d; + final int halfway = (int) Math.ceil(sorted.length / 2d); + final double median; + if (sorted.length % 2 == 0) { + // even + median = (sorted[halfway - 1] + sorted[halfway]) / 2d; + } else { + // odd + median = (sorted[halfway - 1]); + } return median; }