public void ALongSubTest() { Assert.AreEqual("9886", AMath.Sub(new ALong(10509), new ALong(623)).ToString()); Assert.AreEqual("999", AMath.Sub(new ALong(1000), new ALong(1)).ToString()); Assert.AreEqual("1000", AMath.Sub(new ALong(999), new ALong(-1)).ToString()); Assert.AreEqual("-1000", AMath.Sub(new ALong(-1), new ALong(999)).ToString()); Assert.AreEqual("998", AMath.Sub(new ALong(-1), new ALong(-999)).ToString()); Assert.AreEqual("2", AMath.Sub(new ALong("1"), new ALong(-1)).ToString()); Assert.AreEqual("-1", AMath.Sub(new ALong("99"), new ALong(100)).ToString()); var a = new ALong(10); a -= 2; Assert.AreEqual("8", a.ToString()); a -= 8; Assert.AreEqual("0", a.ToString()); a += 10; a -= 15; Assert.AreEqual("-5", a.ToString()); a -= "100000000000000000000000000000000000000000000005"; Assert.AreEqual("-100000000000000000000000000000000000000000000010", a.ToString()); a += "11"; Assert.AreEqual("-99999999999999999999999999999999999999999999999", a.ToString()); Assert.IsTrue(a == "-99999999999999999999999999999999999999999999999"); Assert.IsFalse(a > "-99999999999999999999999999999999999999999999999"); Assert.IsTrue(a >= "-99999999999999999999999999999999999999999999999"); }
public void ALongSumTest() { Assert.AreEqual("1248", (new ALong("00001239") + new ALong("000009")).ToString()); Assert.AreEqual("10", (new ALong("1") + new ALong("9")).ToString()); Assert.AreEqual("180", (new ALong("90") + new ALong("90")).ToString()); Assert.AreEqual("100000000000000000000000000000000000000000000005", (new ALong("100000000000000000000000000000000000000000000000") + new ALong("5")).ToString()); var a = new ALong("10"); // Add string (aware of number pasing errors) a += "20"; Assert.AreEqual("30", a.ToString()); // Add int a += 10; Assert.AreEqual("40", a.ToString()); // Add long a += 10L; Assert.AreEqual("50", a.ToString()); Assert.AreEqual("10000", (new ALong("1") + new ALong("9999")).ToString()); Assert.AreEqual("-1", (new ALong("0") + new ALong("-1")).ToString()); Assert.AreEqual("-999", (new ALong("1") + new ALong("-1000")).ToString()); a += -60; Assert.AreEqual("-10", a.ToString()); }
/// <summary> /// Calculates a quotient and remainder of division of a two ALong instances. /// </summary> /// <param name="num1">Divident</param> /// <param name="num2">Divisor</param> /// <returns>A Tuple of Quotient and Remainder.</returns> public static Tuple<ALong, ALong> Divide(ALong num1, ALong num2) { var num2a = Abs(num2); if (num2a < Int32.MaxValue) { return DivInt(num1, num2.ToString().ToInt()); } if (num2 == 0) { throw new ArgumentException("Division by zero"); } if (num1 == 0) { return new Tuple<ALong, ALong>(new ALong(0), new ALong(0)); } var num1a = Abs(num1); if (num1a == num2a) { return new Tuple<ALong, ALong>(new ALong(1).SetNegative(num1.IsNegative != num2.IsNegative), new ALong(0)); } if (num1a < num2a) { return new Tuple<ALong, ALong>(new ALong(0), new ALong(num1)); } var b = 10; // base var down = new ALong(0); // Get upper limit var up = new ALong(b); while (num1a > num2a * up) { up = up * b; } // Divide while (up - down > 1) { var cur = (down + up) / 2; var c = num2a * (cur); if (c < num1a) { down = cur; continue; } if (c > num1a) { up = cur; continue; } if (c == num1a) { down = cur; up = cur; continue; } } var remainder = num1a - down * num2a; down.SetNegative(num1.IsNegative != num2.IsNegative); remainder.SetNegative(num1.IsNegative); return new Tuple<ALong, ALong>(down, remainder); }
public void ALongSubTest() { Assert.AreEqual("9886", AMath.Sub(new ALong(10509), new ALong(623)).ToString()); Assert.AreEqual("999", AMath.Sub(new ALong(1000), new ALong(1)).ToString()); Assert.AreEqual("1000", AMath.Sub(new ALong(999), new ALong(-1)).ToString()); Assert.AreEqual("-1000", AMath.Sub(new ALong(-1), new ALong(999)).ToString()); Assert.AreEqual("998", AMath.Sub(new ALong(-1), new ALong(-999)).ToString()); Assert.AreEqual("2", AMath.Sub(new ALong("1"), new ALong(-1)).ToString()); Assert.AreEqual("-1", AMath.Sub(new ALong("99"), new ALong(100)).ToString()); var a = new ALong(10); a -= 2; Assert.AreEqual("8", a.ToString()); a -= 8; Assert.AreEqual("0", a.ToString()); a += 10; a -= 15; Assert.AreEqual("-5", a.ToString()); a -= "100000000000000000000000000000000000000000000005"; Assert.AreEqual("-100000000000000000000000000000000000000000000010", a.ToString()); a += "11"; Assert.AreEqual("-99999999999999999999999999999999999999999999999", a.ToString()); Assert.IsTrue(a == "-99999999999999999999999999999999999999999999999"); Assert.IsFalse(a > "-99999999999999999999999999999999999999999999999"); Assert.IsTrue(a >= "-99999999999999999999999999999999999999999999999"); }
public void ALongSumTest() { Assert.AreEqual("1248", (new ALong("00001239") + new ALong("000009")).ToString()); Assert.AreEqual("10", (new ALong("1") + new ALong("9")).ToString()); Assert.AreEqual("180", (new ALong("90") + new ALong("90")).ToString()); Assert.AreEqual("100000000000000000000000000000000000000000000005", (new ALong("100000000000000000000000000000000000000000000000") + new ALong("5")).ToString()); var a = new ALong("10"); // Add string (aware of number pasing errors) a += "20"; Assert.AreEqual("30", a.ToString()); // Add int a += 10; Assert.AreEqual("40", a.ToString()); // Add long a += 10L; Assert.AreEqual("50", a.ToString()); Assert.AreEqual("10000", (new ALong("1") + new ALong("9999")).ToString()); Assert.AreEqual("-1", (new ALong("0") + new ALong("-1")).ToString()); Assert.AreEqual("-999", (new ALong("1") + new ALong("-1000")).ToString()); a += -60; Assert.AreEqual("-10", a.ToString()); }