Assignment float-to-rational

This commit is contained in:
Alex Dyachenko 2014-06-27 10:53:40 -04:00
parent 94a9578826
commit 35a2222d95
3 changed files with 25 additions and 1 deletions

View File

@ -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
}
}

View File

@ -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
};

View File

@ -1162,6 +1162,13 @@ namespace MPIR
mpz_set(&_value->_mp_den, CTXTI(1));
}
/// <summary>
/// Sets the value of the rational object. There is no rounding, this conversion is exact.
/// <para>Do not change the value of an object while it is contained in a hash table, because that changes its hash code.
/// </para></summary>
/// <param name="value">new value for the object</param>
void SetTo(FloatExpression^ value);
/// <summary>
/// Swaps the values of two rationals.
/// <para>This operation is a pointer swap and doesn't affect allocated memory.