diff --git a/build.vc11/mpir.net/mpir.net-tests/HugeFloatTests/Arithmetic.cs b/build.vc11/mpir.net/mpir.net-tests/HugeFloatTests/Arithmetic.cs deleted file mode 100644 index 3570f1f5..00000000 --- a/build.vc11/mpir.net/mpir.net-tests/HugeFloatTests/Arithmetic.cs +++ /dev/null @@ -1,371 +0,0 @@ -/* -Copyright 2014 Alex Dyachenko - -This file is part of the MPIR Library. - -The MPIR Library is free software; you can redistribute it and/or modify -it under the terms of the GNU Lesser General Public License as published -by the Free Software Foundation; either version 3 of the License, or (at -your option) any later version. - -The MPIR Library is distributed in the hope that it will be useful, but -WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public -License for more details. - -You should have received a copy of the GNU Lesser General Public License -along with the MPIR Library. If not, see http://www.gnu.org/licenses/. -*/ - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -namespace MPIR.Tests.HugeFloatTests -{ - public static class FloatAssert - { - public static void AreEqual(string expected, HugeFloat actual) - { - var actualStr = actual.ToString(); - if(expected[0] == '-') - { - Assert.AreEqual(expected[0], actualStr[0]); - actualStr = actualStr.TrimStart('-'); - expected = expected.TrimStart('-'); - } - - var exponent = expected.IndexOf('.'); - expected = expected.Replace(".", ""); - - var exponentStr = "@" + exponent; - - Assert.IsTrue(actualStr.StartsWith("0.")); - actualStr = actualStr.Substring(2); - - Assert.IsTrue(actualStr.EndsWith(exponentStr)); - actualStr = actualStr.Substring(0, actualStr.Length - exponentStr.Length); - - if (expected.Length > actualStr.Length) - { - var roundedUp = expected[actualStr.Length] >= '5'; - expected = expected.Substring(0, actualStr.Length); - if(roundedUp) - { - using (var a = new HugeInt(expected)) - { - a.Value += 1; - expected = a.ToString(10); - } - } - } - Assert.AreEqual(expected, actualStr); - } - } - - [TestClass] - public class Arithmetic - { - #region Add - - [TestMethod] - public void FloatAddHugeFloat() - { - using (var a = new HugeFloat("22250983250345029834502983.5740293845720")) - using (var b = new HugeFloat("2229874359879827.30594288574029879874539")) - using (var c = new HugeFloat(a + b)) - { - FloatAssert.AreEqual("22250983252574904194382810.87997227031229879874539", c); - c.Value = a + (b + 1); - FloatAssert.AreEqual("22250983252574904194382811.87997227031229879874539", c); - } - } - - [TestMethod] - public void FloatAddLimb() - { - using (var a = new HugeFloat("22250983250345029834502983.5740293845720")) - using (var c = new HugeFloat()) - { - var b = 4288574029879874539UL; - c.Value = a + b; - FloatAssert.AreEqual("22250987538919059714377522.5740293845720", c); - c.Value = b + a; - FloatAssert.AreEqual("22250987538919059714377522.5740293845720", c); - } - } - - [TestMethod] - public void FloatAddSignedLimb() - { - using (var a = new HugeFloat("22250983250345029834502983.5740293845720")) - using (var c = new HugeFloat()) - { - var b = -4288574029879874539; - c.Value = a + b; - FloatAssert.AreEqual("22250978961770999954628444.5740293845720", c); - c.Value = b + a; - FloatAssert.AreEqual("22250978961770999954628444.5740293845720", c); - } - } - - #endregion - - #region Subtract - - [TestMethod] - public void FloatSubtractHugeFloat() - { - using (var a = new HugeFloat("22250983250345029834502983.5740293845720")) - using (var b = new HugeFloat("2229874359879827.30594288574029879874539")) - using (var c = new HugeFloat(a - b)) - { - FloatAssert.AreEqual("22250983248115155474623156.26808649883170120125461", c); - c.Value = b - (a + 1); - FloatAssert.AreEqual("-22250983248115155474623157.26808649883170120125461", c); - } - } - - [TestMethod] - public void FloatSubtractLimb() - { - using(var a = new HugeFloat("22250983250345029834502983.5740293845720")) - using(var c = new HugeFloat()) - { - var b = 4288574029879874539UL; - c.Value = a - b; - FloatAssert.AreEqual("22250978961770999954628444.5740293845720", c); - c.Value = b - a; - FloatAssert.AreEqual("-22250978961770999954628444.5740293845720", c); - } - } - - [TestMethod] - public void FloatSubtractSignedLimb() - { - using(var a = new HugeFloat("22250983250345029834502983.5740293845720")) - using(var c = new HugeFloat()) - { - var b = -4288574029879874539; - c.Value = a - b; - FloatAssert.AreEqual("22250987538919059714377522.5740293845720", c); - c.Value = b - a; - FloatAssert.AreEqual("-22250987538919059714377522.5740293845720", c); - } - } - - #endregion - - #region Multiply - - [TestMethod] - public void FloatMultiplyByHugeFloat() - { - using (var a = new HugeFloat("90234098723098475098479385.345098345")) - using (var b = new HugeFloat("78594873598734.59872354987610987897")) - using (var c = new HugeFloat(a * b)) - { - FloatAssert.AreEqual("7091937583437663707014199538801620613535.95657143399816050772069730465", c); - c.Value = b * (a + 1); - FloatAssert.AreEqual("7091937583437663707014199617396494212270.55529498387427038669069730465", c); - } - } - - [TestMethod] - public void FloatMultiplyByLimb() - { - using (var a = new HugeFloat("9023409872309847509847.9385345098345")) - using (var c = new HugeFloat()) - { - ulong b = 17390538260286101342; - c.Value = a * b; - FloatAssert.AreEqual("156921954622647727368660197878904460649174.746962647899", c); - c.Value = b * -a; - FloatAssert.AreEqual("-156921954622647727368660197878904460649174.746962647899", c); - } - } - - [TestMethod] - public void FloatMultiplyBySignedLimb() - { - using (var a = new HugeFloat("9023409872309847509847.9385345098345")) - using (var c = new HugeFloat()) - { - long b = -7390538260286101342; - c.Value = a * b; - FloatAssert.AreEqual("-66687855899549252270180812533806115649174.746962647899", c); - c.Value = b * -a; - FloatAssert.AreEqual("66687855899549252270180812533806115649174.746962647899", c); - } - } - - #endregion - - #region Shift Left - - [TestMethod] - public void FloatShiftLeft() - { - using (var a = new HugeFloat("-12345700987ABA245230948.17607EF", 16)) - using (var e = new HugeFloat("-12345700987ABA24523094817607.EF", 16)) - { - ulong b = 20; - a.Value = a << b; - Assert.AreEqual(e, a); - } - } - - #endregion - - #region Shift Right - - [TestMethod] - public void FloatShiftRight() - { - using (var a = new HugeFloat("-12345700987ABA24523094817607.EF", 16)) - using (var e = new HugeFloat("-12345700987ABA245230948.17607EF", 16)) - { - ulong b = 20; - a.Value = a >> b; - Assert.AreEqual(e, a); - } - } - - #endregion - - #region Negate - - [TestMethod] - public void FloatNegate() - { - using(var a = new HugeFloat("9023409872309847509847.9385345098345")) - { - a.Value = -a; - FloatAssert.AreEqual("-9023409872309847509847.9385345098345", a); - a.Value = -a; - FloatAssert.AreEqual("9023409872309847509847.9385345098345", a); - } - } - - #endregion - - #region Abs - - [TestMethod] - public void FloatMakeAbsolute() - { - using(var a = new HugeFloat("-9023409872309847509847.9385345098345")) - { - a.Value = a.Abs(); - FloatAssert.AreEqual("9023409872309847509847.9385345098345", a); - a.Value = a.Abs(); - FloatAssert.AreEqual("9023409872309847509847.9385345098345", a); - } - } - - #endregion - - #region Power - - [TestMethod] - public void FloatPower() - { - using(var a = new HugeFloat("-902340.945098345")) - { - a.Value = a ^ 5; - FloatAssert.AreEqual("-598209523815275040074985233466.4619735146023546465747916785912044", a); - } - } - - #endregion - - #region Sqrt - - [TestMethod] - public void FloatSqrt() - { - using(var a = new HugeFloat("9023404035982394058230584.9434590783455098345")) - { - a.Value = a.SquareRoot(); - FloatAssert.AreEqual("3003898140081.0504277325488426221152130989784856425363142756", a); - } - } - - [TestMethod] - public void FloatSqrtLimb() - { - using(var a = new HugeFloat()) - { - a.Value = HugeFloat.SquareRoot(2540928740928357403UL); - FloatAssert.AreEqual("1594029090.3645257511790832426801336140951314995369452070983", a); - } - } - - #endregion - - #region Divide - - #region Int - - [TestMethod] - public void FloatDivideHugeFloat() - { - using (var a = new HugeFloat("1157569866683036578989624354347957.394580293847")) - using (var b = new HugeFloat("593169091750307653294.549782395235784")) - { - a.Value = a / b; - FloatAssert.AreEqual("1951500647593.2689953514865540344827449639493356367018584357", a); - } - } - - #endregion - - #region Limb - - [TestMethod] - public void FloatDivideLimb() - { - using (var a = new HugeFloat("1157569866683036578989624354347957.394580293847")) - { - ulong b = 5931690917503076532; - a.Value = a / b; - FloatAssert.AreEqual("195150064759326.89956625512472902395197480398952074748799190", a); - } - } - - [TestMethod] - public void FloatDivideSignedLimb() - { - using(var a = new HugeFloat("1157569866683036578989624354347957.394580293847")) - { - long b = -5931690917503076532; - a.Value = a / b; - FloatAssert.AreEqual("-195150064759326.89956625512472902395197480398952074748799190", a); - } - } - - [TestMethod] - public void FloatDivideLimbBy() - { - using(var a = new HugeFloat("11575698666830.39458029384723405203984572")) - { - ulong b = 5931690917503076532; - a.Value = b / a; - FloatAssert.AreEqual("512426.16866833708737257760720580856722540469109813901673959", a); - } - } - - [TestMethod] - public void FloatDivideSignedLimbBy() - { - using(var a = new HugeFloat("11575698666830.39458029384723405203984572")) - { - long b = -5931690917503076532; - a.Value = b / a; - FloatAssert.AreEqual("-512426.16866833708737257760720580856722540469109813901673959", a); - } - } - - #endregion - - #endregion - } -} diff --git a/build.vc11/mpir.net/mpir.net-tests/HugeFloatTests/Comparisons.cs b/build.vc11/mpir.net/mpir.net-tests/HugeFloatTests/Comparisons.cs deleted file mode 100644 index e006f930..00000000 --- a/build.vc11/mpir.net/mpir.net-tests/HugeFloatTests/Comparisons.cs +++ /dev/null @@ -1,649 +0,0 @@ -/* -Copyright 2014 Alex Dyachenko - -This file is part of the MPIR Library. - -The MPIR Library is free software; you can redistribute it and/or modify -it under the terms of the GNU Lesser General Public License as published -by the Free Software Foundation; either version 3 of the License, or (at -your option) any later version. - -The MPIR Library is distributed in the hope that it will be useful, but -WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public -License for more details. - -You should have received a copy of the GNU Lesser General Public License -along with the MPIR Library. If not, see http://www.gnu.org/licenses/. -*/ - -using System; -using Microsoft.VisualStudio.TestTools.UnitTesting; - -namespace MPIR.Tests.HugeFloatTests -{ - [TestClass] - public class Comparisons - { - #region CompareTo - - [TestMethod] - public void FloatCompareToHugeFloat() - { - using (var a = new HugeFloat("-22250983250345029834503.9835740293845721345345354")) - using (var b = new HugeFloat("22250983250345029834502.9835740293845721345345354")) - { - Assert.AreEqual(1, Math.Sign(b.CompareTo(a))); - Assert.AreEqual(-1,Math.Sign(a.CompareTo(b + 1))); - Assert.AreEqual(0, Math.Sign((a + 1).CompareTo(-b))); - Assert.AreEqual(1, Math.Sign(a.CompareTo(null))); - } - } - - [TestMethod] - public void FloatCompareToObject() - { - using (var a = new HugeFloat("-22250983250345029834503.9835740293845721345345354")) - using (var b = new HugeFloat("22250983250345029834502.9835740293845721345345354")) - { - Assert.AreEqual(1, Math.Sign(((IComparable)b).CompareTo((object)a))); - Assert.AreEqual(-1,Math.Sign(((IComparable)a).CompareTo((object)b))); - Assert.AreEqual(1, Math.Sign(((IComparable)a).CompareTo(null))); - Assert.AreEqual(0, Math.Sign(((IComparable)(a + 1)).CompareTo((object)-b))); - } - } - - [TestMethod] - public void FloatCompareToExpression() - { - using (var a = new HugeFloat("-22250983250345029834503.9835740293845721345345354")) - using (var b = new HugeFloat("22250983250345029834502.9835740293845721345345354")) - { - Assert.AreEqual(1, Math.Sign(((IComparable)b).CompareTo(a))); - Assert.AreEqual(-1,Math.Sign(((IComparable)a).CompareTo(b))); - Assert.AreEqual(1, Math.Sign(((IComparable)a).CompareTo(null))); - Assert.AreEqual(0, Math.Sign(((IComparable)(a + 1)).CompareTo(-b))); - } - } - - [TestMethod] - [ExpectedException(typeof(ArgumentException))] - public void FloatCompareToNonExpression() - { - using (var a = new HugeFloat("-222509832503450298345029835740293845721.57898962467957")) - { - a.CompareTo("abc"); - } - } - - [TestMethod] - public void FloatCompareToLimb() - { - using (var a = new HugeFloat("-222509821")) - { - ulong b = 222509820; - Assert.AreEqual(-1,Math.Sign(a.CompareTo(b))); - Assert.AreEqual(-1,Math.Sign(a.CompareTo(b + 1))); - Assert.AreEqual(0, Math.Sign((-(a + 1)).CompareTo(b))); - Assert.AreEqual(0, Math.Sign((-a).CompareTo(b + 1))); - Assert.AreEqual(1, Math.Sign((-a).CompareTo(b))); - } - } - - [TestMethod] - public void FloatCompareToSignedLimb() - { - using (var a = new HugeFloat("-222509821")) - { - long b = -222509820; - Assert.AreEqual(-1,Math.Sign(a.CompareTo(b))); - Assert.AreEqual(-1,Math.Sign(a.CompareTo(b + 1))); - Assert.AreEqual(0, Math.Sign((a + 1).CompareTo(b))); - Assert.AreEqual(0, Math.Sign(a.CompareTo(b - 1))); - Assert.AreEqual(1, Math.Sign(a.CompareTo(b - 2))); - } - } - - [TestMethod] - public void FloatCompareToSignedDouble() - { - using (var a = new HugeFloat("-222509821")) - { - double b = -222509820; - Assert.AreEqual(-1,Math.Sign(a.CompareTo(b))); - Assert.AreEqual(-1,Math.Sign(a.CompareTo(b + 1))); - Assert.AreEqual(0, Math.Sign((a + 1).CompareTo(b))); - Assert.AreEqual(0, Math.Sign(a.CompareTo(b - 1))); - Assert.AreEqual(1, Math.Sign(a.CompareTo(b - 1.1))); - } - } - - #endregion - - #region comparison operators with expr - - [TestMethod] - public void FloatOperatorLessThan() - { - using (var a = new HugeFloat("-2225098325034502983450.29835740293845721")) - using (var b = new HugeFloat("2225098325034502983450.29835740293845721")) - using (var c = new HugeFloat()) - { - c.Value = a; - Assert.IsTrue(a < b); - Assert.IsFalse(b < a); - Assert.IsFalse(a < c); - Assert.IsFalse(a > c); - Assert.IsFalse(a < null); - Assert.IsTrue(null < a); - } - } - - [TestMethod] - public void FloatOperatorLessThanOrEqual() - { - using (var a = new HugeFloat("-2225098325034502983451.29835740293845721")) - using (var b = new HugeFloat("2225098325034502983450.29835740293845721")) - using (var c = new HugeFloat()) - { - c.Value = a; - Assert.IsTrue(a <= b); - Assert.IsFalse(b <= a); - Assert.IsTrue(a <= c); - Assert.IsFalse(a <= null); - Assert.IsTrue(null <= a); - } - } - - [TestMethod] - public void FloatOperatorGreaterThan() - { - using (var a = new HugeFloat("-2225098325034502983451.29835740293845721")) - using (var b = new HugeFloat("2225098325034502983450.29835740293845721")) - using (var c = new HugeFloat()) - { - c.Value = a; - Assert.IsFalse(a > b); - Assert.IsTrue(b > a); - Assert.IsFalse(a > c); - Assert.IsTrue(a > null); - Assert.IsFalse(null > a); - } - } - - [TestMethod] - public void FloatOperatorGreaterThanOrEqual() - { - using (var a = new HugeFloat("-2225098325034502983451.29835740293845721")) - using (var b = new HugeFloat("2225098325034502983450.29835740293845721")) - using (var c = new HugeFloat()) - { - c.Value = a; - Assert.IsFalse(a >= b); - Assert.IsTrue(b >= a); - Assert.IsTrue(a >= c); - Assert.IsTrue(a >= null); - Assert.IsFalse(null >= a); - } - } - - #endregion - - #region comparison operators with limb - - [TestMethod] - public void FloatOperatorLessThanLimb() - { - using (var a = new HugeFloat("3845721")) - { - ulong c = 5432; - ulong b = 5432349587; - Assert.IsTrue(a < b); - Assert.IsFalse(b < a); - Assert.IsFalse(a < c); - Assert.IsTrue(c < a); - } - } - - [TestMethod] - public void FloatOperatorLessThanOrEqualLimb() - { - using (var a = new HugeFloat("3845721")) - { - ulong c = 5432; - ulong b = 5432349587; - ulong d = 3845721; - Assert.IsTrue(a <= b); - Assert.IsFalse(b <= a); - Assert.IsFalse(a <= c); - Assert.IsTrue(c <= a); - Assert.IsTrue(a <= d); - Assert.IsTrue(d <= a); - } - } - - [TestMethod] - public void FloatOperatorGreaterThanLimb() - { - using (var a = new HugeFloat("3845721")) - { - ulong c = 5432; - ulong b = 5432349587; - Assert.IsFalse(a > b); - Assert.IsTrue(b > a); - Assert.IsTrue(a > c); - Assert.IsFalse(c > a); - } - } - - [TestMethod] - public void FloatOperatorGreaterThanOrEqualLimb() - { - using (var a = new HugeFloat("3845721")) - { - ulong c = 5432; - ulong b = 5432349587; - ulong d = 3845721; - Assert.IsFalse(a >= b); - Assert.IsTrue(b >= a); - Assert.IsTrue(a >= c); - Assert.IsFalse(c >= a); - Assert.IsTrue(a >= d); - Assert.IsTrue(d >= a); - } - } - - #endregion - - #region comparison operators with signed limb - - [TestMethod] - public void FloatOperatorLessThanSignedLimb() - { - using (var a = new HugeFloat("-3845721")) - { - long c = -543254325432; - long b = -9587; - Assert.IsTrue(a < b); - Assert.IsFalse(b < a); - Assert.IsFalse(a < c); - Assert.IsTrue(c < a); - } - } - - [TestMethod] - public void FloatOperatorLessThanOrEqualSignedLimb() - { - using (var a = new HugeFloat("-3845721")) - { - long c = -543254325432; - long b = -9587; - long d = -3845721; - Assert.IsTrue(a <= b); - Assert.IsFalse(b <= a); - Assert.IsFalse(a <= c); - Assert.IsTrue(c <= a); - Assert.IsTrue(a <= d); - Assert.IsTrue(d <= a); - } - } - - [TestMethod] - public void FloatOperatorGreaterThanSignedLimb() - { - using (var a = new HugeFloat("-3845721")) - { - long c = -543254325432; - long b = -9587; - Assert.IsFalse(a > b); - Assert.IsTrue(b > a); - Assert.IsTrue(a > c); - Assert.IsFalse(c > a); - } - } - - [TestMethod] - public void FloatOperatorGreaterThanOrEqualSignedLimb() - { - using (var a = new HugeFloat("-3845721")) - { - long c = -543254325432; - long b = -9587; - long d = -3845721; - Assert.IsFalse(a >= b); - Assert.IsTrue(b >= a); - Assert.IsTrue(a >= c); - Assert.IsFalse(c >= a); - Assert.IsTrue(a >= d); - Assert.IsTrue(d >= a); - } - } - - #endregion - - #region comparison operators with double - - [TestMethod] - public void FloatOperatorLessThanDouble() - { - using (var a = new HugeFloat("-3845721")) - { - double c = -543254325432; - double b = -9587; - Assert.IsTrue(a < b); - Assert.IsFalse(b < a); - Assert.IsFalse(a < c); - Assert.IsTrue(c < a); - } - } - - [TestMethod] - public void FloatOperatorLessThanOrEqualDouble() - { - using (var a = new HugeFloat("-3845721")) - { - double c = -543254325432; - double b = -9587; - double d = -3845721; - Assert.IsTrue(a <= b); - Assert.IsFalse(b <= a); - Assert.IsFalse(a <= c); - Assert.IsTrue(c <= a); - Assert.IsTrue(a <= d); - Assert.IsTrue(d <= a); - } - } - - [TestMethod] - public void FloatOperatorGreaterThanDouble() - { - using (var a = new HugeFloat("-3845721")) - { - double c = -543254325432; - double b = -9587; - Assert.IsFalse(a > b); - Assert.IsTrue(b > a); - Assert.IsTrue(a > c); - Assert.IsFalse(c > a); - } - } - - [TestMethod] - public void FloatOperatorGreaterThanOrEqualDouble() - { - using (var a = new HugeFloat("-3845721")) - { - double c = -543254325432; - double b = -9587; - double d = -3845721; - Assert.IsFalse(a >= b); - Assert.IsTrue(b >= a); - Assert.IsTrue(a >= c); - Assert.IsFalse(c >= a); - Assert.IsTrue(a >= d); - Assert.IsTrue(d >= a); - Assert.IsFalse(d - 0.1 >= a); - } - } - - #endregion - - #region Equals - - [TestMethod] - public void FloatEqualsHugeFloat() - { - using (var a = new HugeFloat("-2225098325034502983451.29835740293845721")) - using (var b = new HugeFloat("2225098325034502983450.29835740293845721")) - { - Assert.IsFalse(b.Equals(a)); - Assert.IsFalse(a.Equals(b + 1)); - Assert.IsTrue((a + 1).Equals(-b)); - Assert.IsFalse(a.Equals(null)); - Assert.IsTrue(Equals(a + 1, -b)); - } - } - - [TestMethod] - public void FloatEqualsExpression() - { - using (var a = new HugeFloat("-2225098325034502983451.29835740293845721")) - using (var b = new HugeFloat("2225098325034502983450.29835740293845721")) - { - Assert.IsFalse(((IEquatable)b).Equals(a)); - Assert.IsFalse(((IEquatable)a).Equals(b)); - Assert.IsFalse(((IEquatable)a).Equals(null)); - Assert.IsTrue(((IEquatable)(a + 1)).Equals(-b)); - } - } - - [TestMethod] - public void FloatEqualsNonExpression() - { - using (var a = new HugeFloat("-2225098325034502983450.29835740293845721")) - { - Assert.IsFalse(a.Equals("abc")); - } - } - - [TestMethod] - public void FloatEqualsLimb() - { - using (var a = new HugeFloat("222509832503")) - { - ulong b = 222509832504; - Assert.IsFalse(a.Equals(b + 1)); - Assert.IsTrue(a.Equals(b - 1)); - Assert.IsTrue((a + 1).Equals(b)); - } - } - - [TestMethod] - public void FloatEqualsSignedLimb() - { - using (var a = new HugeFloat("-222509832505")) - { - long b = -222509832504; - Assert.IsFalse(a.Equals(b + 1)); - Assert.IsTrue(a.Equals(b - 1)); - Assert.IsTrue((a + 1).Equals(b)); - } - } - - [TestMethod] - public void FloatEqualsDouble() - { - using (var a = new HugeFloat("-222509832505")) - { - double b = -222509832504; - Assert.IsFalse(a.Equals(b + 1)); - Assert.IsTrue(a.Equals(b - 1)); - Assert.IsTrue((a + 1).Equals(b)); - Assert.IsFalse((a + 1).Equals(b + 0.1)); - } - } - - [TestMethod] - public void FloatEqualsHugeFloatApproximately() - { - using (var a = new HugeFloat("ABCDEF12948576AB49587.ACD34EFB345", 16)) - using (var b = new HugeFloat("ABCDEF12948576AB49587.ACD34EFB245", 16)) - { - Assert.IsTrue(a.Equals(b, 119)); - Assert.IsFalse(a.Equals(b, 120)); - Assert.IsTrue(a.Equals(b - 1, 83)); - Assert.IsFalse(a.Equals(b - 1, 84)); - Assert.IsTrue((a + 512).Equals(b, 74)); - Assert.IsFalse((a + 512).Equals(b, 75)); - - //same mantissa, different exponent should always return false - Assert.IsFalse(a.Equals(a >> 1, 119)); - Assert.IsFalse(a.Equals(a << 1, 119)); - Assert.IsFalse(a.Equals(a * 2, 119)); - Assert.IsFalse(a.Equals(a / 2, 119)); - } - } - - [TestMethod] - public void FloatRelDiff() - { - using (var a = new HugeFloat("1234523549876.24935230589472305894245")) - using (var b = new HugeFloat("1234523549541.45207354209357842979873")) - { - Assert.AreEqual(a.RelativeDifferenceFrom(b), (a - b).Abs() / a); - Assert.AreNotEqual(a.RelativeDifferenceFrom(b), (a - b).Abs() / b); - Assert.AreEqual(b.RelativeDifferenceFrom(a), (a - b).Abs() / b); - Assert.AreNotEqual(b.RelativeDifferenceFrom(a), (a - b).Abs() / a); - } - } - - #endregion - - #region Equality operators with expr - - [TestMethod] - public void FloatEqualsOperatorHugeFloat() - { - using (var a = new HugeFloat("-2225098325034502983451.29835740293845721")) - using (var b = new HugeFloat("2225098325034502983450.29835740293845721")) - { - Assert.IsFalse(b == a); - Assert.IsFalse(a == b + 1); - Assert.IsTrue(a + 1 == -b); - Assert.IsFalse(a == null); - } - } - - [TestMethod] - public void FloatNotEqualOperatorHugeFloat() - { - using (var a = new HugeFloat("-2225098325034502983451.29835740293845721")) - using (var b = new HugeFloat("2225098325034502983450.29835740293845721")) - { - Assert.IsTrue(b != a); - Assert.IsTrue(a != b + 1); - Assert.IsFalse(a + 1 != -b); - Assert.IsTrue(a != null); - } - } - - #endregion - - #region Equality operators with Limb - - [TestMethod] - public void FloatEqualsOperatorLimb() - { - using (var a = new HugeFloat("-835740293845721")) - { - ulong b = 835740293845720; - Assert.IsFalse(b == a); - Assert.IsFalse(a == b + 1); - Assert.IsTrue(-(a + 1) == b); - } - } - - [TestMethod] - public void FloatNotEqualOperatorLimb() - { - using (var a = new HugeFloat("-835740293845721")) - { - ulong b = 835740293845720; - Assert.IsTrue(b != a); - Assert.IsTrue(a != b + 1); - Assert.IsFalse(-(a + 1) != b); - } - } - - #endregion - - #region Equality operators with Signed Limb - - [TestMethod] - public void FloatEqualsOperatorSignedLimb() - { - using (var a = new HugeFloat("-835740293845721")) - { - long b = -835740293845720; - Assert.IsFalse(b == a); - Assert.IsFalse(a == b + 1); - Assert.IsTrue(a + 1 == b); - } - } - - [TestMethod] - public void FloatNotEqualOperatorSignedLimb() - { - using (var a = new HugeFloat("-835740293845721")) - { - long b = -835740293845720; - Assert.IsTrue(b != a); - Assert.IsTrue(a != b + 1); - Assert.IsFalse(a + 1 != b); - } - } - - #endregion - - #region Equality operators with Double - - [TestMethod] - public void FloatEqualsOperatorDouble() - { - using (var a = new HugeFloat("-835740293845721")) - { - double b = -835740293845720; - Assert.IsFalse(b == a); - Assert.IsFalse(a == b + 1); - Assert.IsTrue(a + 1 == b); - Assert.IsFalse(a + 1 == b + 0.1); - } - } - - [TestMethod] - public void FloatNotEqualOperatorDouble() - { - using (var a = new HugeFloat("-835740293845721")) - { - double b = -835740293845720; - Assert.IsTrue(b != a); - Assert.IsTrue(a != b + 1); - Assert.IsFalse(a + 1 != b); - Assert.IsTrue(a + 1 != b + 0.1); - } - } - - #endregion - - #region GetHashCode - - [TestMethod] - public void FloatGetHashCodeTest() - { - using (var a = new HugeFloat("-2225098325034502983450298357.40293845721")) - { - Assert.AreNotEqual(0, a.GetHashCode()); - Assert.AreEqual(a.GetHashCode(), (a + 0).GetHashCode()); - Assert.AreNotEqual(a.GetHashCode(), (-a).GetHashCode()); - } - } - - #endregion - - #region Sign - - [TestMethod] - public void FloatSign() - { - using (var a = new HugeFloat("-22250983250345029834.502983574029384572134354")) - { - Assert.AreEqual(-1, a.Sign()); - Assert.AreEqual(1, (-a).Sign()); - Assert.AreEqual(0, (a-a).Sign()); - } - } - - #endregion - //more tests coming here - } -} diff --git a/build.vc11/mpir.net/mpir.net-tests/mpir.net-tests.csproj b/build.vc11/mpir.net/mpir.net-tests/mpir.net-tests.csproj index 27a8f911..8995d327 100644 --- a/build.vc11/mpir.net/mpir.net-tests/mpir.net-tests.csproj +++ b/build.vc11/mpir.net/mpir.net-tests/mpir.net-tests.csproj @@ -94,6 +94,15 @@ + + HugeFloatTests\Arithmetic.cs + + + HugeFloatTests\Comparisons.cs + + + HugeFloatTests\ConstructionAndDisposal.cs + HugeIntTests\Arithmetic.cs @@ -157,8 +166,6 @@ Properties\AssemblyInfo.cs - - diff --git a/mpir.net/mpir.net-tests/HugeFloatTests/Arithmetic.cs b/mpir.net/mpir.net-tests/HugeFloatTests/Arithmetic.cs index c65050e8..3570f1f5 100644 --- a/mpir.net/mpir.net-tests/HugeFloatTests/Arithmetic.cs +++ b/mpir.net/mpir.net-tests/HugeFloatTests/Arithmetic.cs @@ -17,118 +17,93 @@ You should have received a copy of the GNU Lesser General Public License along with the MPIR Library. If not, see http://www.gnu.org/licenses/. */ -using System; using Microsoft.VisualStudio.TestTools.UnitTesting; -namespace MPIR.Tests.HugeRationalTests +namespace MPIR.Tests.HugeFloatTests { + public static class FloatAssert + { + public static void AreEqual(string expected, HugeFloat actual) + { + var actualStr = actual.ToString(); + if(expected[0] == '-') + { + Assert.AreEqual(expected[0], actualStr[0]); + actualStr = actualStr.TrimStart('-'); + expected = expected.TrimStart('-'); + } + + var exponent = expected.IndexOf('.'); + expected = expected.Replace(".", ""); + + var exponentStr = "@" + exponent; + + Assert.IsTrue(actualStr.StartsWith("0.")); + actualStr = actualStr.Substring(2); + + Assert.IsTrue(actualStr.EndsWith(exponentStr)); + actualStr = actualStr.Substring(0, actualStr.Length - exponentStr.Length); + + if (expected.Length > actualStr.Length) + { + var roundedUp = expected[actualStr.Length] >= '5'; + expected = expected.Substring(0, actualStr.Length); + if(roundedUp) + { + using (var a = new HugeInt(expected)) + { + a.Value += 1; + expected = a.ToString(10); + } + } + } + Assert.AreEqual(expected, actualStr); + } + } + [TestClass] public class Arithmetic { #region Add [TestMethod] - public void RationalAddHugeRational() + public void FloatAddHugeFloat() { - using (var a = new HugeRational("222509832503450298345029835740293845720/115756986668303657898962467957")) - using (var b = new HugeRational("222987435987982730594288574029879874539/590872612825179551336102196593")) - using (var c = new HugeRational(a + b)) + using (var a = new HugeFloat("22250983250345029834502983.5740293845720")) + using (var b = new HugeFloat("2229874359879827.30594288574029879874539")) + using (var c = new HugeFloat(a + b)) { - Assert.AreEqual(a.Numerator * b.Denominator + b.Numerator * a.Denominator, c.Numerator); - Assert.AreEqual(a.Denominator * b.Denominator, c.Denominator); + FloatAssert.AreEqual("22250983252574904194382810.87997227031229879874539", c); + c.Value = a + (b + 1); + FloatAssert.AreEqual("22250983252574904194382811.87997227031229879874539", c); } } [TestMethod] - public void RationalAddHugeInt() + public void FloatAddLimb() { - using (var a = new HugeRational("222509832503450298345029835740293845720/115756986668303657898962467957")) - using (var b = new HugeInt("222987435987982730594288574029879874539")) - using (var c = new HugeRational(a + b)) - { - Assert.AreEqual(a.Numerator + b * a.Denominator, c.Numerator); - Assert.AreEqual(a.Denominator, c.Denominator); - c.Value = (b + 1) + a; - Assert.AreEqual(a.Numerator + (b + 1) * a.Denominator, c.Numerator); - Assert.AreEqual(a.Denominator, c.Denominator); - } - } - - [TestMethod] - public void RationalAddLimb() - { - using (var a = new HugeRational("222509832503450298345029835740293845720/115756986668303657898962467957")) - using (var c = new HugeRational()) + using (var a = new HugeFloat("22250983250345029834502983.5740293845720")) + using (var c = new HugeFloat()) { var b = 4288574029879874539UL; c.Value = a + b; - Assert.AreEqual(a.Numerator + b * a.Denominator, c.Numerator); - Assert.AreEqual(a.Denominator, c.Denominator); - } - } - - [TestMethod] - public void RationalAddToLimb() - { - using (var a = new HugeRational("222509832503450298345029835740293845720/115756986668303657898962467957")) - using (var c = new HugeRational()) - { - var b = 4288574029879874539UL; + FloatAssert.AreEqual("22250987538919059714377522.5740293845720", c); c.Value = b + a; - Assert.AreEqual(a.Numerator + b * a.Denominator, c.Numerator); - Assert.AreEqual(a.Denominator, c.Denominator); + FloatAssert.AreEqual("22250987538919059714377522.5740293845720", c); } } [TestMethod] - public void RationalAddSignedLimb() + public void FloatAddSignedLimb() { - using (var a = new HugeRational("222509832503450298345029835740293845720/115756986668303657898962467957")) - using (var c = new HugeRational()) + using (var a = new HugeFloat("22250983250345029834502983.5740293845720")) + using (var c = new HugeFloat()) { - var b = -4288574029879874539L; + var b = -4288574029879874539; c.Value = a + b; - Assert.AreEqual(a.Numerator + b * a.Denominator, c.Numerator); - Assert.AreEqual(a.Denominator, c.Denominator); - } - } - - [TestMethod] - public void RationalAddToSignedLimb() - { - using (var a = new HugeRational("222509832503450298345029835740293845720/115756986668303657898962467957")) - using (var c = new HugeRational()) - { - var b = -4288574029879874539L; + FloatAssert.AreEqual("22250978961770999954628444.5740293845720", c); c.Value = b + a; - Assert.AreEqual(a.Numerator + b * a.Denominator, c.Numerator); - Assert.AreEqual(a.Denominator, c.Denominator); - } - } - - [TestMethod] - public void RationalAddToMaxSignedLimb() - { - using (var a = new HugeRational("222509832503450298345029835740293845720/115756986668303657898962467957")) - using (var c = new HugeRational()) - { - var b = long.MinValue; - c.Value = b + a; - Assert.AreEqual(a.Numerator + b * a.Denominator, c.Numerator); - Assert.AreEqual(a.Denominator, c.Denominator); - } - } - - [TestMethod] - public void RationalAddExpressionHugeRational() - { - using (var a = new HugeRational("222509832503450298345029835740293845720/115756986668303657898962467957")) - using (var b = new HugeRational("222987435987982730594288574029879874539/590872612825179551336102196593")) - using (var c = new HugeRational()) - { - c.Value = 1 + (a + b); - Assert.AreEqual(a.Numerator * b.Denominator + b.Numerator * a.Denominator + a.Denominator * b.Denominator, c.Numerator); - Assert.AreEqual(a.Denominator * b.Denominator, c.Denominator); + FloatAssert.AreEqual("22250978961770999954628444.5740293845720", c); } } @@ -137,101 +112,43 @@ namespace MPIR.Tests.HugeRationalTests #region Subtract [TestMethod] - public void RationalSubtractHugeRational() + public void FloatSubtractHugeFloat() { - using (var a = new HugeRational("445497268491433028939318409770173720259/115756986668303657898962467957")) - using (var b = new HugeRational("222987435987982730594288574029879874539/590872612825179551336102196593")) - using (var c = new HugeRational(a - b)) + using (var a = new HugeFloat("22250983250345029834502983.5740293845720")) + using (var b = new HugeFloat("2229874359879827.30594288574029879874539")) + using (var c = new HugeFloat(a - b)) { - Assert.AreEqual(a.Numerator * b.Denominator - b.Numerator * a.Denominator, c.Numerator); - Assert.AreEqual(a.Denominator * b.Denominator, c.Denominator); + FloatAssert.AreEqual("22250983248115155474623156.26808649883170120125461", c); + c.Value = b - (a + 1); + FloatAssert.AreEqual("-22250983248115155474623157.26808649883170120125461", c); } } [TestMethod] - public void RationalSubtractHugeInt() + public void FloatSubtractLimb() { - using (var a = new HugeRational("222509832503450298345029835740293845720/115756986668303657898962467957")) - using (var b = new HugeInt("222987435987982730594288574029879874539")) - using (var c = new HugeRational(a - b)) + using(var a = new HugeFloat("22250983250345029834502983.5740293845720")) + using(var c = new HugeFloat()) { - Assert.AreEqual(a.Numerator - b * a.Denominator, c.Numerator); - Assert.AreEqual(a.Denominator, c.Denominator); - c.Value = (b + 1) - a; - Assert.AreEqual((b + 1) * a.Denominator - a.Numerator, c.Numerator); - Assert.AreEqual(a.Denominator, c.Denominator); + var b = 4288574029879874539UL; + c.Value = a - b; + FloatAssert.AreEqual("22250978961770999954628444.5740293845720", c); + c.Value = b - a; + FloatAssert.AreEqual("-22250978961770999954628444.5740293845720", c); } } [TestMethod] - public void RationalSubtractLimb() + public void FloatSubtractSignedLimb() { - var b = 4288574029879874539UL; - using (var a = new HugeRational("222509832503450298349318409770173720259/115756986668303657898962467957")) - using (var c = new HugeRational(a - b)) + using(var a = new HugeFloat("22250983250345029834502983.5740293845720")) + using(var c = new HugeFloat()) { - Assert.AreEqual(a.Numerator - b * a.Denominator, c.Numerator); - Assert.AreEqual(a.Denominator, c.Denominator); - } - } - - [TestMethod] - public void RationalSubtractFromLimb() - { - var b = 4288574029879874539UL; - using (var a = new HugeRational("222509832503450298349318409770173720259/115756986668303657898962467957")) - using (var c = new HugeRational(b - a)) - { - Assert.AreEqual(b * a.Denominator - a.Numerator, c.Numerator); - Assert.AreEqual(a.Denominator, c.Denominator); - } - } - - [TestMethod] - public void RationalSubtractPositiveSignedLimb() - { - var b = 4288574029879874539L; - using (var a = new HugeRational("222509832503450298349318409770173720259/115756986668303657898962467957")) - using (var c = new HugeRational(a - b)) - { - Assert.AreEqual(a.Numerator - b * a.Denominator, c.Numerator); - Assert.AreEqual(a.Denominator, c.Denominator); - } - } - - [TestMethod] - public void RationalSubtractFromPositiveSignedLimb() - { - var b = 4288574029879874539L; - using (var a = new HugeRational("222509832503450298349318409770173720259/115756986668303657898962467957")) - using (var c = new HugeRational(a - b)) - { - Assert.AreEqual(a.Numerator - b * a.Denominator, c.Numerator); - Assert.AreEqual(a.Denominator, c.Denominator); - } - } - - [TestMethod] - public void RationalSubtractSignedLimb() - { - var b = -4288574029879874539L; - using (var a = new HugeRational("222509832503450298345029835740293845720/115756986668303657898962467957")) - using (var c = new HugeRational(a - b)) - { - Assert.AreEqual(a.Numerator - b * a.Denominator, c.Numerator); - Assert.AreEqual(a.Denominator, c.Denominator); - } - } - - [TestMethod] - public void RationalSubtractFromSignedLimb() - { - var b = -4288574029879874539L; - using (var a = new HugeRational("222509832503450298345029835740293845720/115756986668303657898962467957")) - using (var c = new HugeRational(b - a)) - { - Assert.AreEqual(b * a.Denominator - a.Numerator, c.Numerator); - Assert.AreEqual(a.Denominator, c.Denominator); + var b = -4288574029879874539; + c.Value = a - b; + FloatAssert.AreEqual("22250987538919059714377522.5740293845720", c); + c.Value = b - a; + FloatAssert.AreEqual("-22250987538919059714377522.5740293845720", c); } } @@ -240,73 +157,43 @@ namespace MPIR.Tests.HugeRationalTests #region Multiply [TestMethod] - public void RationalMultiplyByHugeRational() + public void FloatMultiplyByHugeFloat() { - using (var a = new HugeRational("90234098723098475098479385345098345/115756986668303657898962467957")) - using (var b = new HugeRational("7859487359873459872354987610987897/590872612825179551336102196593")) - using (var c = new HugeRational(a * b)) + using (var a = new HugeFloat("90234098723098475098479385.345098345")) + using (var b = new HugeFloat("78594873598734.59872354987610987897")) + using (var c = new HugeFloat(a * b)) { - Assert.AreEqual(a.Numerator * b.Numerator, c.Numerator); - Assert.AreEqual(a.Denominator * b.Denominator, c.Denominator); + FloatAssert.AreEqual("7091937583437663707014199538801620613535.95657143399816050772069730465", c); + c.Value = b * (a + 1); + FloatAssert.AreEqual("7091937583437663707014199617396494212270.55529498387427038669069730465", c); } } [TestMethod] - public void RationalMultiplytHugeInt() + public void FloatMultiplyByLimb() { - using (var a = new HugeRational("222509832503450298345029835740293845720/115756986668303657898962467957")) - using (var b = new HugeInt("222987435987982730594288574029879874539")) - using (var c = new HugeRational(a * b)) - { - Assert.AreEqual(a.Numerator * b, c.Numerator); - Assert.AreEqual(a.Denominator, c.Denominator); - c.Value = (b + 1) * a; - Assert.AreEqual((b + 1) * a.Numerator, c.Numerator); - Assert.AreEqual(a.Denominator, c.Denominator); - } - } - - [TestMethod] - public void RationalMultiplyByLimb() - { - using (var a = new HugeRational("90234098723098475098479385345098345/115756986668303657898962467957")) + using (var a = new HugeFloat("9023409872309847509847.9385345098345")) + using (var c = new HugeFloat()) { ulong b = 17390538260286101342; - a.Value = a * b; - Assert.AreEqual("1569219546226477273686601978789044606491747469626478990/115756986668303657898962467957", a.ToString()); + c.Value = a * b; + FloatAssert.AreEqual("156921954622647727368660197878904460649174.746962647899", c); + c.Value = b * -a; + FloatAssert.AreEqual("-156921954622647727368660197878904460649174.746962647899", c); } } [TestMethod] - public void RationalMultiplyLimbBy() + public void FloatMultiplyBySignedLimb() { - using (var a = new HugeRational("90234098723098475098479385345098345/115756986668303657898962467957")) - { - ulong b = 17390538260286101342; - a.Value = b * a; - Assert.AreEqual("1569219546226477273686601978789044606491747469626478990/115756986668303657898962467957", a.ToString()); - } - } - - [TestMethod] - public void RationalMultiplyBySignedLimb() - { - using (var a = new HugeRational("90234098723098475098479385345098345/115756986668303657898962467957")) + using (var a = new HugeFloat("9023409872309847509847.9385345098345")) + using (var c = new HugeFloat()) { long b = -7390538260286101342; - a.Value = a * b; - Assert.AreEqual("-666878558995492522701808125338061156491747469626478990/115756986668303657898962467957", a.ToString()); - } - } - - [TestMethod] - public void RationalMultiplySignedLimbBy() - { - using (var a = new HugeRational("90234098723098475098479385345098345/115756986668303657898962467957")) - { - long b = -7390538260286101342; - a.Value = b * a; - Assert.AreEqual("-666878558995492522701808125338061156491747469626478990/115756986668303657898962467957", a.ToString()); + c.Value = a * b; + FloatAssert.AreEqual("-66687855899549252270180812533806115649174.746962647899", c); + c.Value = b * -a; + FloatAssert.AreEqual("66687855899549252270180812533806115649174.746962647899", c); } } @@ -315,13 +202,14 @@ namespace MPIR.Tests.HugeRationalTests #region Shift Left [TestMethod] - public void RationalShiftLeft() + public void FloatShiftLeft() { - using (var a = new HugeRational("-12345700987ABCDEF2345CBDEFA245230948/17607EF654EB9A13FFA163C75", 16)) + using (var a = new HugeFloat("-12345700987ABA245230948.17607EF", 16)) + using (var e = new HugeFloat("-12345700987ABA24523094817607.EF", 16)) { - ulong b = 40; + ulong b = 20; a.Value = a << b; - Assert.AreEqual("-12345700987ABCDEF2345CBDEFA2452309480000000000/17607EF654EB9A13FFA163C75", a.ToString(16)); + Assert.AreEqual(e, a); } } @@ -330,13 +218,14 @@ namespace MPIR.Tests.HugeRationalTests #region Shift Right [TestMethod] - public void RationalShiftRight() + public void FloatShiftRight() { - using (var a = new HugeRational("ABCDEF052834524092854092874502983745029345723098457209305983434345/17607EF654EB9A13FFA163C75", 16)) + using (var a = new HugeFloat("-12345700987ABA24523094817607.EF", 16)) + using (var e = new HugeFloat("-12345700987ABA245230948.17607EF", 16)) { - ulong b = 96; + ulong b = 20; a.Value = a >> b; - Assert.AreEqual("ABCDEF052834524092854092874502983745029345723098457209305983434345/17607EF654EB9A13FFA163C75000000000000000000000000", a.ToString(16)); + Assert.AreEqual(e, a); } } @@ -345,14 +234,14 @@ namespace MPIR.Tests.HugeRationalTests #region Negate [TestMethod] - public void RationalNegate() + public void FloatNegate() { - using (var a = new HugeRational("24092854092874502983745029345723098457209/115756986668303657898962467957")) + using(var a = new HugeFloat("9023409872309847509847.9385345098345")) { a.Value = -a; - Assert.AreEqual("-24092854092874502983745029345723098457209/115756986668303657898962467957", a.ToString()); + FloatAssert.AreEqual("-9023409872309847509847.9385345098345", a); a.Value = -a; - Assert.AreEqual("24092854092874502983745029345723098457209/115756986668303657898962467957", a.ToString()); + FloatAssert.AreEqual("9023409872309847509847.9385345098345", a); } } @@ -361,30 +250,14 @@ namespace MPIR.Tests.HugeRationalTests #region Abs [TestMethod] - public void RationalMakeAbsolute() + public void FloatMakeAbsolute() { - using (var a = new HugeRational("-24092854092874502983745029345723098457209/115756986668303657898962467957")) + using(var a = new HugeFloat("-9023409872309847509847.9385345098345")) { a.Value = a.Abs(); - Assert.AreEqual("24092854092874502983745029345723098457209/115756986668303657898962467957", a.ToString()); + FloatAssert.AreEqual("9023409872309847509847.9385345098345", a); a.Value = a.Abs(); - Assert.AreEqual("24092854092874502983745029345723098457209/115756986668303657898962467957", a.ToString()); - } - } - - #endregion - - #region Invert - - [TestMethod] - public void RationalInvert() - { - using(var a = new HugeRational("-24092854092874502983745029345723098457209/115756986668303657898962467957")) - { - a.Value = a.Invert(); - Assert.AreEqual("-115756986668303657898962467957/24092854092874502983745029345723098457209", a.ToString()); - a.Value = a.Invert(); - Assert.AreEqual("-24092854092874502983745029345723098457209/115756986668303657898962467957", a.ToString()); + FloatAssert.AreEqual("9023409872309847509847.9385345098345", a); } } @@ -393,15 +266,36 @@ namespace MPIR.Tests.HugeRationalTests #region Power [TestMethod] - public void RationalPower() + public void FloatPower() { - using(var n = new HugeInt("-24092854092874502983745029345723098457209")) - using(var d = new HugeInt("115756986668303657898962467957")) - using(var a = new HugeRational(n, d)) + using(var a = new HugeFloat("-902340.945098345")) { a.Value = a ^ 5; - Assert.AreEqual(n ^ 5, a.Numerator); - Assert.AreEqual(d ^ 5, a.Denominator); + FloatAssert.AreEqual("-598209523815275040074985233466.4619735146023546465747916785912044", a); + } + } + + #endregion + + #region Sqrt + + [TestMethod] + public void FloatSqrt() + { + using(var a = new HugeFloat("9023404035982394058230584.9434590783455098345")) + { + a.Value = a.SquareRoot(); + FloatAssert.AreEqual("3003898140081.0504277325488426221152130989784856425363142756", a); + } + } + + [TestMethod] + public void FloatSqrtLimb() + { + using(var a = new HugeFloat()) + { + a.Value = HugeFloat.SquareRoot(2540928740928357403UL); + FloatAssert.AreEqual("1594029090.3645257511790832426801336140951314995369452070983", a); } } @@ -412,43 +306,13 @@ namespace MPIR.Tests.HugeRationalTests #region Int [TestMethod] - public void RationalDivideHugeRational() + public void FloatDivideHugeFloat() { - using (var a = new HugeRational("115756986668303657898962467957/394580293847502987609283945873594873409587")) - using (var b = new HugeRational("593169091750307653294")) + using (var a = new HugeFloat("1157569866683036578989624354347957.394580293847")) + using (var b = new HugeFloat("593169091750307653294.549782395235784")) { a.Value = a / b; - Assert.AreEqual("115756986668303657898962467957/234052834524092854092760134269509268758750275703033222451729578", a.ToString()); - } - } - - [TestMethod] - public void RationalDivideHugeRational2() - { - using (var a = new HugeRational("90234098723098475098479385345098345/115756986668303657898962467957")) - using (var b = new HugeRational("6847944682037444681162770672798288913849/590872612825179551336102196593")) - using (var c = new HugeRational(a / b)) - { - Assert.AreEqual(a.Numerator * b.Denominator, c.Numerator); - Assert.AreEqual(a.Denominator * b.Numerator, c.Denominator); - } - } - - [TestMethod] - public void RationalDivideHugeInt() - { - using (var a = new HugeRational("222509832503450298345029835740293845720/115756986668303657898962467957")) - using (var b = new HugeInt("222987435987982730594288574029879874539")) - using (var c = new HugeRational(a / b)) - using (var d = new HugeRational()) - { - Assert.AreEqual(a.Numerator, c.Numerator); - Assert.AreEqual(a.Denominator * b, c.Denominator); - c.Value = (b + 1) / a; - d.Numerator.Value = (b + 1) * a.Denominator; - d.Denominator.Value = a.Numerator; - d.Canonicalize(); - Assert.AreEqual(d, c); + FloatAssert.AreEqual("1951500647593.2689953514865540344827449639493356367018584357", a); } } @@ -457,46 +321,46 @@ namespace MPIR.Tests.HugeRationalTests #region Limb [TestMethod] - public void RationalDivideLimb() + public void FloatDivideLimb() { - using (var a = new HugeRational("115756986668303657898962467957/39458029384750298767200622330399462537522498")) + using (var a = new HugeFloat("1157569866683036578989624354347957.394580293847")) { ulong b = 5931690917503076532; a.Value = a / b; - Assert.AreEqual("115756986668303657898962467957/234052834524092854092874502983745029345723092857791404165816936", a.ToString()); + FloatAssert.AreEqual("195150064759326.89956625512472902395197480398952074748799190", a); } } [TestMethod] - public void RationalDivideSignedLimb() + public void FloatDivideSignedLimb() { - using(var a = new HugeRational("115756986668303657898962467957/39458029384750298767200622330399462537522498")) + using(var a = new HugeFloat("1157569866683036578989624354347957.394580293847")) { long b = -5931690917503076532; a.Value = a / b; - Assert.AreEqual("-115756986668303657898962467957/234052834524092854092874502983745029345723092857791404165816936", a.ToString()); + FloatAssert.AreEqual("-195150064759326.89956625512472902395197480398952074748799190", a); } } [TestMethod] - public void RationalDivideLimbBy() + public void FloatDivideLimbBy() { - using(var a = new HugeRational("115756986668303657898962467957/39458029384750298767200622330399462537522498")) + using(var a = new HugeFloat("11575698666830.39458029384723405203984572")) { ulong b = 5931690917503076532; a.Value = b / a; - Assert.AreEqual("234052834524092854092874502983745029345723092857791404165816936/115756986668303657898962467957", a.ToString()); + FloatAssert.AreEqual("512426.16866833708737257760720580856722540469109813901673959", a); } } [TestMethod] - public void RationalDivideSignedLimbBy() + public void FloatDivideSignedLimbBy() { - using(var a = new HugeRational("115756986668303657898962467957/39458029384750298767200622330399462537522498")) + using(var a = new HugeFloat("11575698666830.39458029384723405203984572")) { long b = -5931690917503076532; a.Value = b / a; - Assert.AreEqual("-234052834524092854092874502983745029345723092857791404165816936/115756986668303657898962467957", a.ToString()); + FloatAssert.AreEqual("-512426.16866833708737257760720580856722540469109813901673959", a); } } diff --git a/mpir.net/mpir.net-tests/HugeFloatTests/Comparisons.cs b/mpir.net/mpir.net-tests/HugeFloatTests/Comparisons.cs index 5d64164e..e006f930 100644 --- a/mpir.net/mpir.net-tests/HugeFloatTests/Comparisons.cs +++ b/mpir.net/mpir.net-tests/HugeFloatTests/Comparisons.cs @@ -20,7 +20,7 @@ along with the MPIR Library. If not, see http://www.gnu.org/licenses/. using System; using Microsoft.VisualStudio.TestTools.UnitTesting; -namespace MPIR.Tests.HugeRationalTests +namespace MPIR.Tests.HugeFloatTests { [TestClass] public class Comparisons @@ -28,126 +28,93 @@ namespace MPIR.Tests.HugeRationalTests #region CompareTo [TestMethod] - public void RationalCompareToHugeRational() + public void FloatCompareToHugeFloat() { - using (var a = new HugeRational("-222509832503450298345029835740293845721/115756986668303657898962467957")) - using (var b = new HugeRational("222509832503450298345029835740293845720/115756986668303657898962467957")) - using (var c = new HugeRational("1/115756986668303657898962467957")) + using (var a = new HugeFloat("-22250983250345029834503.9835740293845721345345354")) + using (var b = new HugeFloat("22250983250345029834502.9835740293845721345345354")) { - Assert.AreEqual(1, System.Math.Sign(b.CompareTo(a))); - Assert.AreEqual(-1, System.Math.Sign(a.CompareTo(b + 1))); - Assert.AreEqual(0, System.Math.Sign((a + c).CompareTo(-b))); - Assert.AreEqual(1, System.Math.Sign(a.CompareTo(null))); + Assert.AreEqual(1, Math.Sign(b.CompareTo(a))); + Assert.AreEqual(-1,Math.Sign(a.CompareTo(b + 1))); + Assert.AreEqual(0, Math.Sign((a + 1).CompareTo(-b))); + Assert.AreEqual(1, Math.Sign(a.CompareTo(null))); } } [TestMethod] - public void RationalCompareToObject() + public void FloatCompareToObject() { - using (var a = new HugeRational("-222509832503450298345029835740293845721/115756986668303657898962467957")) - using (var b = new HugeRational("222509832503450298345029835740293845720/115756986668303657898962467957")) - using (var c = new HugeRational("1/115756986668303657898962467957")) + using (var a = new HugeFloat("-22250983250345029834503.9835740293845721345345354")) + using (var b = new HugeFloat("22250983250345029834502.9835740293845721345345354")) { - Assert.AreEqual(1, System.Math.Sign(((IComparable)b).CompareTo((object)a))); - Assert.AreEqual(-1, System.Math.Sign(((IComparable)a).CompareTo((object)b))); - Assert.AreEqual(1, System.Math.Sign(((IComparable)a).CompareTo(null))); - Assert.AreEqual(0, System.Math.Sign(((IComparable)(a + c)).CompareTo((object)-b))); + Assert.AreEqual(1, Math.Sign(((IComparable)b).CompareTo((object)a))); + Assert.AreEqual(-1,Math.Sign(((IComparable)a).CompareTo((object)b))); + Assert.AreEqual(1, Math.Sign(((IComparable)a).CompareTo(null))); + Assert.AreEqual(0, Math.Sign(((IComparable)(a + 1)).CompareTo((object)-b))); } } [TestMethod] - public void RationalCompareToExpression() + public void FloatCompareToExpression() { - using (var a = new HugeRational("-222509832503450298345029835740293845721/115756986668303657898962467957")) - using (var b = new HugeRational("222509832503450298345029835740293845720/115756986668303657898962467957")) - using (var c = new HugeRational("1/115756986668303657898962467957")) + using (var a = new HugeFloat("-22250983250345029834503.9835740293845721345345354")) + using (var b = new HugeFloat("22250983250345029834502.9835740293845721345345354")) { - Assert.AreEqual(1, System.Math.Sign(((IComparable)b).CompareTo(a))); - Assert.AreEqual(-1, System.Math.Sign(((IComparable)a).CompareTo(b))); - Assert.AreEqual(1, System.Math.Sign(((IComparable)a).CompareTo(null))); - Assert.AreEqual(0, System.Math.Sign(((IComparable)(a + c)).CompareTo(-b))); + Assert.AreEqual(1, Math.Sign(((IComparable)b).CompareTo(a))); + Assert.AreEqual(-1,Math.Sign(((IComparable)a).CompareTo(b))); + Assert.AreEqual(1, Math.Sign(((IComparable)a).CompareTo(null))); + Assert.AreEqual(0, Math.Sign(((IComparable)(a + 1)).CompareTo(-b))); } } [TestMethod] [ExpectedException(typeof(ArgumentException))] - public void RationalCompareToNonExpression() + public void FloatCompareToNonExpression() { - using (var a = new HugeRational("-222509832503450298345029835740293845721/115756986668303657898962467957")) + using (var a = new HugeFloat("-222509832503450298345029835740293845721.57898962467957")) { a.CompareTo("abc"); } } [TestMethod] - public void RationalCompareToLimb() + public void FloatCompareToLimb() { - using (var a = new HugeRational("-222509821")) + using (var a = new HugeFloat("-222509821")) { ulong b = 222509820; - Assert.AreEqual(-1, System.Math.Sign(a.CompareTo(b))); - Assert.AreEqual(-1, System.Math.Sign(a.CompareTo(b + 1))); - Assert.AreEqual(0, System.Math.Sign((-(a + 1)).CompareTo(b))); - Assert.AreEqual(1, System.Math.Sign((-a).CompareTo(b))); + Assert.AreEqual(-1,Math.Sign(a.CompareTo(b))); + Assert.AreEqual(-1,Math.Sign(a.CompareTo(b + 1))); + Assert.AreEqual(0, Math.Sign((-(a + 1)).CompareTo(b))); + Assert.AreEqual(0, Math.Sign((-a).CompareTo(b + 1))); + Assert.AreEqual(1, Math.Sign((-a).CompareTo(b))); } } [TestMethod] - public void RationalCompareToLimb2() + public void FloatCompareToSignedLimb() { - using(var a = new HugeRational("-222509947/127")) - { - ulong b = 222509820; - ulong d = 127; - Assert.AreEqual(-1, System.Math.Sign(a.CompareTo(b, d))); - Assert.AreEqual(-1, System.Math.Sign(a.CompareTo(b + 1, d))); - Assert.AreEqual(0, System.Math.Sign((-(a + 1)).CompareTo(b, d))); - Assert.AreEqual(0, System.Math.Sign((-a).CompareTo(b + d, d))); - Assert.AreEqual(1, System.Math.Sign((-a).CompareTo(b, d))); - } - } - - [TestMethod] - public void RationalCompareToSignedLimb() - { - using (var a = new HugeRational("-222509821")) + using (var a = new HugeFloat("-222509821")) { long b = -222509820; - Assert.AreEqual(-1, System.Math.Sign(a.CompareTo(b))); - Assert.AreEqual(-1, System.Math.Sign(a.CompareTo(b + 1))); - Assert.AreEqual(0, System.Math.Sign((a + 1).CompareTo(b))); - Assert.AreEqual(0, System.Math.Sign(a.CompareTo(b - 1))); - Assert.AreEqual(1, System.Math.Sign(a.CompareTo(b - 2))); + Assert.AreEqual(-1,Math.Sign(a.CompareTo(b))); + Assert.AreEqual(-1,Math.Sign(a.CompareTo(b + 1))); + Assert.AreEqual(0, Math.Sign((a + 1).CompareTo(b))); + Assert.AreEqual(0, Math.Sign(a.CompareTo(b - 1))); + Assert.AreEqual(1, Math.Sign(a.CompareTo(b - 2))); } } [TestMethod] - public void RationalCompareToSignedLimb2() + public void FloatCompareToSignedDouble() { - using(var a = new HugeRational("-222509947/127")) - { - long b = -222509820; - long d = 127; - Assert.AreEqual(-1, System.Math.Sign(a.CompareTo(b, (ulong)d))); - Assert.AreEqual(-1, System.Math.Sign(a.CompareTo(b + 1, (ulong)d))); - Assert.AreEqual(0, System.Math.Sign((a + 1).CompareTo(b, (ulong)d))); - Assert.AreEqual(-1, System.Math.Sign(a.CompareTo(b - d + 1, (ulong)d))); - Assert.AreEqual(0, System.Math.Sign(a.CompareTo(b - d, (ulong)d))); - Assert.AreEqual(1, System.Math.Sign(a.CompareTo(b - d - 1, (ulong)d))); - } - } - - [TestMethod] - public void RationalCompareToSignedDouble() - { - using (var a = new HugeRational("-222509821")) + using (var a = new HugeFloat("-222509821")) { double b = -222509820; - Assert.AreEqual(-1, System.Math.Sign(a.CompareTo(b))); - Assert.AreEqual(-1, System.Math.Sign(a.CompareTo(b + 1))); - Assert.AreEqual(0, System.Math.Sign((a + 1).CompareTo(b))); - Assert.AreEqual(0, System.Math.Sign(a.CompareTo(b - 1))); - Assert.AreEqual(1, System.Math.Sign(a.CompareTo(b - 1.1))); + Assert.AreEqual(-1,Math.Sign(a.CompareTo(b))); + Assert.AreEqual(-1,Math.Sign(a.CompareTo(b + 1))); + Assert.AreEqual(0, Math.Sign((a + 1).CompareTo(b))); + Assert.AreEqual(0, Math.Sign(a.CompareTo(b - 1))); + Assert.AreEqual(1, Math.Sign(a.CompareTo(b - 1.1))); } } @@ -156,27 +123,28 @@ namespace MPIR.Tests.HugeRationalTests #region comparison operators with expr [TestMethod] - public void RationalOperatorLessThan() + public void FloatOperatorLessThan() { - using (var a = new HugeRational("-222509832503450298345029835740293845721/115756986668303657898962467957")) - using (var b = new HugeRational("222509832503450298345029835740293845720/115756986668303657898962467957")) - using (var c = new HugeRational()) + using (var a = new HugeFloat("-2225098325034502983450.29835740293845721")) + using (var b = new HugeFloat("2225098325034502983450.29835740293845721")) + using (var c = new HugeFloat()) { c.Value = a; Assert.IsTrue(a < b); Assert.IsFalse(b < a); Assert.IsFalse(a < c); + Assert.IsFalse(a > c); Assert.IsFalse(a < null); Assert.IsTrue(null < a); } } [TestMethod] - public void RationalOperatorLessThanOrEqual() + public void FloatOperatorLessThanOrEqual() { - using (var a = new HugeRational("-222509832503450298345029835740293845721/115756986668303657898962467957")) - using (var b = new HugeRational("222509832503450298345029835740293845720/115756986668303657898962467957")) - using (var c = new HugeRational()) + using (var a = new HugeFloat("-2225098325034502983451.29835740293845721")) + using (var b = new HugeFloat("2225098325034502983450.29835740293845721")) + using (var c = new HugeFloat()) { c.Value = a; Assert.IsTrue(a <= b); @@ -188,11 +156,11 @@ namespace MPIR.Tests.HugeRationalTests } [TestMethod] - public void RationalOperatorGreaterThan() + public void FloatOperatorGreaterThan() { - using (var a = new HugeRational("-222509832503450298345029835740293845721/115756986668303657898962467957")) - using (var b = new HugeRational("222509832503450298345029835740293845720/115756986668303657898962467957")) - using (var c = new HugeRational()) + using (var a = new HugeFloat("-2225098325034502983451.29835740293845721")) + using (var b = new HugeFloat("2225098325034502983450.29835740293845721")) + using (var c = new HugeFloat()) { c.Value = a; Assert.IsFalse(a > b); @@ -204,11 +172,11 @@ namespace MPIR.Tests.HugeRationalTests } [TestMethod] - public void RationalOperatorGreaterThanOrEqual() + public void FloatOperatorGreaterThanOrEqual() { - using (var a = new HugeRational("-222509832503450298345029835740293845721/115756986668303657898962467957")) - using (var b = new HugeRational("222509832503450298345029835740293845720/115756986668303657898962467957")) - using (var c = new HugeRational()) + using (var a = new HugeFloat("-2225098325034502983451.29835740293845721")) + using (var b = new HugeFloat("2225098325034502983450.29835740293845721")) + using (var c = new HugeFloat()) { c.Value = a; Assert.IsFalse(a >= b); @@ -224,9 +192,9 @@ namespace MPIR.Tests.HugeRationalTests #region comparison operators with limb [TestMethod] - public void RationalOperatorLessThanLimb() + public void FloatOperatorLessThanLimb() { - using (var a = new HugeRational("3845721")) + using (var a = new HugeFloat("3845721")) { ulong c = 5432; ulong b = 5432349587; @@ -238,9 +206,9 @@ namespace MPIR.Tests.HugeRationalTests } [TestMethod] - public void RationalOperatorLessThanOrEqualLimb() + public void FloatOperatorLessThanOrEqualLimb() { - using (var a = new HugeRational("3845721")) + using (var a = new HugeFloat("3845721")) { ulong c = 5432; ulong b = 5432349587; @@ -255,9 +223,9 @@ namespace MPIR.Tests.HugeRationalTests } [TestMethod] - public void RationalOperatorGreaterThanLimb() + public void FloatOperatorGreaterThanLimb() { - using (var a = new HugeRational("3845721")) + using (var a = new HugeFloat("3845721")) { ulong c = 5432; ulong b = 5432349587; @@ -269,9 +237,9 @@ namespace MPIR.Tests.HugeRationalTests } [TestMethod] - public void RationalOperatorGreaterThanOrEqualLimb() + public void FloatOperatorGreaterThanOrEqualLimb() { - using (var a = new HugeRational("3845721")) + using (var a = new HugeFloat("3845721")) { ulong c = 5432; ulong b = 5432349587; @@ -290,9 +258,9 @@ namespace MPIR.Tests.HugeRationalTests #region comparison operators with signed limb [TestMethod] - public void RationalOperatorLessThanSignedLimb() + public void FloatOperatorLessThanSignedLimb() { - using (var a = new HugeRational("-3845721")) + using (var a = new HugeFloat("-3845721")) { long c = -543254325432; long b = -9587; @@ -304,9 +272,9 @@ namespace MPIR.Tests.HugeRationalTests } [TestMethod] - public void RationalOperatorLessThanOrEqualSignedLimb() + public void FloatOperatorLessThanOrEqualSignedLimb() { - using (var a = new HugeRational("-3845721")) + using (var a = new HugeFloat("-3845721")) { long c = -543254325432; long b = -9587; @@ -321,9 +289,9 @@ namespace MPIR.Tests.HugeRationalTests } [TestMethod] - public void RationalOperatorGreaterThanSignedLimb() + public void FloatOperatorGreaterThanSignedLimb() { - using (var a = new HugeRational("-3845721")) + using (var a = new HugeFloat("-3845721")) { long c = -543254325432; long b = -9587; @@ -335,9 +303,9 @@ namespace MPIR.Tests.HugeRationalTests } [TestMethod] - public void RationalOperatorGreaterThanOrEqualSignedLimb() + public void FloatOperatorGreaterThanOrEqualSignedLimb() { - using (var a = new HugeRational("-3845721")) + using (var a = new HugeFloat("-3845721")) { long c = -543254325432; long b = -9587; @@ -356,9 +324,9 @@ namespace MPIR.Tests.HugeRationalTests #region comparison operators with double [TestMethod] - public void RationalOperatorLessThanDouble() + public void FloatOperatorLessThanDouble() { - using (var a = new HugeRational("-3845721")) + using (var a = new HugeFloat("-3845721")) { double c = -543254325432; double b = -9587; @@ -370,9 +338,9 @@ namespace MPIR.Tests.HugeRationalTests } [TestMethod] - public void RationalOperatorLessThanOrEqualDouble() + public void FloatOperatorLessThanOrEqualDouble() { - using (var a = new HugeRational("-3845721")) + using (var a = new HugeFloat("-3845721")) { double c = -543254325432; double b = -9587; @@ -387,9 +355,9 @@ namespace MPIR.Tests.HugeRationalTests } [TestMethod] - public void RationalOperatorGreaterThanDouble() + public void FloatOperatorGreaterThanDouble() { - using (var a = new HugeRational("-3845721")) + using (var a = new HugeFloat("-3845721")) { double c = -543254325432; double b = -9587; @@ -401,9 +369,9 @@ namespace MPIR.Tests.HugeRationalTests } [TestMethod] - public void RationalOperatorGreaterThanOrEqualDouble() + public void FloatOperatorGreaterThanOrEqualDouble() { - using (var a = new HugeRational("-3845721")) + using (var a = new HugeFloat("-3845721")) { double c = -543254325432; double b = -9587; @@ -423,47 +391,45 @@ namespace MPIR.Tests.HugeRationalTests #region Equals [TestMethod] - public void RationalEqualsHugeRational() + public void FloatEqualsHugeFloat() { - using (var a = new HugeRational("-222509832503450298345029835740293845721/115756986668303657898962467957")) - using (var b = new HugeRational("222509832503450298345029835740293845720/115756986668303657898962467957")) - using (var c = new HugeRational("1/115756986668303657898962467957")) + using (var a = new HugeFloat("-2225098325034502983451.29835740293845721")) + using (var b = new HugeFloat("2225098325034502983450.29835740293845721")) { Assert.IsFalse(b.Equals(a)); - Assert.IsFalse(a.Equals(b + c)); - Assert.IsTrue((a + c).Equals(-b)); + Assert.IsFalse(a.Equals(b + 1)); + Assert.IsTrue((a + 1).Equals(-b)); Assert.IsFalse(a.Equals(null)); - Assert.IsTrue(Equals(a + c, -b)); + Assert.IsTrue(Equals(a + 1, -b)); } } [TestMethod] - public void RationalEqualsExpression() + public void FloatEqualsExpression() { - using (var a = new HugeRational("-222509832503450298345029835740293845721/115756986668303657898962467957")) - using (var b = new HugeRational("222509832503450298345029835740293845720/115756986668303657898962467957")) - using (var c = new HugeRational("1/115756986668303657898962467957")) + using (var a = new HugeFloat("-2225098325034502983451.29835740293845721")) + using (var b = new HugeFloat("2225098325034502983450.29835740293845721")) { - Assert.IsFalse(((IEquatable)b).Equals(a)); - Assert.IsFalse(((IEquatable)a).Equals(b)); - Assert.IsFalse(((IEquatable)a).Equals(null)); - Assert.IsTrue(((IEquatable)(a + c)).Equals(-b)); + Assert.IsFalse(((IEquatable)b).Equals(a)); + Assert.IsFalse(((IEquatable)a).Equals(b)); + Assert.IsFalse(((IEquatable)a).Equals(null)); + Assert.IsTrue(((IEquatable)(a + 1)).Equals(-b)); } } [TestMethod] - public void RationalEqualsNonExpression() + public void FloatEqualsNonExpression() { - using (var a = new HugeRational("-222509832503450298345029835740293845721/115756986668303657898962467957")) + using (var a = new HugeFloat("-2225098325034502983450.29835740293845721")) { Assert.IsFalse(a.Equals("abc")); } } [TestMethod] - public void RationalEqualsLimb() + public void FloatEqualsLimb() { - using (var a = new HugeRational("222509832503")) + using (var a = new HugeFloat("222509832503")) { ulong b = 222509832504; Assert.IsFalse(a.Equals(b + 1)); @@ -473,9 +439,9 @@ namespace MPIR.Tests.HugeRationalTests } [TestMethod] - public void RationalEqualsSignedLimb() + public void FloatEqualsSignedLimb() { - using (var a = new HugeRational("-222509832505")) + using (var a = new HugeFloat("-222509832505")) { long b = -222509832504; Assert.IsFalse(a.Equals(b + 1)); @@ -485,35 +451,9 @@ namespace MPIR.Tests.HugeRationalTests } [TestMethod] - public void RationalEqualsLimb2() + public void FloatEqualsDouble() { - using(var a = new HugeRational("222509832377/127")) - { - ulong b = 222509832504; - ulong d = 127; - Assert.IsFalse(a.Equals(b + 1, d)); - Assert.IsTrue(a.Equals(b - d, d)); - Assert.IsTrue((a + 1).Equals(b, d)); - } - } - - [TestMethod] - public void RationalEqualsSignedLimb2() - { - using(var a = new HugeRational("-222509832377/127")) - { - long b = -222509832504; - ulong d = 127; - Assert.IsFalse(a.Equals(b + 1, d)); - Assert.IsTrue(a.Equals(b + (long)d, d)); - Assert.IsTrue((a - 1).Equals(b, d)); - } - } - - [TestMethod] - public void RationalEqualsDouble() - { - using (var a = new HugeRational("-222509832505")) + using (var a = new HugeFloat("-222509832505")) { double b = -222509832504; Assert.IsFalse(a.Equals(b + 1)); @@ -523,34 +463,66 @@ namespace MPIR.Tests.HugeRationalTests } } + [TestMethod] + public void FloatEqualsHugeFloatApproximately() + { + using (var a = new HugeFloat("ABCDEF12948576AB49587.ACD34EFB345", 16)) + using (var b = new HugeFloat("ABCDEF12948576AB49587.ACD34EFB245", 16)) + { + Assert.IsTrue(a.Equals(b, 119)); + Assert.IsFalse(a.Equals(b, 120)); + Assert.IsTrue(a.Equals(b - 1, 83)); + Assert.IsFalse(a.Equals(b - 1, 84)); + Assert.IsTrue((a + 512).Equals(b, 74)); + Assert.IsFalse((a + 512).Equals(b, 75)); + + //same mantissa, different exponent should always return false + Assert.IsFalse(a.Equals(a >> 1, 119)); + Assert.IsFalse(a.Equals(a << 1, 119)); + Assert.IsFalse(a.Equals(a * 2, 119)); + Assert.IsFalse(a.Equals(a / 2, 119)); + } + } + + [TestMethod] + public void FloatRelDiff() + { + using (var a = new HugeFloat("1234523549876.24935230589472305894245")) + using (var b = new HugeFloat("1234523549541.45207354209357842979873")) + { + Assert.AreEqual(a.RelativeDifferenceFrom(b), (a - b).Abs() / a); + Assert.AreNotEqual(a.RelativeDifferenceFrom(b), (a - b).Abs() / b); + Assert.AreEqual(b.RelativeDifferenceFrom(a), (a - b).Abs() / b); + Assert.AreNotEqual(b.RelativeDifferenceFrom(a), (a - b).Abs() / a); + } + } + #endregion #region Equality operators with expr [TestMethod] - public void RationalEqualsOperatorHugeRational() + public void FloatEqualsOperatorHugeFloat() { - using (var a = new HugeRational("-222509832503450298345029835740293845721/115756986668303657898962467957")) - using (var b = new HugeRational("222509832503450298345029835740293845720/115756986668303657898962467957")) - using (var c = new HugeRational("1/115756986668303657898962467957")) + using (var a = new HugeFloat("-2225098325034502983451.29835740293845721")) + using (var b = new HugeFloat("2225098325034502983450.29835740293845721")) { Assert.IsFalse(b == a); - Assert.IsFalse(a == b + c); - Assert.IsTrue(a + c == -b); + Assert.IsFalse(a == b + 1); + Assert.IsTrue(a + 1 == -b); Assert.IsFalse(a == null); } } [TestMethod] - public void RationalNotEqualOperatorHugeRational() + public void FloatNotEqualOperatorHugeFloat() { - using (var a = new HugeRational("-222509832503450298345029835740293845721/115756986668303657898962467957")) - using (var b = new HugeRational("222509832503450298345029835740293845720/115756986668303657898962467957")) - using (var c = new HugeRational("1/115756986668303657898962467957")) + using (var a = new HugeFloat("-2225098325034502983451.29835740293845721")) + using (var b = new HugeFloat("2225098325034502983450.29835740293845721")) { Assert.IsTrue(b != a); - Assert.IsTrue(a != b + c); - Assert.IsFalse(a + c != -b); + Assert.IsTrue(a != b + 1); + Assert.IsFalse(a + 1 != -b); Assert.IsTrue(a != null); } } @@ -560,9 +532,9 @@ namespace MPIR.Tests.HugeRationalTests #region Equality operators with Limb [TestMethod] - public void RationalEqualsOperatorLimb() + public void FloatEqualsOperatorLimb() { - using (var a = new HugeRational("-835740293845721")) + using (var a = new HugeFloat("-835740293845721")) { ulong b = 835740293845720; Assert.IsFalse(b == a); @@ -572,9 +544,9 @@ namespace MPIR.Tests.HugeRationalTests } [TestMethod] - public void RationalNotEqualOperatorLimb() + public void FloatNotEqualOperatorLimb() { - using (var a = new HugeRational("-835740293845721")) + using (var a = new HugeFloat("-835740293845721")) { ulong b = 835740293845720; Assert.IsTrue(b != a); @@ -588,9 +560,9 @@ namespace MPIR.Tests.HugeRationalTests #region Equality operators with Signed Limb [TestMethod] - public void RationalEqualsOperatorSignedLimb() + public void FloatEqualsOperatorSignedLimb() { - using (var a = new HugeRational("-835740293845721")) + using (var a = new HugeFloat("-835740293845721")) { long b = -835740293845720; Assert.IsFalse(b == a); @@ -600,9 +572,9 @@ namespace MPIR.Tests.HugeRationalTests } [TestMethod] - public void RationalNotEqualOperatorSignedLimb() + public void FloatNotEqualOperatorSignedLimb() { - using (var a = new HugeRational("-835740293845721")) + using (var a = new HugeFloat("-835740293845721")) { long b = -835740293845720; Assert.IsTrue(b != a); @@ -616,9 +588,9 @@ namespace MPIR.Tests.HugeRationalTests #region Equality operators with Double [TestMethod] - public void RationalEqualsOperatorDouble() + public void FloatEqualsOperatorDouble() { - using (var a = new HugeRational("-835740293845721")) + using (var a = new HugeFloat("-835740293845721")) { double b = -835740293845720; Assert.IsFalse(b == a); @@ -629,9 +601,9 @@ namespace MPIR.Tests.HugeRationalTests } [TestMethod] - public void RationalNotEqualOperatorDouble() + public void FloatNotEqualOperatorDouble() { - using (var a = new HugeRational("-835740293845721")) + using (var a = new HugeFloat("-835740293845721")) { double b = -835740293845720; Assert.IsTrue(b != a); @@ -646,9 +618,9 @@ namespace MPIR.Tests.HugeRationalTests #region GetHashCode [TestMethod] - public void RationalGetHashCodeTest() + public void FloatGetHashCodeTest() { - using (var a = new HugeRational("-222509832503450298345029835740293845721/115756986668303657898962467957")) + using (var a = new HugeFloat("-2225098325034502983450298357.40293845721")) { Assert.AreNotEqual(0, a.GetHashCode()); Assert.AreEqual(a.GetHashCode(), (a + 0).GetHashCode()); @@ -661,9 +633,9 @@ namespace MPIR.Tests.HugeRationalTests #region Sign [TestMethod] - public void RationalSign() + public void FloatSign() { - using (var a = new HugeRational("-222509832503450298345029835740293845721/115756986668303657898962467957")) + using (var a = new HugeFloat("-22250983250345029834.502983574029384572134354")) { Assert.AreEqual(-1, a.Sign()); Assert.AreEqual(1, (-a).Sign()); @@ -672,7 +644,6 @@ namespace MPIR.Tests.HugeRationalTests } #endregion - //more tests coming here } }