[System.Numerics] BigInteger.GCD(0,x) must return abs(x) instead of x
authorChristoph Ruegg <git@cdrnet.ch>
Wed, 12 Jun 2013 21:58:42 +0000 (23:58 +0200)
committerChristoph Ruegg <git@cdrnet.ch>
Wed, 12 Jun 2013 22:06:41 +0000 (00:06 +0200)
According to the docs and matching the behavior in .Net, BigInteger's
GreatestCommonDivisor must return the absolute value of the non-zero
parameter if one of the parameters is zero.

Contains a fix and extended unit tests to cover the case.

mcs/class/System.Numerics/System.Numerics/BigInteger.cs
mcs/class/System.Numerics/System.Numerics/ChangeLog
mcs/class/System.Numerics/Test/System.Numerics/BigIntegerTest.cs
mcs/class/System.Numerics/Test/System.Numerics/ChangeLog

index 1a32f7eceaeab0efb71acbbb00c0fb6924861376..816ca2ffceeb3a6a250e8b1f682e77919a3f658d 100644 (file)
@@ -1725,9 +1725,9 @@ namespace System.Numerics {
                        if (right.sign != 0 && right.data.Length == 1 && right.data [0] == 1)
                                return new BigInteger (1, ONE);
                        if (left.IsZero)
-                               return right;
+                               return Abs(right);
                        if (right.IsZero)
-                               return left;
+                               return Abs(left);
 
                        BigInteger x = new BigInteger (1, left.data);
                        BigInteger y = new BigInteger (1, right.data);
index 7634bc04a15cb28f162afc55ea5ab2558294f7fe..02bc81a4533a275892a918af2d6424a9c9c155a8 100644 (file)
@@ -1,3 +1,8 @@
+2013-06-12 Christoph Ruegg <git@cdrnet.ch>
+
+       * BigInteger.cs: Fix GreatestCommonDivisor to
+       return absolute value if one of the args is zero.
+
 2013-06-09 Christoph Ruegg <git@cdrnet.ch>
 
        * Complex.cs: Fix IFormattable.ToString to pass custom
index dd5afb61204a451ae5c16b059a338e65eb9eff98..098c50854b78f1be97ebd61df42710541e7ccbbc 100644 (file)
@@ -150,6 +150,9 @@ namespace MonoTests.System.Numerics
                        Assert.AreEqual (2, (int)BigInteger.GreatestCommonDivisor (-12345678, -8765432), "#14");
 
                        Assert.AreEqual (40, (int)BigInteger.GreatestCommonDivisor (5581 * 40, 6671 * 40), "#15");
+
+                       Assert.AreEqual (5, (int)BigInteger.GreatestCommonDivisor (-5, 0), "#16");
+                       Assert.AreEqual (5, (int)BigInteger.GreatestCommonDivisor (0, -5), "#17");
                }
 
                [Test]
index 12d9d30e1a2f4c9c590b7bb0908d65dc6e33ab53..ec7ae1f94b4cf85f9518da5f72c4ce43323a6bc6 100644 (file)
@@ -1,3 +1,8 @@
+2013-06-13 Christoph Ruegg <git@cdrnet.ch>
+
+       * BigIntegerTest.cs: Extended tests for
+       GreatestCommonDivisor to cover zero-args.
+
 2013-06-09 Christoph Ruegg <git@cdrnet.ch>
 
        * ComplexTest.cs: Created; Tests ToString