Float assignment tests.

This commit is contained in:
Alex Dyachenko 2014-06-16 17:10:05 -04:00
parent ef95a16dab
commit cc66a187e1
5 changed files with 59 additions and 26 deletions

View File

@ -97,6 +97,9 @@
<Compile Include="..\..\..\mpir.net\mpir.net-tests\HugeFloatTests\Arithmetic.cs"> <Compile Include="..\..\..\mpir.net\mpir.net-tests\HugeFloatTests\Arithmetic.cs">
<Link>HugeFloatTests\Arithmetic.cs</Link> <Link>HugeFloatTests\Arithmetic.cs</Link>
</Compile> </Compile>
<Compile Include="..\..\..\mpir.net\mpir.net-tests\HugeFloatTests\Assignment.cs">
<Link>HugeFloatTests\Assignment.cs</Link>
</Compile>
<Compile Include="..\..\..\mpir.net\mpir.net-tests\HugeFloatTests\Comparisons.cs"> <Compile Include="..\..\..\mpir.net\mpir.net-tests\HugeFloatTests\Comparisons.cs">
<Link>HugeFloatTests\Comparisons.cs</Link> <Link>HugeFloatTests\Comparisons.cs</Link>
</Compile> </Compile>

View File

@ -20,70 +20,81 @@ along with the MPIR Library. If not, see http://www.gnu.org/licenses/.
using System; using System;
using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace MPIR.Tests.HugeRationalTests namespace MPIR.Tests.HugeFloatTests
{ {
[TestClass] [TestClass]
public class Assignment public class Assignment
{ {
[TestMethod] [TestMethod]
public void RationalAssignCopy() public void FloatAssignCopy()
{ {
using (var a = new HugeRational("-222509832503450298345029835740293845720/115756986668303657898962467957")) var s = "-1.22250983250345029834502983574029384572";
using (var b = new HugeRational()) using (var a = new HugeFloat(s))
using (var b = new HugeFloat())
{ {
b.Value = a; b.Value = a;
Assert.AreEqual("-222509832503450298345029835740293845720/115756986668303657898962467957", b.ToString()); FloatAssert.AreEqual(s, b);
} }
} }
[TestMethod] [TestMethod]
public void RationalSwap() public void FloatSwap()
{ {
using (var a = new HugeRational("-222509832503450298345029835740293845720/115756986668303657898962467957")) using (var a = new HugeFloat(192))
using (var b = new HugeRational("2039847290878794872059384789347534534/590872612825179551336102196593")) using (var b = new HugeFloat(128))
{ {
var aValue = a._value(); var aValue = a._value();
var bValue = b._value(); var bValue = b._value();
var aPrec = a._allocatedPrecision;
var bPrec = b._allocatedPrecision;
a.Swap(b); a.Swap(b);
Assert.AreEqual(bValue, a._value()); Assert.AreEqual(bValue, a._value());
Assert.AreEqual(aValue, b._value()); Assert.AreEqual(aValue, b._value());
Assert.AreEqual(bPrec, a._allocatedPrecision);
Assert.AreEqual(aPrec, b._allocatedPrecision);
} }
} }
[TestMethod] [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 += 1;
a.Value *= 10; a.Value *= 10;
Assert.AreEqual("10542320795030095862464859203020/115756986668303657898962467957", a.ToString()); FloatAssert.AreEqual("9384750928347059283475234523461.15756986668303657898962467957", a);
} }
} }
[TestMethod] [TestMethod]
public void RationalAssignInt() public void FloatAssignInt()
{ {
using (var a = new HugeInt("222509832503450298345029835740293845720")) using (var a = new HugeInt("222509832503450298345029835740293845720"))
using (var b = new HugeRational("1/3")) using (var b = new HugeFloat())
{ {
b.SetTo(a); b.SetTo(a);
Assert.AreEqual("222509832503450298345029835740293845720/1", b.ToString()); FloatAssert.AreEqual("222509832503450298345029835740293845720.", b);
} }
} }
[TestMethod] [TestMethod]
public void RationalAssignInt2() public void FloatAssignDouble()
{ {
using (var a = new HugeInt("222509832503450298345029835740293845719")) using(var a = new HugeFloat())
using (var d = new HugeInt("115756986668303657898962467957"))
using (var b = new HugeRational("1/3"))
{ {
b.SetTo(a, d); a.SetTo(22250983250345.125);
Assert.AreEqual("222509832503450298345029835740293845719/115756986668303657898962467957", b.ToString()); Assert.IsTrue(a == 22250983250345.125);
b.SetTo(b.Numerator - b.Denominator, b.Denominator * 5); }
Assert.AreEqual(a - d, b.Numerator); }
Assert.AreEqual(d * 5, b.Denominator);
[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 //more tests coming here

View File

@ -19,6 +19,7 @@ along with the MPIR Library. If not, see http://www.gnu.org/licenses/.
#include "Stdafx.h" #include "Stdafx.h"
#include "HugeInt.h" #include "HugeInt.h"
#include "HugeRational.h"
#include "HugeFloat.h" #include "HugeFloat.h"
//#include "Random.h" //#include "Random.h"

View File

@ -48,6 +48,7 @@ using namespace System::Runtime::InteropServices;
#define MPEXPR(x) LIT(MPTYPE_NAME)##x##Expression #define MPEXPR(x) LIT(MPTYPE_NAME)##x##Expression
#define CTXT(x) context.FloatArgs[x] #define CTXT(x) context.FloatArgs[x]
#define CTXTI(x) context.IntArgs[x] #define CTXTI(x) context.IntArgs[x]
#define CTXTR(x) context.RationalArgs[x]
#define ASSIGN_TO CONCAT(AssignTo, LIT(MPTYPE_NAME)) #define ASSIGN_TO CONCAT(AssignTo, LIT(MPTYPE_NAME))
#include "ExpressionMacros.h" #include "ExpressionMacros.h"
@ -1042,7 +1043,7 @@ namespace MPIR
void SetTo(String^ value, int base); void SetTo(String^ value, int base);
/// <summary> /// <summary>
/// Sets the value of the raitonal object. /// Sets the value of the float 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>Do not change the value of an object while it is contained in a hash table, because that changes its hash code.
/// </para></summary> /// </para></summary>
/// <param name="value">new value for the object</param> /// <param name="value">new value for the object</param>
@ -1053,6 +1054,18 @@ namespace MPIR
MP(set_z)(_value, CTXTI(0)); MP(set_z)(_value, CTXTI(0));
} }
/// <summary>
/// Sets the value of the float 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></summary>
/// <param name="value">new value for the object</param>
void SetTo(RationalExpression^ value)
{
EvaluationContext context;
value->AssignToRational(context);
MP(set_q)(_value, CTXTR(0));
}
/// <summary> /// <summary>
/// Swaps the values of two floats. /// Swaps the values of two floats.
/// <para>This operation is a pointer swap and doesn't affect allocated memory. /// <para>This operation is a pointer swap and doesn't affect allocated memory.
@ -1062,8 +1075,13 @@ namespace MPIR
void Swap(MPTYPE^ a) void Swap(MPTYPE^ a)
{ {
MP(ptr) temp = a->_value; MP(ptr) temp = a->_value;
mp_bitcnt_t prec = a->_allocatedPrecision;
a->_value = _value; a->_value = _value;
a->_allocatedPrecision = _allocatedPrecision;
_value = temp; _value = temp;
_allocatedPrecision = prec;
} }
#pragma endregion #pragma endregion

View File

@ -1135,7 +1135,7 @@ namespace MPIR
void SetTo(String^ value, int base); void SetTo(String^ value, int base);
/// <summary> /// <summary>
/// Sets the value of the raitonal object. /// Sets the value of the rational 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>Do not change the value of an object while it is contained in a hash table, because that changes its hash code.
/// </para></summary> /// </para></summary>
/// <param name="value">new value for the object</param> /// <param name="value">new value for the object</param>
@ -1146,7 +1146,7 @@ namespace MPIR
} }
/// <summary> /// <summary>
/// Sets the value of the raitonal object. /// Sets the value of the rational 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>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. /// </para>If the fraction is not in canonical form, Canonicalize() must be called.
/// </summary> /// </summary>