From 35a2222d955953eebb57973921b18d6345594301 Mon Sep 17 00:00:00 2001 From: Alex Dyachenko Date: Fri, 27 Jun 2014 10:53:40 -0400 Subject: [PATCH] Assignment float-to-rational --- .../mpir.net-tests/HugeRationalTests/Assignment.cs | 11 +++++++++++ mpir.net/mpir.net/HugeFloat.cpp | 8 +++++++- mpir.net/mpir.net/HugeRational.h | 7 +++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/mpir.net/mpir.net-tests/HugeRationalTests/Assignment.cs b/mpir.net/mpir.net-tests/HugeRationalTests/Assignment.cs index 00665cbd..8a5ec838 100644 --- a/mpir.net/mpir.net-tests/HugeRationalTests/Assignment.cs +++ b/mpir.net/mpir.net-tests/HugeRationalTests/Assignment.cs @@ -86,6 +86,17 @@ namespace MPIR.Tests.HugeRationalTests Assert.AreEqual(d * 5, b.Denominator); } } + + [TestMethod] + public void RationalAssignFloat() + { + using(var a = new HugeFloat("222509832503450298342455029.125")) + using(var b = new HugeRational("1/3")) + { + b.SetTo(a); + Assert.AreEqual("1780078660027602386739640233/8", b.ToString()); + } + } //more tests coming here } } diff --git a/mpir.net/mpir.net/HugeFloat.cpp b/mpir.net/mpir.net/HugeFloat.cpp index 04c1fd80..514d2b9d 100644 --- a/mpir.net/mpir.net/HugeFloat.cpp +++ b/mpir.net/mpir.net/HugeFloat.cpp @@ -427,7 +427,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) { @@ -435,5 +435,11 @@ namespace MPIR mpz_set_f(_value, CTXT(0)); } + void HugeRational::SetTo(FloatExpression^ 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/HugeRational.h b/mpir.net/mpir.net/HugeRational.h index 0d28c2f5..e8bf9934 100644 --- a/mpir.net/mpir.net/HugeRational.h +++ b/mpir.net/mpir.net/HugeRational.h @@ -1162,6 +1162,13 @@ namespace MPIR mpz_set(&_value->_mp_den, CTXTI(1)); } + /// + /// Sets the value of the rational object. There is no rounding, this conversion is exact. + /// 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); + /// /// Swaps the values of two rationals. /// This operation is a pointer swap and doesn't affect allocated memory.