Skip to content

Commit

Permalink
- Fixes Bug #45 - Restore the ability to raise a value to a decimal p…
Browse files Browse the repository at this point in the history
…ower.

- Minor Version Bump
  • Loading branch information
AdamWhiteHat committed May 8, 2024
1 parent 37c6d6e commit 8d3de28
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
15 changes: 15 additions & 0 deletions BigDecimal/BigDecimal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -852,6 +852,21 @@ private static BigDecimal Pow_Precision(BigDecimal baseValue, BigInteger exponen
return result;
}

/// <summary>Returns a specified number raised to the specified power.</summary>
public static BigDecimal Pow(Double basis, Double exponent)
{
var tmp = One;
while (Math.Abs(exponent) > ExpChunk)
{
var diff = exponent > 0 ? ExpChunk : -ExpChunk;
tmp *= Math.Pow(basis, diff);
exponent -= diff;
}

return tmp * Math.Pow(basis, exponent);
}
private static double ExpChunk = 2.0d;

public static BigDecimal SquareRoot(BigDecimal input, Int32 decimalPlaces)
{
return NthRoot(input, 2, decimalPlaces);
Expand Down
2 changes: 1 addition & 1 deletion BigDecimal/BigDecimal.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

<PropertyGroup>
<MajorVersion Condition=" '$(MajorVersion)' == '' ">2025</MajorVersion>
<MinorVersion Condition=" '$(MinorVersion)' == '' ">1000</MinorVersion>
<MinorVersion Condition=" '$(MinorVersion)' == '' ">1001</MinorVersion>
<PatchVersion Condition=" '$(PatchVersion)' == '' ">2</PatchVersion>

<BuildTimestamp>$([System.DateTime]::Now.DayOfYear.ToString().PadLeft(3,'0'))</BuildTimestamp>
Expand Down
54 changes: 54 additions & 0 deletions TestBigDecimal/TestBigDecimalOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,60 @@ public void TestMultiply2()
Assert.AreEqual(expected, actual.ToString());
}


[Test]
public void TestExponentiation1()
{
double exp = 0.052631578947368421d;
double phi = (1.0d + Math.Sqrt(5)) / 2.0d;

BigDecimal result1 = BigDecimal.Pow(9.0d, 0.5d);
BigDecimal result2 = BigDecimal.Pow(16.0d, 0.25d);
string expected1 = "3";
string expected2 = "2";

BigDecimal result3 = BigDecimal.Pow(phi, 13);
string expected3 = "521.001919378725";

BigDecimal result4 = BigDecimal.Pow(1162261467, exp);
BigDecimal result5 = BigDecimal.Pow(9349, exp);
string expected4 = "3";
string expected5 = "1.61803398777557";

BigDecimal result6 = BigDecimal.Pow(1.618034d, 16.000000256d);
BigDecimal result7 = BigDecimal.Pow(phi, 20.0000000128d);
string expected6 = "2207.00006429941";
string expected7 = "15127.0000270679";

BigDecimal result8 = BigDecimal.Pow(29192926025390625d, 0.07142857142857142d);
string expected8 = "14.999999999999998";

Assert.AreEqual(expected1, result1.ToString());
Assert.AreEqual(expected2, result2.ToString());
Assert.AreEqual(expected3, result3.ToString().Substring(0, 16));
Assert.AreEqual(expected4, result4.ToString());
Assert.AreEqual(expected5, result5.ToString().Substring(0, 16));
Assert.AreEqual(expected6, result6.ToString().Substring(0, 16));
Assert.AreEqual(expected7, result7.ToString().Substring(0, 16));
Assert.AreEqual(expected8, result8.ToString());
}

[Test]
public void TestExponentiation2()
{
BigDecimal result1 = BigDecimal.Pow(new BigDecimal(16), new BigInteger(16));
BigDecimal result2 = BigDecimal.Pow(new BigDecimal(101), new BigInteger(13));
string expected1 = "18446744073709551616";
string expected2 = "113809328043328941786781301";

BigDecimal result3 = BigDecimal.Pow(new BigDecimal(0.25), 2);
string expected3 = "0.0625";

Assert.AreEqual(expected1, result1.ToString());
Assert.AreEqual(expected2, result2.ToString());
Assert.AreEqual(expected3, result3.ToString());
}

[Test]
public void TestNegate()
{
Expand Down

0 comments on commit 8d3de28

Please sign in to comment.