aherbert commented on code in PR #336:
URL: https://github.com/apache/commons-math/pull/336#discussion_r4361077061
##########
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:
I don't think numerator can ever be non-zero if the denominator is zero.
This check could be:
```java
denom == 0.0 ? 0.0 : num / denom;
```
It is then testing for `a[i] == b[i] == 0`.
--
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]