You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by ja...@apache.org on 2023/02/01 09:52:51 UTC

[lucene] 03/03: Adjust return type for VectorUtil methods (#12122)

This is an automated email from the ASF dual-hosted git repository.

javanna pushed a commit to branch branch_9x
in repository https://gitbox.apache.org/repos/asf/lucene.git

commit 6711a805e430cf355f55168785d628fb4066128b
Author: Luca Cavanna <ja...@apache.org>
AuthorDate: Wed Feb 1 10:48:05 2023 +0100

    Adjust return type for VectorUtil methods (#12122)
    
    Two of the methods (squareDistance and dotProduct) that take byte arrays return a float while
    the variable used to store the value is an int. They can just return an int.
---
 .../src/java/org/apache/lucene/index/VectorSimilarityFunction.java    | 2 +-
 lucene/core/src/java/org/apache/lucene/util/VectorUtil.java           | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/lucene/core/src/java/org/apache/lucene/index/VectorSimilarityFunction.java b/lucene/core/src/java/org/apache/lucene/index/VectorSimilarityFunction.java
index d5ebcabb7a3..3646cf65584 100644
--- a/lucene/core/src/java/org/apache/lucene/index/VectorSimilarityFunction.java
+++ b/lucene/core/src/java/org/apache/lucene/index/VectorSimilarityFunction.java
@@ -37,7 +37,7 @@ public enum VectorSimilarityFunction {
 
     @Override
     public float compare(byte[] v1, byte[] v2) {
-      return 1 / (1 + squareDistance(v1, v2));
+      return 1 / (1f + squareDistance(v1, v2));
     }
   },
 
diff --git a/lucene/core/src/java/org/apache/lucene/util/VectorUtil.java b/lucene/core/src/java/org/apache/lucene/util/VectorUtil.java
index 8800d434ba6..2a08436ec0b 100644
--- a/lucene/core/src/java/org/apache/lucene/util/VectorUtil.java
+++ b/lucene/core/src/java/org/apache/lucene/util/VectorUtil.java
@@ -181,7 +181,7 @@ public final class VectorUtil {
   }
 
   /** Returns the sum of squared differences of the two vectors. */
-  public static float squareDistance(byte[] a, byte[] b) {
+  public static int squareDistance(byte[] a, byte[] b) {
     // Note: this will not overflow if dim < 2^18, since max(byte * byte) = 2^14.
     int squareSum = 0;
     for (int i = 0; i < a.length; i++) {
@@ -249,7 +249,7 @@ public final class VectorUtil {
    * @param b bytes containing another vector, of the same dimension
    * @return the value of the dot product of the two vectors
    */
-  public static float dotProduct(byte[] a, byte[] b) {
+  public static int dotProduct(byte[] a, byte[] b) {
     assert a.length == b.length;
     int total = 0;
     for (int i = 0; i < a.length; i++) {