Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions SignificantNumber.Test/SignificantNumberTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,48 @@ public void Pow_NegativeBaseAndPositiveExponent_ReturnsCorrectResult()
Assert.AreEqual(SignificantNumber.CreateFromComponents(0, new BigInteger(9)), result);
}

[TestMethod]
public void Pow_NegativeBaseAndOddExponent_ReturnsNegativeResult()
{
SignificantNumber baseNumber = SignificantNumber.CreateFromComponents(0, new BigInteger(-2));
SignificantNumber power = SignificantNumber.CreateFromComponents(0, new BigInteger(3));
SignificantNumber result = baseNumber.Pow(power);

// Expected result is (-2)^3 = -8
Assert.AreEqual(SignificantNumber.CreateFromComponents(0, new BigInteger(-8)), result);
}

[TestMethod]
public void Pow_NegativeOneAndOddExponent_ReturnsNegativeOne()
{
SignificantNumber baseNumber = SignificantNumber.CreateFromComponents(0, new BigInteger(-1));
SignificantNumber power = SignificantNumber.CreateFromComponents(0, new BigInteger(3));
SignificantNumber result = baseNumber.Pow(power);

// Expected result is (-1)^3 = -1
Assert.AreEqual(SignificantNumber.CreateFromComponents(0, new BigInteger(-1)), result);
}

[TestMethod]
public void Pow_NegativeBaseAndFractionalExponent_ThrowsArgumentException()
{
SignificantNumber baseNumber = SignificantNumber.CreateFromComponents(0, new BigInteger(-2));
SignificantNumber power = SignificantNumber.CreateFromComponents(-1, new BigInteger(5));

// (-2)^0.5 has no real result
Assert.ThrowsExactly<ArgumentException>(() => baseNumber.Pow(power));
}

[TestMethod]
public void Pow_ZeroBaseAndNegativeExponent_ThrowsDivideByZeroException()
{
SignificantNumber baseNumber = SignificantNumber.CreateFromComponents(0, BigInteger.Zero);
SignificantNumber power = SignificantNumber.CreateFromComponents(0, new BigInteger(-1));

// 0^-1 is 1/0
Assert.ThrowsExactly<DivideByZeroException>(() => baseNumber.Pow(power));
}

[TestMethod]
public void Operator_Addition_WithPreciseNumber_ReturnsCorrectResult()
{
Expand Down
19 changes: 16 additions & 3 deletions SignificantNumber/SignificantNumber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,8 @@ internal static bool DoesImplementGenericInterface(Type type, Type genericInterf
/// </summary>
/// <param name="power">The power to raise the significant number to.</param>
/// <returns>The current number raised to <paramref name="power"/>, rounded to the fewest significant digits of the two.</returns>
/// <exception cref="DivideByZeroException">Thrown when the current number is zero and <paramref name="power"/> is negative.</exception>
/// <exception cref="ArgumentException">Thrown when the current number is negative and <paramref name="power"/> is not an integer, as the result is not a real number.</exception>
public SignificantNumber Pow(PreciseNumber power)
{
if (Equal(power, Zero))
Expand All @@ -569,18 +571,29 @@ public SignificantNumber Pow(PreciseNumber power)
}
else if (Equal(this, Zero))
{
return Zero;
return PreciseNumber.IsNegative(power)
? throw new DivideByZeroException("Cannot raise zero to a negative power.")
: Zero;
}
else if (Equal(this, One))
{
return One;
}

bool isNegativeBase = PreciseNumber.IsNegative(Value);
if (isNegativeBase && !PreciseNumber.IsInteger(power))
{
throw new ArgumentException("Cannot raise a negative number to a non-integer power.", nameof(power));
}

int significantDigits = LowestSignificantDigits(this, power);

// Use logarithm and exponential to support decimal powers
// Use logarithm and exponential to support decimal powers. This computes |x|^p, so the
// sign of a negative base is restored for odd integer powers.
double logValue = Math.Log(Math.Abs(Value.To<double>()));
return Math.Exp(logValue * power.To<double>()).ToSignificantNumber(significantDigits);
double magnitude = Math.Exp(logValue * power.To<double>());
double result = isNegativeBase && PreciseNumber.IsOddInteger(power) ? -magnitude : magnitude;
return result.ToSignificantNumber(significantDigits);
}

/// <summary>
Expand Down
Loading