Skip to content

Commit dabf3a5

Browse files
author
Luc Maisonobe
committed
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
1 parent 4564adb commit dabf3a5

3 files changed

Lines changed: 7 additions & 1 deletion

File tree

src/java/org/apache/commons/math/util/MathUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ public static double factorialLog(final int n) {
409409
* @since 1.1
410410
*/
411411
public static int gcd(int u, int v) {
412-
if (u * v == 0) {
412+
if ((u == 0) || (v == 0)) {
413413
return (Math.abs(u) + Math.abs(v));
414414
}
415415
// keep u and v negative, as negative integers range down to

src/site/xdoc/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ The <action> type attribute can be add,update,fix,remove.
3939
</properties>
4040
<body>
4141
<release version="2.0" date="TBD" description="TBD">
42+
<action dev="luc" type="fix" issue="MATH-238" due-to="Chritian Semrau">
43+
Fixed an error in gcd computation for large values.
44+
</action>
4245
<action dev="luc" type="add" >
4346
Added method to walk matrix entries with or without changing them in the
4447
visitor design pattern sense. Three different orders can be used, row by row,

src/test/org/apache/commons/math/util/MathUtilsTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,9 @@ public void testGcd() {
291291
assertEquals(1, MathUtils.gcd(-a, c));
292292
assertEquals(1, MathUtils.gcd(a, -c));
293293
assertEquals(1, MathUtils.gcd(-a, -c));
294+
295+
assertEquals(3 * (1<<15), MathUtils.gcd(3 * (1<<20), 9 * (1<<15)));
296+
294297
}
295298

296299
public void testHash() {

0 commit comments

Comments
 (0)