simpleqt commented on PR #348:
URL: https://github.com/apache/commons-math/pull/348#issuecomment-6614171656
Done in 37e1259 — both methods now accumulate in a `long` and only clamp on
return:
- `distance1(int[], int[])`: `long sum` accumulates `Math.abs((long) p1[i] -
p2[i])`; the return is `sum > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int)
sum`.
- `distanceInf(int[], int[])`: `long max` accumulates the same way; the
return converts after the loop.
Your bound is what makes the single check at the end sufficient: `(2^31 - 1)
* (2^32 - 1) < 2^63`, so the accumulator cannot overflow a `long` even for two
maximum-length arrays. I kept that reasoning in the commit message rather than
in the code, since the credit for it is yours.
Verified with `mvn -pl commons-math-legacy-core test -Dtest=MathArraysTest`
— 47 tests, 0 failures, 0 errors (the module's `install` also builds clean);
the new tests cover `{MAX_VALUE}` vs `{-1}` for both methods, the two-element
accumulation case `{MAX_VALUE, 0}` vs `{0, MAX_VALUE}`, and the argument-order
symmetry. The `double[]` overloads are untouched.
--
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]