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">
<Link>HugeFloatTests\Arithmetic.cs</Link>
</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">
<Link>HugeFloatTests\Comparisons.cs</Link>
</Compile>

View File

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

View File

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

View File

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

View File

@ -1135,7 +1135,7 @@ namespace MPIR
void SetTo(String^ value, int base);
/// <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></summary>
/// <param name="value">new value for the object</param>
@ -1146,7 +1146,7 @@ namespace MPIR
}
/// <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>If the fraction is not in canonical form, Canonicalize() must be called.
/// </summary>