diff --git a/src/Managed.UnitTests/NumberTheoryTests/IntegerTheoryTest.cs b/src/Managed.UnitTests/NumberTheoryTests/IntegerTheoryTest.cs index 8f8db543..9a81d7b2 100644 --- a/src/Managed.UnitTests/NumberTheoryTests/IntegerTheoryTest.cs +++ b/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() { diff --git a/src/Managed/NumberTheory/IntegerTheory.cs b/src/Managed/NumberTheory/IntegerTheory.cs index dfe0bf8a..46799aae 100644 --- a/src/Managed/NumberTheory/IntegerTheory.cs +++ b/src/Managed/NumberTheory/IntegerTheory.cs @@ -154,6 +154,38 @@ namespace MathNet.Numerics.NumberTheory return number + 1; } + /// + /// Raises 2 to the provided integer exponent (0 <= exponent < 31). + /// + /// The exponent to raise 2 up to. + /// 2 ^ exponent. + /// + public static int PowerOfTwo(this int exponent) + { + if (exponent < 0 || exponent >= 31) + { + throw new ArgumentOutOfRangeException("exponent"); + } + + return 1 << exponent; + } + + /// + /// Raises 2 to the provided integer exponent (0 <= exponent < 63). + /// + /// The exponent to raise 2 up to. + /// 2 ^ exponent. + /// + public static long PowerOfTwo(this long exponent) + { + if (exponent < 0 || exponent >= 63) + { + throw new ArgumentOutOfRangeException("exponent"); + } + + return ((long)1) << (int)exponent; + } + /// /// Find out whether the provided 32 bit integer is a perfect square, i.e. a square of an integer. ///