From cc66a187e1d341dd5c03eef2c1867fd48825950a Mon Sep 17 00:00:00 2001 From: Alex Dyachenko Date: Mon, 16 Jun 2014 17:10:05 -0400 Subject: [PATCH] Float assignment tests. --- .../mpir.net-tests/mpir.net-tests.csproj | 3 + .../HugeFloatTests/Assignment.cs | 57 +++++++++++-------- mpir.net/mpir.net/HugeFloat.cpp | 1 + mpir.net/mpir.net/HugeFloat.h | 20 ++++++- mpir.net/mpir.net/HugeRational.h | 4 +- 5 files changed, 59 insertions(+), 26 deletions(-) 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 8995d327..fea31899 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 @@ -97,6 +97,9 @@ HugeFloatTests\Arithmetic.cs + + HugeFloatTests\Assignment.cs + HugeFloatTests\Comparisons.cs diff --git a/mpir.net/mpir.net-tests/HugeFloatTests/Assignment.cs b/mpir.net/mpir.net-tests/HugeFloatTests/Assignment.cs index 00665cbd..1fe241d9 100644 --- a/mpir.net/mpir.net-tests/HugeFloatTests/Assignment.cs +++ b/mpir.net/mpir.net-tests/HugeFloatTests/Assignment.cs @@ -20,70 +20,81 @@ 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 Assignment { [TestMethod] - public void RationalAssignCopy() + public void FloatAssignCopy() { - using (var a = new HugeRational("-222509832503450298345029835740293845720/115756986668303657898962467957")) - using (var b = new HugeRational()) + var s = "-1.22250983250345029834502983574029384572"; + using (var a = new HugeFloat(s)) + using (var b = new HugeFloat()) { b.Value = a; - Assert.AreEqual("-222509832503450298345029835740293845720/115756986668303657898962467957", b.ToString()); + FloatAssert.AreEqual(s, b); } } [TestMethod] - public void RationalSwap() + public void FloatSwap() { - using (var a = new HugeRational("-222509832503450298345029835740293845720/115756986668303657898962467957")) - using (var b = new HugeRational("2039847290878794872059384789347534534/590872612825179551336102196593")) + using (var a = new HugeFloat(192)) + using (var b = new HugeFloat(128)) { var aValue = a._value(); var bValue = b._value(); + var aPrec = a._allocatedPrecision; + var bPrec = b._allocatedPrecision; a.Swap(b); Assert.AreEqual(bValue, a._value()); Assert.AreEqual(aValue, b._value()); + Assert.AreEqual(bPrec, a._allocatedPrecision); + Assert.AreEqual(aPrec, b._allocatedPrecision); } } [TestMethod] - public void RationalCompoundOperators() + public void FloatCompoundOperators() { - using (var a = new HugeRational("938475092834705928347523452345/115756986668303657898962467957")) + using (var a = new HugeFloat("938475092834705928347523452345.115756986668303657898962467957")) { a.Value += 1; a.Value *= 10; - Assert.AreEqual("10542320795030095862464859203020/115756986668303657898962467957", a.ToString()); + FloatAssert.AreEqual("9384750928347059283475234523461.15756986668303657898962467957", a); } } [TestMethod] - public void RationalAssignInt() + public void FloatAssignInt() { using (var a = new HugeInt("222509832503450298345029835740293845720")) - using (var b = new HugeRational("1/3")) + using (var b = new HugeFloat()) { b.SetTo(a); - Assert.AreEqual("222509832503450298345029835740293845720/1", b.ToString()); + FloatAssert.AreEqual("222509832503450298345029835740293845720.", b); } } [TestMethod] - public void RationalAssignInt2() + public void FloatAssignDouble() { - using (var a = new HugeInt("222509832503450298345029835740293845719")) - using (var d = new HugeInt("115756986668303657898962467957")) - using (var b = new HugeRational("1/3")) + using(var a = new HugeFloat()) { - b.SetTo(a, d); - Assert.AreEqual("222509832503450298345029835740293845719/115756986668303657898962467957", b.ToString()); - b.SetTo(b.Numerator - b.Denominator, b.Denominator * 5); - Assert.AreEqual(a - d, b.Numerator); - Assert.AreEqual(d * 5, b.Denominator); + a.SetTo(22250983250345.125); + Assert.IsTrue(a == 22250983250345.125); + } + } + + [TestMethod] + public void FloatAssignRational() + { + using (var a = new HugeRational(1, 3)) + using (var b = new HugeFloat()) + { + b.SetTo(a); + FloatAssert.AreEqual(".3333333333333333333333333333333333333333333333333333333333", b); } } //more tests coming here diff --git a/mpir.net/mpir.net/HugeFloat.cpp b/mpir.net/mpir.net/HugeFloat.cpp index c57ca194..d32c7ccf 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" diff --git a/mpir.net/mpir.net/HugeFloat.h b/mpir.net/mpir.net/HugeFloat.h index 8e8962ca..c8480b33 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" @@ -1042,7 +1043,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 @@ -1053,6 +1054,18 @@ 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) + { + EvaluationContext context; + value->AssignToRational(context); + MP(set_q)(_value, CTXTR(0)); + } + /// /// Swaps the values of two floats. /// This operation is a pointer swap and doesn't affect allocated memory. @@ -1062,8 +1075,13 @@ namespace MPIR void Swap(MPTYPE^ a) { MP(ptr) temp = a->_value; + mp_bitcnt_t prec = a->_allocatedPrecision; + a->_value = _value; + a->_allocatedPrecision = _allocatedPrecision; + _value = temp; + _allocatedPrecision = prec; } #pragma endregion diff --git a/mpir.net/mpir.net/HugeRational.h b/mpir.net/mpir.net/HugeRational.h index 28da9444..0d28c2f5 100644 --- a/mpir.net/mpir.net/HugeRational.h +++ b/mpir.net/mpir.net/HugeRational.h @@ -1135,7 +1135,7 @@ namespace MPIR void SetTo(String^ value, int base); /// - /// Sets the value of the raitonal object. + /// Sets the value of the rational 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 @@ -1146,7 +1146,7 @@ namespace MPIR } /// - /// Sets the value of the raitonal object. + /// Sets the value of the rational object. /// Do not change the value of an object while it is contained in a hash table, because that changes its hash code. /// If the fraction is not in canonical form, Canonicalize() must be called. ///