From aaa7b56e7028b815a4fc92d5f5ca209a2dbf85c5 Mon Sep 17 00:00:00 2001 From: Alex Dyachenko Date: Sun, 23 Nov 2014 22:34:20 -0500 Subject: [PATCH] Float comparison tests. Added construction and assignment of floats from rationals and vice versa. --- .../HugeFloatTests/Comparisons.cs | 234 +++++++++--------- .../HugeFloatTests/ConstructionAndDisposal.cs | 14 +- .../HugeRationalTests/Assignment.cs | 11 + .../ConstructionAndDisposal.cs | 10 + mpir.net/mpir.net/HugeFloat.cpp | 16 +- mpir.net/mpir.net/HugeFloat.h | 16 +- mpir.net/mpir.net/HugeRational.cpp | 14 ++ mpir.net/mpir.net/HugeRational.h | 13 + 8 files changed, 207 insertions(+), 121 deletions(-) diff --git a/mpir.net/mpir.net-tests/HugeFloatTests/Comparisons.cs b/mpir.net/mpir.net-tests/HugeFloatTests/Comparisons.cs index 5d64164e..72e65a7b 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,11 +28,11 @@ 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("-222509832503450298345029835740293845721/115756986668303657898962467957")) + using (var b = new HugeFloat("222509832503450298345029835740293845720/115756986668303657898962467957")) + using (var c = new HugeFloat("1/115756986668303657898962467957")) { Assert.AreEqual(1, System.Math.Sign(b.CompareTo(a))); Assert.AreEqual(-1, System.Math.Sign(a.CompareTo(b + 1))); @@ -42,11 +42,11 @@ namespace MPIR.Tests.HugeRationalTests } [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("-222509832503450298345029835740293845721/115756986668303657898962467957")) + using (var b = new HugeFloat("222509832503450298345029835740293845720/115756986668303657898962467957")) + using (var c = new HugeFloat("1/115756986668303657898962467957")) { Assert.AreEqual(1, System.Math.Sign(((IComparable)b).CompareTo((object)a))); Assert.AreEqual(-1, System.Math.Sign(((IComparable)a).CompareTo((object)b))); @@ -56,33 +56,33 @@ namespace MPIR.Tests.HugeRationalTests } [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("-222509832503450298345029835740293845721/115756986668303657898962467957")) + using (var b = new HugeFloat("222509832503450298345029835740293845720/115756986668303657898962467957")) + using (var c = new HugeFloat("1/115756986668303657898962467957")) { - 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, 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))); } } [TestMethod] [ExpectedException(typeof(ArgumentException))] - public void RationalCompareToNonExpression() + public void FloatCompareToNonExpression() { - using (var a = new HugeRational("-222509832503450298345029835740293845721/115756986668303657898962467957")) + using (var a = new HugeFloat("-222509832503450298345029835740293845721/115756986668303657898962467957")) { 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))); @@ -93,9 +93,9 @@ namespace MPIR.Tests.HugeRationalTests } [TestMethod] - public void RationalCompareToLimb2() + public void FloatCompareToLimb2() { - using(var a = new HugeRational("-222509947/127")) + using(var a = new HugeFloat("-222509947/127")) { ulong b = 222509820; ulong d = 127; @@ -108,9 +108,9 @@ namespace MPIR.Tests.HugeRationalTests } [TestMethod] - public void RationalCompareToSignedLimb() + public void FloatCompareToSignedLimb() { - using (var a = new HugeRational("-222509821")) + using (var a = new HugeFloat("-222509821")) { long b = -222509820; Assert.AreEqual(-1, System.Math.Sign(a.CompareTo(b))); @@ -122,9 +122,9 @@ namespace MPIR.Tests.HugeRationalTests } [TestMethod] - public void RationalCompareToSignedLimb2() + public void FloatCompareToSignedLimb2() { - using(var a = new HugeRational("-222509947/127")) + using(var a = new HugeFloat("-222509947/127")) { long b = -222509820; long d = 127; @@ -138,9 +138,9 @@ namespace MPIR.Tests.HugeRationalTests } [TestMethod] - public void RationalCompareToSignedDouble() + public void FloatCompareToSignedDouble() { - using (var a = new HugeRational("-222509821")) + using (var a = new HugeFloat("-222509821")) { double b = -222509820; Assert.AreEqual(-1, System.Math.Sign(a.CompareTo(b))); @@ -156,11 +156,11 @@ 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("-222509832503450298345029835740293845721/115756986668303657898962467957")) + using (var b = new HugeFloat("222509832503450298345029835740293845720/115756986668303657898962467957")) + using (var c = new HugeFloat()) { c.Value = a; Assert.IsTrue(a < b); @@ -172,11 +172,11 @@ namespace MPIR.Tests.HugeRationalTests } [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("-222509832503450298345029835740293845721/115756986668303657898962467957")) + using (var b = new HugeFloat("222509832503450298345029835740293845720/115756986668303657898962467957")) + using (var c = new HugeFloat()) { c.Value = a; Assert.IsTrue(a <= b); @@ -188,11 +188,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("-222509832503450298345029835740293845721/115756986668303657898962467957")) + using (var b = new HugeFloat("222509832503450298345029835740293845720/115756986668303657898962467957")) + using (var c = new HugeFloat()) { c.Value = a; Assert.IsFalse(a > b); @@ -204,11 +204,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("-222509832503450298345029835740293845721/115756986668303657898962467957")) + using (var b = new HugeFloat("222509832503450298345029835740293845720/115756986668303657898962467957")) + using (var c = new HugeFloat()) { c.Value = a; Assert.IsFalse(a >= b); @@ -224,9 +224,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 +238,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 +255,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 +269,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 +290,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 +304,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 +321,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 +335,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 +356,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 +370,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 +387,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 +401,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,11 +423,11 @@ 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("-222509832503450298345029835740293845721/115756986668303657898962467957")) + using (var b = new HugeFloat("222509832503450298345029835740293845720/115756986668303657898962467957")) + using (var c = new HugeFloat("1/115756986668303657898962467957")) { Assert.IsFalse(b.Equals(a)); Assert.IsFalse(a.Equals(b + c)); @@ -438,32 +438,32 @@ namespace MPIR.Tests.HugeRationalTests } [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("-222509832503450298345029835740293845721/115756986668303657898962467957")) + using (var b = new HugeFloat("222509832503450298345029835740293845720/115756986668303657898962467957")) + using (var c = new HugeFloat("1/115756986668303657898962467957")) { - 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 + c)).Equals(-b)); } } [TestMethod] - public void RationalEqualsNonExpression() + public void FloatEqualsNonExpression() { - using (var a = new HugeRational("-222509832503450298345029835740293845721/115756986668303657898962467957")) + using (var a = new HugeFloat("-222509832503450298345029835740293845721/115756986668303657898962467957")) { 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 +473,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,9 +485,9 @@ namespace MPIR.Tests.HugeRationalTests } [TestMethod] - public void RationalEqualsLimb2() + public void FloatEqualsLimb2() { - using(var a = new HugeRational("222509832377/127")) + using(var a = new HugeFloat("222509832377/127")) { ulong b = 222509832504; ulong d = 127; @@ -498,9 +498,9 @@ namespace MPIR.Tests.HugeRationalTests } [TestMethod] - public void RationalEqualsSignedLimb2() + public void FloatEqualsSignedLimb2() { - using(var a = new HugeRational("-222509832377/127")) + using(var a = new HugeFloat("-222509832377/127")) { long b = -222509832504; ulong d = 127; @@ -511,9 +511,9 @@ namespace MPIR.Tests.HugeRationalTests } [TestMethod] - public void RationalEqualsDouble() + public void FloatEqualsDouble() { - using (var a = new HugeRational("-222509832505")) + using (var a = new HugeFloat("-222509832505")) { double b = -222509832504; Assert.IsFalse(a.Equals(b + 1)); @@ -528,11 +528,11 @@ namespace MPIR.Tests.HugeRationalTests #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("-222509832503450298345029835740293845721/115756986668303657898962467957")) + using (var b = new HugeFloat("222509832503450298345029835740293845720/115756986668303657898962467957")) + using (var c = new HugeFloat("1/115756986668303657898962467957")) { Assert.IsFalse(b == a); Assert.IsFalse(a == b + c); @@ -542,11 +542,11 @@ namespace MPIR.Tests.HugeRationalTests } [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("-222509832503450298345029835740293845721/115756986668303657898962467957")) + using (var b = new HugeFloat("222509832503450298345029835740293845720/115756986668303657898962467957")) + using (var c = new HugeFloat("1/115756986668303657898962467957")) { Assert.IsTrue(b != a); Assert.IsTrue(a != b + c); @@ -560,9 +560,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 +572,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 +588,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 +600,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 +616,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 +629,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 +646,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("-222509832503450298345029835740293845721/115756986668303657898962467957")) { Assert.AreNotEqual(0, a.GetHashCode()); Assert.AreEqual(a.GetHashCode(), (a + 0).GetHashCode()); @@ -661,9 +661,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("-222509832503450298345029835740293845721/115756986668303657898962467957")) { Assert.AreEqual(-1, a.Sign()); Assert.AreEqual(1, (-a).Sign()); diff --git a/mpir.net/mpir.net-tests/HugeFloatTests/ConstructionAndDisposal.cs b/mpir.net/mpir.net-tests/HugeFloatTests/ConstructionAndDisposal.cs index 96a62852..a49e7a53 100644 --- a/mpir.net/mpir.net-tests/HugeFloatTests/ConstructionAndDisposal.cs +++ b/mpir.net/mpir.net-tests/HugeFloatTests/ConstructionAndDisposal.cs @@ -188,10 +188,20 @@ namespace MPIR.Tests.HugeFloatTests [TestMethod] public void FloatConstructorFromIntExpression() { - using (var a = new HugeInt("234095827340957823409582587")) + using (var a = new HugeInt("23409582734095782340")) using (var b = new HugeFloat(a + 1)) { - Assert.AreEqual("0.23409582734095782341@27", b.ToString()); + Assert.AreEqual("0.23409582734095782341@20", b.ToString()); + } + } + + [TestMethod] + public void FloatConstructorFromRationalExpression() + { + using (var a = new HugeRational("23409582734095782340/16")) + using (var b = new HugeFloat(a + 1)) + { + Assert.AreEqual("0.146309892088098639725@19", b.ToString()); } } } diff --git a/mpir.net/mpir.net-tests/HugeRationalTests/Assignment.cs b/mpir.net/mpir.net-tests/HugeRationalTests/Assignment.cs index 00665cbd..ffbc6de6 100644 --- a/mpir.net/mpir.net-tests/HugeRationalTests/Assignment.cs +++ b/mpir.net/mpir.net-tests/HugeRationalTests/Assignment.cs @@ -61,6 +61,17 @@ namespace MPIR.Tests.HugeRationalTests } } + [TestMethod] + public void RationalAssignFloat() + { + using (var a = new HugeFloat("9363833093638312937.25")) + using (var b = new HugeRational()) + { + b.SetTo(a); + Assert.AreEqual("37455332374553251749/4", b.ToString()); + } + } + [TestMethod] public void RationalAssignInt() { diff --git a/mpir.net/mpir.net-tests/HugeRationalTests/ConstructionAndDisposal.cs b/mpir.net/mpir.net-tests/HugeRationalTests/ConstructionAndDisposal.cs index da55ceb7..c1636601 100644 --- a/mpir.net/mpir.net-tests/HugeRationalTests/ConstructionAndDisposal.cs +++ b/mpir.net/mpir.net-tests/HugeRationalTests/ConstructionAndDisposal.cs @@ -262,6 +262,16 @@ namespace MPIR.Tests.HugeRationalTests } } + [TestMethod] + public void RationalConstructorFromFloatExpression() + { + using (var a = new HugeFloat("2340958273409578234.25")) + using (var b = new HugeRational(a + 1)) + { + Assert.AreEqual("9363833093638312941/4", b.ToString()); + } + } + [TestMethod] public void RationalConstructorFromIntExpression() { diff --git a/mpir.net/mpir.net/HugeFloat.cpp b/mpir.net/mpir.net/HugeFloat.cpp index 2ec593fe..bd6f71a0 100644 --- a/mpir.net/mpir.net/HugeFloat.cpp +++ b/mpir.net/mpir.net/HugeFloat.cpp @@ -19,6 +19,7 @@ along with the MPIR Library. If not, see http://www.gnu.org/licenses/. #include "Stdafx.h" #include "HugeInt.h" +#include "HugeRational.h" #include "HugeFloat.h" //#include "Random.h" @@ -55,6 +56,13 @@ namespace MPIR MP(init)(_value); SetTo(value); } + + MPTYPE::MPTYPE(RationalExpression^ value) + { + AllocateStruct(); + MP(init)(_value); + SetTo(value); + } MPTYPE^ MPTYPE::Allocate(mp_bitcnt_t precision) { @@ -341,7 +349,7 @@ namespace MPIR #pragma endregion - #pragma region methods in other classes with rational parameters + #pragma region methods in other classes with float parameters void HugeInt::SetTo(MPEXPR_NAME^ value) { @@ -349,5 +357,11 @@ namespace MPIR mpz_set_f(_value, CTXT(0)); } + void HugeRational::SetTo(MPEXPR_NAME^ value) + { + IN_CONTEXT(value); + mpq_set_f(_value, CTXT(0)); + } + #pragma endregion }; \ No newline at end of file diff --git a/mpir.net/mpir.net/HugeFloat.h b/mpir.net/mpir.net/HugeFloat.h index 0015999f..2c40e2c4 100644 --- a/mpir.net/mpir.net/HugeFloat.h +++ b/mpir.net/mpir.net/HugeFloat.h @@ -48,6 +48,7 @@ using namespace System::Runtime::InteropServices; #define MPEXPR(x) LIT(MPTYPE_NAME)##x##Expression #define CTXT(x) context.FloatArgs[x] #define CTXTI(x) context.IntArgs[x] +#define CTXTR(x) context.RationalArgs[x] #define ASSIGN_TO CONCAT(AssignTo, LIT(MPTYPE_NAME)) #include "ExpressionMacros.h" @@ -816,6 +817,12 @@ namespace MPIR /// the expression that will be computed, and the result set as the initial value of the new instance. MPTYPE(IntegerExpression^ value); + /// + /// Initializes a new float instance and sets its value to the result of computing the source expression. + /// + /// the expression that will be computed, and the result set as the initial value of the new instance. + MPTYPE(RationalExpression^ value); + /// /// Constructs and returns a new float instance with its value set to . /// @@ -1003,7 +1010,7 @@ namespace MPIR void SetTo(String^ value, int base); /// - /// Sets the value of the raitonal object. + /// Sets the value of the float object. /// Do not change the value of an object while it is contained in a hash table, because that changes its hash code. /// /// new value for the object @@ -1014,6 +1021,13 @@ namespace MPIR MP(set_z)(_value, CTXTI(0)); } + /// + /// Sets the value of the float object. + /// Do not change the value of an object while it is contained in a hash table, because that changes its hash code. + /// + /// new value for the object + void SetTo(RationalExpression^ value); + /// /// Swaps the values of two floats. /// This operation is a pointer swap and doesn't affect allocated memory. diff --git a/mpir.net/mpir.net/HugeRational.cpp b/mpir.net/mpir.net/HugeRational.cpp index c21435a2..cd679381 100644 --- a/mpir.net/mpir.net/HugeRational.cpp +++ b/mpir.net/mpir.net/HugeRational.cpp @@ -19,6 +19,7 @@ along with the MPIR Library. If not, see http://www.gnu.org/licenses/. #include "Stdafx.h" #include "HugeInt.h" +#include "HugeFloat.h" #include "HugeRational.h" //#include "Random.h" @@ -56,6 +57,13 @@ namespace MPIR SetTo(value); } + MPTYPE::MPTYPE(FloatExpression^ value) + { + AllocateStruct(); + MP(init)(_value); + SetTo(value); + } + MPTYPE::MPTYPE(IntegerExpression^ numerator, IntegerExpression^ denominator) { AllocateStruct(); @@ -423,5 +431,11 @@ namespace MPIR mpz_set_q(_value, CTXT(0)); } + void HugeFloat::SetTo(MPEXPR_NAME^ value) + { + IN_CONTEXT(value); + mpf_set_q(_value, CTXT(0)); + } + #pragma endregion }; \ No newline at end of file diff --git a/mpir.net/mpir.net/HugeRational.h b/mpir.net/mpir.net/HugeRational.h index 28da9444..c24864fe 100644 --- a/mpir.net/mpir.net/HugeRational.h +++ b/mpir.net/mpir.net/HugeRational.h @@ -914,6 +914,12 @@ namespace MPIR /// the expression that will be computed, and the result set as the initial value of the new instance. MPTYPE(IntegerExpression^ value); + /// + /// Initializes a new rational instance and sets its value to the result of computing the source expression. + /// + /// the expression that will be computed, and the result set as the initial value of the new instance. + MPTYPE(FloatExpression^ value); + /// /// Constructs and returns a new rational instance with its value set to / . /// If the fraction is not in canonical form, Canonicalize() must be called. @@ -1145,6 +1151,13 @@ namespace MPIR mpz_set_ui(&_value->_mp_den, 1); } + /// + /// Sets the value of the raitonal object. + /// Do not change the value of an object while it is contained in a hash table, because that changes its hash code. + /// + /// new value for the object + void SetTo(FloatExpression^ value); + /// /// Sets the value of the raitonal object. /// Do not change the value of an object while it is contained in a hash table, because that changes its hash code.