Skip to content

Commit 4d2d91f

Browse files
authored
Fix exponential histogram midpoint computation (#133719)
1 parent b5f5dcd commit 4d2d91f

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

libs/exponential-histogram/src/main/java/org/elasticsearch/exponentialhistogram/ExponentialScaleUtils.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,9 +234,19 @@ static double exponentiallyScaledToDoubleValue(long index, int scale) {
234234
*/
235235
public static double getPointOfLeastRelativeError(long bucketIndex, int scale) {
236236
checkIndexAndScaleBounds(bucketIndex, scale);
237-
double upperBound = getUpperBucketBoundary(bucketIndex, scale);
238237
double histogramBase = Math.pow(2, Math.scalb(1, -scale));
239-
return 2 / (histogramBase + 1) * upperBound;
238+
if (Double.isFinite(histogramBase)) {
239+
double upperBound = getUpperBucketBoundary(bucketIndex, scale);
240+
return 2 / (histogramBase + 1) * upperBound;
241+
} else {
242+
if (bucketIndex >= 0) {
243+
// the bucket is (1, +inf), approximate point of least error as inf
244+
return Double.POSITIVE_INFINITY;
245+
} else {
246+
// the bucket is (1/(Inf), 1), approximate point of least error as 0
247+
return 0;
248+
}
249+
}
240250
}
241251

242252
/**

libs/exponential-histogram/src/test/java/org/elasticsearch/exponentialhistogram/ExponentialScaleUtilsTests.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@ public void testExtremeValueIndexing() {
7979
}
8080
}
8181

82+
public void testPointOfLeastErrorAtInfinity() {
83+
assertThat(getPointOfLeastRelativeError(0, MIN_SCALE), equalTo(Double.POSITIVE_INFINITY));
84+
assertThat(getPointOfLeastRelativeError(-1, MIN_SCALE), equalTo(0.0));
85+
assertThat(getPointOfLeastRelativeError(10, MIN_SCALE + 2), equalTo(Double.POSITIVE_INFINITY));
86+
}
87+
8288
public void testRandomValueIndexing() {
8389
for (int i = 0; i < 100_000; i++) {
8490
// generate values in the range 10^-100 to 10^100

0 commit comments

Comments
 (0)