Merge branch 'master' of github.com:adyache/mpir

Conflicts:
	mpir.net/mpir.net/HugeRational.h
This commit is contained in:
Alex Dyachenko 2014-06-03 12:59:35 -04:00
commit 55f82b2046
5 changed files with 1442 additions and 1295 deletions

View File

@ -374,6 +374,39 @@ namespace MPIR.Tests.HugeRationalTests
#endregion
#region Invert
[TestMethod]
public void RationalInvert()
{
using(var a = new HugeRational("-24092854092874502983745029345723098457209/115756986668303657898962467957"))
{
a.Value = a.Invert();
Assert.AreEqual("-115756986668303657898962467957/24092854092874502983745029345723098457209", a.ToString());
a.Value = a.Invert();
Assert.AreEqual("-24092854092874502983745029345723098457209/115756986668303657898962467957", a.ToString());
}
}
#endregion
#region Power
[TestMethod]
public void RationalPower()
{
using(var n = new HugeInt("-24092854092874502983745029345723098457209"))
using(var d = new HugeInt("115756986668303657898962467957"))
using(var a = new HugeRational(n, d))
{
a.Value = a ^ 5;
Assert.AreEqual(n ^ 5, a.Numerator);
Assert.AreEqual(d ^ 5, a.Denominator);
}
}
#endregion
#region Divide
#region Int
@ -434,6 +467,39 @@ namespace MPIR.Tests.HugeRationalTests
}
}
[TestMethod]
public void RationalDivideSignedLimb()
{
using(var a = new HugeRational("115756986668303657898962467957/39458029384750298767200622330399462537522498"))
{
long b = -5931690917503076532;
a.Value = a / b;
Assert.AreEqual("-115756986668303657898962467957/234052834524092854092874502983745029345723092857791404165816936", a.ToString());
}
}
[TestMethod]
public void RationalDivideLimbBy()
{
using(var a = new HugeRational("115756986668303657898962467957/39458029384750298767200622330399462537522498"))
{
ulong b = 5931690917503076532;
a.Value = b / a;
Assert.AreEqual("234052834524092854092874502983745029345723092857791404165816936/115756986668303657898962467957", a.ToString());
}
}
[TestMethod]
public void RationalDivideSignedLimbBy()
{
using(var a = new HugeRational("115756986668303657898962467957/39458029384750298767200622330399462537522498"))
{
long b = -5931690917503076532;
a.Value = b / a;
Assert.AreEqual("-234052834524092854092874502983745029345723092857791404165816936/115756986668303657898962467957", a.ToString());
}
}
#endregion
#endregion

View File

