aherbert commented on code in PR #348:
URL: https://github.com/apache/commons-math/pull/348#discussion_r4459279258


##########
commons-math-legacy-core/src/main/java/org/apache/commons/math4/legacy/core/MathArrays.java:
##########
@@ -273,14 +276,17 @@ public static double distanceInf(double[] p1, double[] 
p2) {
      *
      * @param p1 the first point
      * @param p2 the second point
-     * @return the L<sub>&infin;</sub> distance between the two points
+     * @return the L<sub>&infin;</sub> distance between the two points, or
+     * {@link Integer#MAX_VALUE} if the distance is too large to be
+     * represented as an {@code int}
      * @throws DimensionMismatchException if the array lengths differ.
      */
     public static int distanceInf(int[] p1, int[] p2) {
         checkEqualLength(p1, p2);
         int max = 0;
         for (int i = 0; i < p1.length; i++) {
-            max = JdkMath.max(max, JdkMath.abs(p1[i] - p2[i]));
+            final long delta = JdkMath.abs((long) p1[i] - p2[i]);
+            max = (int) JdkMath.min(JdkMath.max(max, delta), 
Integer.MAX_VALUE);

Review Comment:
   Compute max as a long and then convert to int after the loop for the return



##########
commons-math-legacy-core/src/main/java/org/apache/commons/math4/legacy/core/MathArrays.java:
##########
@@ -203,14 +203,17 @@ public static double distance1(double[] p1, double[] p2) {
      *
      * @param p1 the first point
      * @param p2 the second point
-     * @return the L<sub>1</sub> distance between the two points
+     * @return the L<sub>1</sub> distance between the two points, or
+     * {@link Integer#MAX_VALUE} if the distance is too large to be
+     * represented as an {@code int}
      * @throws DimensionMismatchException if the array lengths differ.
      */
     public static int distance1(int[] p1, int[] p2) {
         checkEqualLength(p1, p2);
         int sum = 0;
         for (int i = 0; i < p1.length; i++) {
-            sum += JdkMath.abs(p1[i] - p2[i]);
+            final long delta = JdkMath.abs((long) p1[i] - p2[i]);

Review Comment:
   Compute sum as a long. Then do a final check against MAX_VALUE after the 
loop on the return.
   
   The sum cannot overflow a long as two max length arrays with MIN in one and 
MAX in the other is (2^31-1) * (2^32-1) < 2^63.



-- 
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]

Reply via email to