benwtrent commented on code in PR #12281: URL: https://github.com/apache/lucene/pull/12281#discussion_r1225418442
########## lucene/core/src/java/org/apache/lucene/util/VectorUtilDefaultProvider.java: ########## @@ -85,50 +87,78 @@ public float dotProduct(float[] a, float[] b) { + b[i + 6] * a[i + 6] + b[i + 7] * a[i + 7]; } - return res; + float resF = (float) res; + checkFinite(resF, a, b, "dot product"); + return resF; } @Override public float cosine(float[] a, float[] b) { - float sum = 0.0f; - float norm1 = 0.0f; - float norm2 = 0.0f; + double sum = 0.0; + double norm1 = 0.0; + double norm2 = 0.0; int dim = a.length; for (int i = 0; i < dim; i++) { - float elem1 = a[i]; - float elem2 = b[i]; + double elem1 = a[i]; + double elem2 = b[i]; sum += elem1 * elem2; norm1 += elem1 * elem1; norm2 += elem2 * elem2; } - return (float) (sum / Math.sqrt(norm1 * norm2)); + float r = (float) (sum / Math.sqrt(norm1 * norm2)); + checkFinite(r, a, b, "cosine"); + return r; } @Override public float squareDistance(float[] a, float[] b) { - float squareSum = 0.0f; + double squareSum = 0.0; int dim = a.length; int i; for (i = 0; i + 8 <= dim; i += 8) { squareSum += squareDistanceUnrolled(a, b, i); } for (; i < dim; i++) { - float diff = a[i] - b[i]; + double diff = a[i] - b[i]; squareSum += diff * diff; } - return squareSum; + float r = (float) squareSum; + checkFinite(r, a, b, "square distance"); + return r; + } + + private static void checkFinite(float r, float[] a, float[] b, String optype) { Review Comment: it is indeed the caller's responsibility to ensure the passed vectors have valid values. We cannot do validations, even in exceptional cases. -- 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. To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org For additional commands, e-mail: issues-h...@lucene.apache.org