From 20618e11ed2bb0f2bb095701f5228806d37a01e2 Mon Sep 17 00:00:00 2001 From: Alex Dyachenko Date: Mon, 26 May 2014 15:54:50 -0400 Subject: [PATCH] Assignment and construction from one or two integer expressions --- .../HugeRationalTests/Assignment.cs | 15 ++++++++ .../ConstructionAndDisposal.cs | 22 ++++++++++++ mpir.net/mpir.net/HugeRational.cpp | 14 ++++++++ mpir.net/mpir.net/HugeRational.h | 34 +++++++++++++++++-- 4 files changed, 83 insertions(+), 2 deletions(-) diff --git a/mpir.net/mpir.net-tests/HugeRationalTests/Assignment.cs b/mpir.net/mpir.net-tests/HugeRationalTests/Assignment.cs index 75de516d..00665cbd 100644 --- a/mpir.net/mpir.net-tests/HugeRationalTests/Assignment.cs +++ b/mpir.net/mpir.net-tests/HugeRationalTests/Assignment.cs @@ -71,6 +71,21 @@ namespace MPIR.Tests.HugeRationalTests Assert.AreEqual("222509832503450298345029835740293845720/1", b.ToString()); } } + + [TestMethod] + public void RationalAssignInt2() + { + using (var a = new HugeInt("222509832503450298345029835740293845719")) + using (var d = new HugeInt("115756986668303657898962467957")) + using (var b = new HugeRational("1/3")) + { + 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); + } + } //more tests coming here } } diff --git a/mpir.net/mpir.net-tests/HugeRationalTests/ConstructionAndDisposal.cs b/mpir.net/mpir.net-tests/HugeRationalTests/ConstructionAndDisposal.cs index e6a3d3fd..7ac111df 100644 --- a/mpir.net/mpir.net-tests/HugeRationalTests/ConstructionAndDisposal.cs +++ b/mpir.net/mpir.net-tests/HugeRationalTests/ConstructionAndDisposal.cs @@ -248,5 +248,27 @@ namespace MPIR.Tests.HugeRationalTests Assert.AreEqual(a + 1, b); } } + + [TestMethod] + public void RationalConstructorFromIntExpression() + { + using (var a = new HugeInt("2340958273409578234095823045723490587")) + using (var b = new HugeRational(a + 1)) + { + Assert.AreEqual("2340958273409578234095823045723490588/1", b.ToString()); + } + } + + [TestMethod] + public void RationalConstructorFromIntExpression2() + { + using (var a = new HugeInt("2340958273409578234095823045723490587")) + using (var d = new HugeInt("362736035870515331128527330659")) + using (var b = new HugeRational(a + 2, d * 2)) + { + Assert.AreEqual(a + 2, b.Numerator); + Assert.AreEqual(d * 2, b.Denominator); + } + } } } diff --git a/mpir.net/mpir.net/HugeRational.cpp b/mpir.net/mpir.net/HugeRational.cpp index a76be70c..c21435a2 100644 --- a/mpir.net/mpir.net/HugeRational.cpp +++ b/mpir.net/mpir.net/HugeRational.cpp @@ -48,6 +48,20 @@ namespace MPIR MP(init)(_value); value->AssignTo(_value); } + + MPTYPE::MPTYPE(IntegerExpression^ value) + { + AllocateStruct(); + MP(init)(_value); + SetTo(value); + } + + MPTYPE::MPTYPE(IntegerExpression^ numerator, IntegerExpression^ denominator) + { + AllocateStruct(); + MP(init)(_value); + SetTo(numerator, denominator); + } MPTYPE^ MPTYPE::Allocate(mp_bitcnt_t numeratorBits, mp_bitcnt_t denominatorBits) { diff --git a/mpir.net/mpir.net/HugeRational.h b/mpir.net/mpir.net/HugeRational.h index ed4bae6e..caa75d2c 100644 --- a/mpir.net/mpir.net/HugeRational.h +++ b/mpir.net/mpir.net/HugeRational.h @@ -908,6 +908,20 @@ namespace MPIR /// the expression that will be computed, and the result set as the initial value of the new instance. MPTYPE(MPEXPR_NAME^ 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(IntegerExpression^ 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. + /// + /// Numerator for the initial value for the new rational instance + /// Denominator for the initial value for the new rational instance + MPTYPE(IntegerExpression^ numerator, IntegerExpression^ denominator); + /// /// Constructs and returns a new rational instance with its value set to / . /// If the fraction is not in canonical form, Canonicalize() must be called. @@ -1156,9 +1170,25 @@ namespace MPIR /// new value for the object void SetTo(IntegerExpression^ value) { + value->AssignTo(&_value->_mp_num); + 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. + /// If the fraction is not in canonical form, Canonicalize() must be called. + /// + /// Numerator for the new value for the object + /// Denominator for the new value for the object + void SetTo(IntegerExpression^ numerator, IntegerExpression^ denominator) + { + //use context in case source expressions reference the previous numerator or denominator of the rational EvaluationContext context; - value->AssignToInteger(context); - MP(set_z)(_value, CTXTI(0)); + numerator->AssignToInteger(context); + denominator->AssignToInteger(context); + mpz_set(&_value->_mp_num, CTXTI(0)); + mpz_set(&_value->_mp_den, CTXTI(1)); } ///