# lucene-issues mailing list archives

##### Site index · List index
Message view
Top
From GitBox <...@apache.org>
Subject [GitHub] [lucene-solr] bruno-roustant commented on a change in pull request #1043: LUCENE-9071: Speed up BM25 scores.
Date Mon, 02 Dec 2019 09:28:28 GMT
```bruno-roustant commented on a change in pull request #1043: LUCENE-9071: Speed up BM25 scores.
URL: https://github.com/apache/lucene-solr/pull/1043#discussion_r352489912

##########
File path: lucene/core/src/java/org/apache/lucene/util/MathUtil.java
##########
@@ -165,4 +165,32 @@ public static double sumRelativeErrorBound(int numValues) {
return (numValues - 1) * u;
}

+  /**
+   * If this returns {@code true} then it means that the sum {@code x + y}
+   * doesn't need rounding to fit in a float. This method assumes two positive
+   * floats. No assumptions can be made if this method returns {@code false}.
+   */
+  public static boolean isSumAccurate(float x, float y) {
+    assert x >= 0;
+    assert y >= 0;
+    int xBits = Float.floatToRawIntBits(x);
+    int yBits = Float.floatToRawIntBits(y);
+    int minBits = Math.min(xBits, yBits);
+    int maxBits = Math.max(xBits, yBits);
+    int maxBiasedExp = maxBits >>> 23;
+    int minBiasedExp = minBits >>> 23;
+    // The result of the sum will have an exponent that is equal to the
+    // exponent of the maximum value, or the exponent of the maximum value plus
+    // one. So if we can do a lossless conversion of both numbers to a float
+    // that has the maximum exponent plus one as an exponent, then the sum is
+    // accurate.
+    int expDelta = maxBiasedExp - minBiasedExp;
+    final boolean accurate = (maxBits & 0x01) == 0 &&
+        // beware of overflows, a biased exponent of 254 is the greatest exponent that is
not Infinity or NaN
+        maxBiasedExp < 254 &&
+        expDelta <= 22 &&
+        Integer.numberOfTrailingZeros(minBits) > expDelta;
+    return accurate;
+  }

Review comment:
+1, even if I appreciate the technique to determine if a float sum is accurate.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

users@infra.apache.org

With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org