Browse Source

number theory: power of two

Signed-off-by: Christoph Ruegg <git@cdrnet.ch>
pull/36/head
Christoph Ruegg 17 years ago
parent
commit
91945b882f
  1. 70
      src/Managed.UnitTests/NumberTheoryTests/IntegerTheoryTest.cs
  2. 32
      src/Managed/NumberTheory/IntegerTheory.cs

70
src/Managed.UnitTests/NumberTheoryTests/IntegerTheoryTest.cs

@ -231,6 +231,76 @@ namespace MathNet.Numerics.UnitTests.NumberTheoryTests
() => IntegerTheory.CeilingToPowerOfTwo(maxPowerOfTwo - 1));
}
[Test]
public void PowerOfTwoMatchesFloatingPointPower32()
{
for(int i=0; i<31; i++)
{
Assert.AreEqual(Math.Round(Math.Pow(2, i)), IntegerTheory.PowerOfTwo(i));
}
}
[Test]
public void PowerOfTwoMatchesFloatingPointPower64()
{
for (int i = 0; i < 63; i++)
{
Assert.AreEqual(Math.Round(Math.Pow(2, i)), IntegerTheory.PowerOfTwo((long)i));
}
}
[Test]
public void PowerOfTwoThrowsWhenOutOfRange32()
{
Assert.Throws(
typeof(ArgumentOutOfRangeException),
() => IntegerTheory.PowerOfTwo(-1));
Assert.Throws(
typeof(ArgumentOutOfRangeException),
() => IntegerTheory.PowerOfTwo(31));
Assert.Throws(
typeof(ArgumentOutOfRangeException),
() => IntegerTheory.PowerOfTwo(Int32.MinValue));
Assert.Throws(
typeof(ArgumentOutOfRangeException),
() => IntegerTheory.PowerOfTwo(Int32.MaxValue));
Assert.DoesNotThrow(
() => IntegerTheory.PowerOfTwo(30));
Assert.DoesNotThrow(
() => IntegerTheory.PowerOfTwo(0));
}
[Test]
public void PowerOfTwoThrowsWhenOutOfRange64()
{
Assert.Throws(
typeof(ArgumentOutOfRangeException),
() => IntegerTheory.PowerOfTwo((long)-1));
Assert.Throws(
typeof(ArgumentOutOfRangeException),
() => IntegerTheory.PowerOfTwo((long)63));
Assert.Throws(
typeof(ArgumentOutOfRangeException),
() => IntegerTheory.PowerOfTwo(Int64.MinValue));
Assert.Throws(
typeof(ArgumentOutOfRangeException),
() => IntegerTheory.PowerOfTwo(Int64.MaxValue));
Assert.DoesNotThrow(
() => IntegerTheory.PowerOfTwo((long)62));
Assert.DoesNotThrow(
() => IntegerTheory.PowerOfTwo((long)0));
}
[Test]
public void TestIsPerfectSquare32()
{

32
src/Managed/NumberTheory/IntegerTheory.cs

@ -154,6 +154,38 @@ namespace MathNet.Numerics.NumberTheory
return number + 1;
}
/// <summary>
/// Raises 2 to the provided integer exponent (0 &lt;= exponent &lt; 31).
/// </summary>
/// <param name="exponent">The exponent to raise 2 up to.</param>
/// <returns>2 ^ exponent.</returns>
/// <exception cref="ArgumentOutOfRangeException"/>
public static int PowerOfTwo(this int exponent)
{
if (exponent < 0 || exponent >= 31)
{
throw new ArgumentOutOfRangeException("exponent");
}
return 1 << exponent;
}
/// <summary>
/// Raises 2 to the provided integer exponent (0 &lt;= exponent &lt; 63).
/// </summary>
/// <param name="exponent">The exponent to raise 2 up to.</param>
/// <returns>2 ^ exponent.</returns>
/// <exception cref="ArgumentOutOfRangeException"/>
public static long PowerOfTwo(this long exponent)
{
if (exponent < 0 || exponent >= 63)
{
throw new ArgumentOutOfRangeException("exponent");
}
return ((long)1) << (int)exponent;
}
/// <summary>
/// Find out whether the provided 32 bit integer is a perfect square, i.e. a square of an integer.
/// </summary>

Loading…
Cancel
Save