hutiefang76 commented on code in PR #336:
URL: https://github.com/apache/commons-math/pull/336#discussion_r4364718991
##########
commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/ml/distance/CanberraDistance.java:
##########
@@ -31,9 +31,16 @@ public double compute(double[] a, double[] b) {
MathArrays.checkEqualLength(a, b);
double sum = 0;
for (int i = 0; i < a.length; i++) {
+ final double absA = JdkMath.abs(a[i]);
+ final double absB = JdkMath.abs(b[i]);
final double num = JdkMath.abs(a[i] - b[i]);
- final double denom = JdkMath.abs(a[i]) + JdkMath.abs(b[i]);
- sum += num == 0.0 && denom == 0.0 ? 0.0 : num / denom;
+ final double denom = absA + absB;
+ if (Double.isInfinite(denom) && Double.isFinite(a[i]) &&
Double.isFinite(b[i])) {
+ // Rescale large finite coordinates to avoid intermediate
overflow.
+ sum += JdkMath.abs(a[i] * 0.5 - b[i] * 0.5) / (absA * 0.5 +
absB * 0.5);
+ } else {
+ sum += num == 0.0 && denom == 0.0 ? 0.0 : num / denom;
Review Comment:
Changed to `denom == 0.0 ? 0.0 : num / denom` in `7eaa1d03`. A zero sum of
the absolute values means both coordinates are zero, so the numerator check was
redundant. The 8 Canberra tests pass, including signed zero and subnormal
cases. `mvn clean verify` also passes locally on Java 17/macOS arm64 (3798
tests, 33 skips).
The branch already includes current master `a9aabf60` through merge
`dadaf4cd`, including the `macos-latest` and Zulu/Java 8 runner changes
(`56a8f696` and `a9aabf60`). Both are ancestors of this commit. Hosted CI still
needs to run on the new head.
<sub>AI-generated reply; validation figures are from the local execution
logs.</sub>
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]