Assignment and construction from one or two integer expressions
This commit is contained in:
parent
1771a0d36e
commit
20618e11ed
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
{
|
||||
|
@ -908,6 +908,20 @@ namespace MPIR
|
||||
/// <param name="value">the expression that will be computed, and the result set as the initial value of the new instance.</param>
|
||||
MPTYPE(MPEXPR_NAME^ value);
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new rational instance and sets its value to the result of computing the source expression.
|
||||
/// </summary>
|
||||
/// <param name="value">the expression that will be computed, and the result set as the initial value of the new instance.</param>
|
||||
MPTYPE(IntegerExpression^ value);
|
||||
|
||||
/// <summary>
|
||||
/// Constructs and returns a new rational instance with its value set to <paramref name="numerator"/> / <paramref name="denominator"/>.
|
||||
/// <para>If the fraction is not in canonical form, Canonicalize() must be called.</para>
|
||||
/// </summary>
|
||||
/// <param name="numerator">Numerator for the initial value for the new rational instance</param>
|
||||
/// <param name="denominator">Denominator for the initial value for the new rational instance</param>
|
||||
MPTYPE(IntegerExpression^ numerator, IntegerExpression^ denominator);
|
||||
|
||||
/// <summary>
|
||||
/// Constructs and returns a new rational instance with its value set to <paramref name="numerator"/> / <paramref name="denominator"/>.
|
||||
/// <para>If the fraction is not in canonical form, Canonicalize() must be called.</para>
|
||||
@ -1156,9 +1170,25 @@ namespace MPIR
|
||||
/// <param name="value">new value for the object</param>
|
||||
void SetTo(IntegerExpression^ value)
|
||||
{
|
||||
value->AssignTo(&_value->_mp_num);
|
||||
mpz_set_ui(&_value->_mp_den, 1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the value of the raitonal object.
|
||||
/// <para>Do not change the value of an object while it is contained in a hash table, because that changes its hash code.
|
||||
/// </para>If the fraction is not in canonical form, Canonicalize() must be called.
|
||||
/// </summary>
|
||||
/// <param name="numerator">Numerator for the new value for the object</param>
|
||||
/// <param name="denominator">Denominator for the new value for the object</param>
|
||||
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));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
Loading…
Reference in New Issue
Block a user