From dabf3a5beb9ab697d570154b9961078a8586c787 Mon Sep 17 00:00:00 2001 From: Luc Maisonobe Date: Fri, 16 Jan 2009 23:06:32 +0000 Subject: [PATCH] fixed overflow error in gdc computation JIRA: MATH-238 git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@735178 13f79535-47bb-0310-9956-ffa450edef68 --- src/java/org/apache/commons/math/util/MathUtils.java | 2 +- src/site/xdoc/changes.xml | 3 +++ src/test/org/apache/commons/math/util/MathUtilsTest.java | 3 +++ 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/java/org/apache/commons/math/util/MathUtils.java b/src/java/org/apache/commons/math/util/MathUtils.java index c5342aeb3..df714052f 100644 --- a/src/java/org/apache/commons/math/util/MathUtils.java +++ b/src/java/org/apache/commons/math/util/MathUtils.java @@ -409,7 +409,7 @@ public final class MathUtils { * @since 1.1 */ public static int gcd(int u, int v) { - if (u * v == 0) { + if ((u == 0) || (v == 0)) { return (Math.abs(u) + Math.abs(v)); } // keep u and v negative, as negative integers range down to diff --git a/src/site/xdoc/changes.xml b/src/site/xdoc/changes.xml index 8e0dc75e2..388df56a6 100644 --- a/src/site/xdoc/changes.xml +++ b/src/site/xdoc/changes.xml @@ -39,6 +39,9 @@ The type attribute can be add,update,fix,remove. + + Fixed an error in gcd computation for large values. + Added method to walk matrix entries with or without changing them in the visitor design pattern sense. Three different orders can be used, row by row, diff --git a/src/test/org/apache/commons/math/util/MathUtilsTest.java b/src/test/org/apache/commons/math/util/MathUtilsTest.java index 0aad8f54f..dd99e9135 100644 --- a/src/test/org/apache/commons/math/util/MathUtilsTest.java +++ b/src/test/org/apache/commons/math/util/MathUtilsTest.java @@ -291,6 +291,9 @@ public final class MathUtilsTest extends TestCase { assertEquals(1, MathUtils.gcd(-a, c)); assertEquals(1, MathUtils.gcd(a, -c)); assertEquals(1, MathUtils.gcd(-a, -c)); + + assertEquals(3 * (1<<15), MathUtils.gcd(3 * (1<<20), 9 * (1<<15))); + } public void testHash() {