Added HugeInt.AllocatedSize and HugeFloat.AllocatedPrecision properties

This commit is contained in:
Alex Dyachenko 2016-01-09 15:55:05 -05:00
parent 80de7b0c89
commit 59aabe3eca
5 changed files with 61 additions and 4 deletions

View File

@ -287,6 +287,23 @@ namespace MPIR.Tests.HugeFloatTests
}
}
[TestMethod]
public void FloatAllocatedPrecision()
{
using (var a = new HugeFloat(1))
using (var b = HugeFloat.Allocate(256))
{
var bAllocated = b.AllocatedPrecision;
var aAllocated = a.AllocatedPrecision;
Assert.IsTrue(bAllocated > aAllocated);
a.Precision = 64;
b.Precision = 64;
Assert.AreEqual(bAllocated, b.AllocatedPrecision);
Assert.AreEqual(aAllocated, a.AllocatedPrecision);
}
}
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void FloatSettingPrecisionOverAllocated()

View File

@ -285,6 +285,23 @@ namespace MPIR.Tests.HugeIntTests
}
}
[TestMethod]
public void IntAllocatedSize()
{
using (var a = new HugeInt("-0x10123456789ABCDEF0123456789ABCDEF0123456789ABCDEF"))
{
var allocated = a.AllocatedSize;
Assert.IsTrue(allocated >= (int)a.Size());
a.Value = -a;
Assert.AreEqual(allocated, a.AllocatedSize);
Assert.AreEqual(4UL, a.Size());
a.Value >>= 64;
Assert.AreEqual(3UL, a.Size());
Assert.AreEqual(allocated, a.AllocatedSize);
}
}
[TestMethod]
public void IntGetLimb()
{

View File

@ -61,10 +61,7 @@ namespace MPIR.Tests
if (_value(x) == IntPtr.Zero)
return 0;
unsafe
{
return ((int*)_value(x).ToPointer())[0];
}
return x.AllocatedSize;
}
internal static int NumberOfLimbsUsed(this HugeInt x)

View File

@ -1180,6 +1180,18 @@ namespace MPIR
}
}
/// <summary>
/// Gets the precision in bits that is currently allocated for internal storage of the mantissa.
/// <para>The precision actually in effect, used in calculations, is initially the same but may be reduced by setting the Precision property.
/// </para>However Precision cannot exceed AllocatedPrecision.
/// <para>To change AllocatedPrecision, call Reallocate().
/// </para>The value actually allocated may be slightly more than the number of bits requested by Allocate() or Reallocate().
/// </summary>
property mp_bitcnt_t AllocatedPrecision
{
mp_bitcnt_t get() { return _allocatedPrecision; }
}
/// <summary>
/// Set the precision of rop to be at least prec bits, reallocating its limbs data.
/// <para>The value in rop will be truncated to the new precision.

View File

@ -1510,6 +1510,20 @@ namespace MPIR
#pragma endregion
#pragma region Properties
/// <summary>
/// Gets the number of limbs currently allocated. This value will never be less than Size().
/// <para>When a new value is assigned to the object, more space is automatically allocated if necessary.
/// </para>Reallocate() can also be used manually.
/// </summary>
property int AllocatedSize
{
int get() { return _value->_mp_alloc; }
}
#pragma endregion
#pragma region conversions
/// <summary>