@ -92,6 +92,21 @@ namespace MPIR.Tests.HugeRationalTests
}
}
[TestMethod]
public void RationalCompareToLimb2()
{
using(var a = new HugeRational("-222509947/127"))
{
ulong b = 222509820;
ulong d = 127;
Assert.AreEqual(-1, System.Math.Sign(a.CompareTo(b, d)));
Assert.AreEqual(-1, System.Math.Sign(a.CompareTo(b + 1, d)));
Assert.AreEqual(0, System.Math.Sign((-(a + 1)).CompareTo(b, d)));
Assert.AreEqual(0, System.Math.Sign((-a).CompareTo(b + d, d)));
Assert.AreEqual(1, System.Math.Sign((-a).CompareTo(b, d)));
}
}
[TestMethod]
public void RationalCompareToSignedLimb()
{
@ -106,6 +121,22 @@ namespace MPIR.Tests.HugeRationalTests
}
}
[TestMethod]
public void RationalCompareToSignedLimb2()
{
using(var a = new HugeRational("-222509947/127"))
{
long b = -222509820;
long d = 127;
Assert.AreEqual(-1, System.Math.Sign(a.CompareTo(b, (ulong)d)));
Assert.AreEqual(-1, System.Math.Sign(a.CompareTo(b + 1, (ulong)d)));
Assert.AreEqual(0, System.Math.Sign((a + 1).CompareTo(b, (ulong)d)));
Assert.AreEqual(-1, System.Math.Sign(a.CompareTo(b - d + 1, (ulong)d)));
Assert.AreEqual(0, System.Math.Sign(a.CompareTo(b - d, (ulong)d)));
Assert.AreEqual(1, System.Math.Sign(a.CompareTo(b - d - 1, (ulong)d)));
}
}
[TestMethod]
public void RationalCompareToSignedDouble()
{
@ -453,6 +484,32 @@ namespace MPIR.Tests.HugeRationalTests
}
}
[TestMethod]
public void RationalEqualsLimb2()
{
using(var a = new HugeRational("222509832377/127"))
{
ulong b = 222509832504;
ulong d = 127;
Assert.IsFalse(a.Equals(b + 1, d));
Assert.IsTrue(a.Equals(b - d, d));
Assert.IsTrue((a + 1).Equals(b, d));
}
}
[TestMethod]
public void RationalEqualsSignedLimb2()
{
using(var a = new HugeRational("-222509832377/127"))
{
long b = -222509832504;
ulong d = 127;
Assert.IsFalse(a.Equals(b + 1, d));
Assert.IsTrue(a.Equals(b + (long)d, d));
Assert.IsTrue((a - 1).Equals(b, d));
}
}
[TestMethod]
public void RationalEqualsDouble()
{

View File

@ -178,6 +178,17 @@ namespace MPIR.Tests.HugeRationalTests
}
}
[TestMethod]
public void RationalStringConstructorNumeratorOnly()
{
var n = "5432109876543212345789023245987";
using(var a = new HugeRational(n))
{
Assert.AreEqual(2, a.NumeratorNumberOfLimbsUsed());
Assert.AreEqual(n + "/1", a.ToString());
}
}
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void RationalStringConstructorInvalid()
@ -198,8 +209,10 @@ namespace MPIR.Tests.HugeRationalTests
using (var i = new HugeInt("362736035870515331128527330659"))
{
var d = i.ToString(16);
using (var a = new HugeRational("143210ABCDEF32123457ACDB324598799/" + d, 16))
var n = "143210ABCDEF32123457ACDB324598799";
using (var a = new HugeRational(n + "/" + d, 16))
{
Assert.AreEqual(n, a.Numerator.ToString(16));
Assert.AreEqual(3, a.NumeratorNumberOfLimbsUsed());
Assert.AreEqual(i, a.Denominator);
}

View File

@ -60,6 +60,9 @@ namespace MPIR.Tests.HugeRationalTests
ulong d = 12764787846358441471;
a.SetTo(b, d);
Assert.AreEqual(b.ToString() + "/" + d.ToString(), a.ToString());
a.SetTo(b);
Assert.AreEqual(b.ToString() + "/1", a.ToString());
}
}
@ -72,6 +75,9 @@ namespace MPIR.Tests.HugeRationalTests
ulong d = 12764787846358441471;
a.SetTo(b, d);
Assert.AreEqual(b.ToString() + "/" + d.ToString(), a.ToString());
a.SetTo(b);
Assert.AreEqual(b.ToString() + "/1", a.ToString());
}
}
@ -114,6 +120,10 @@ namespace MPIR.Tests.HugeRationalTests
n = "-98ABCDEF876529834765234123984761/17607EF654EB9A13FFA163C75";
a.SetTo(n, 16);
Assert.AreEqual(n, a.ToString(16));
n = "-98ABCDEF876529834765234123984761";
a.SetTo(n, 16);
Assert.AreEqual(n + "/1", a.ToString(16));
}
}

View File

@ -1005,7 +1005,8 @@ namespace MPIR
/// <summary>
/// Returns the value of the number as a double, truncating if necessary (rounding towards zero).
/// <para>If the exponent from the conversion is too big, the result is system dependent. An infinity is returned where available. /// A hardware overflow trap may or may not occur.
/// <para>If the exponent from the conversion is too big, the result is system dependent. An infinity is returned where available.
/// A hardware overflow trap may or may not occur.
/// </para></summary>
/// <returns>The value as a double, possibly truncated.</returns>
double ToDouble() { return MP(get_d)(_value); }