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
15 changes: 15 additions & 0 deletions PreciseNumber.Test/PreciseNumberExponentialTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,21 @@ public void TestPowRejectsAFractionalPowerOfANegativeValue()
Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => Parse("-2").Pow(Parse("0.5")));
}

[TestMethod]
public void TestPowOfZeroToANegativePowerThrowsLikeDivision()
{
// Zero to a negative power is one over zero, which One / Zero and RootN(0, -n) already reject.
Assert.ThrowsExactly<DivideByZeroException>(() => PreciseNumber.Zero.Pow(PreciseNumber.NegativeOne));
Assert.ThrowsExactly<DivideByZeroException>(() => PreciseNumber.Zero.Pow(Parse("-0.5")));
Assert.ThrowsExactly<DivideByZeroException>(() => PreciseNumber.Pow(PreciseNumber.Zero, Parse("-2")));
Assert.ThrowsExactly<DivideByZeroException>(() => PreciseNumber.Pow(PreciseNumber.Zero, Parse("-0.5")));

// A positive power of zero is still zero, and the zero power of anything is still one.
Assert.AreEqual(PreciseNumber.Zero, PreciseNumber.Pow(PreciseNumber.Zero, 2.ToPreciseNumber()));
Assert.AreEqual(PreciseNumber.Zero, PreciseNumber.Pow(PreciseNumber.Zero, Parse("0.5")));
Assert.AreEqual(PreciseNumber.One, PreciseNumber.Pow(PreciseNumber.Zero, PreciseNumber.Zero));
}

[TestMethod]
public void TestExpM1KeepsTheDigitsOfASmallArgument()
{
Expand Down
1 change: 1 addition & 0 deletions PreciseNumber/PreciseNumber.Exponentials.cs
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,7 @@ public static PreciseNumber Exp10M1(PreciseNumber x, int significantDigits)
/// <param name="y">The exponent.</param>
/// <returns><c>x^y</c>.</returns>
/// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="x"/> is negative and <paramref name="y"/> is not an integer.</exception>
/// <exception cref="DivideByZeroException">Thrown when <paramref name="x"/> is zero and <paramref name="y"/> is negative.</exception>
/// <exception cref="OverflowException">Thrown when the result needs an exponent outside the range of an <see cref="int"/>.</exception>
/// <remarks>
/// An integer exponent is exact, by repeated squaring. Anything else is
Expand Down
14 changes: 13 additions & 1 deletion PreciseNumber/PreciseNumber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,7 @@
/// A fixed precision such as <c>E15</c> rounds values that need 17 digits, which turns
/// <see cref="double.MaxValue"/> into a number that converts back to infinity.
/// </remarks>
internal static string GetStringFormatForFloatType<TFloat>()

Check warning on line 700 in PreciseNumber/PreciseNumber.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Remove this method and declare a constant for this value.

Check warning on line 700 in PreciseNumber/PreciseNumber.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Remove this method and declare a constant for this value.

Check warning on line 700 in PreciseNumber/PreciseNumber.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Remove this method and declare a constant for this value.

Check warning on line 700 in PreciseNumber/PreciseNumber.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Remove this method and declare a constant for this value.

Check warning on line 700 in PreciseNumber/PreciseNumber.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Remove this method and declare a constant for this value.

Check warning on line 700 in PreciseNumber/PreciseNumber.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Remove this method and declare a constant for this value.
where TFloat : INumber<TFloat>
=> "R";

Expand Down Expand Up @@ -1111,7 +1111,7 @@
public static PreciseNumber MinMagnitudeNumber(PreciseNumber x, PreciseNumber y) => MinMagnitude(x, y);

/// <inheritdoc/>
public static PreciseNumber Parse(ReadOnlySpan<char> s, NumberStyles style, IFormatProvider? provider)

Check warning on line 1114 in PreciseNumber/PreciseNumber.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Refactor this method to reduce its Cognitive Complexity from 23 to the 15 allowed.

Check warning on line 1114 in PreciseNumber/PreciseNumber.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Refactor this method to reduce its Cognitive Complexity from 23 to the 15 allowed.

Check warning on line 1114 in PreciseNumber/PreciseNumber.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Refactor this method to reduce its Cognitive Complexity from 23 to the 15 allowed.

Check warning on line 1114 in PreciseNumber/PreciseNumber.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Refactor this method to reduce its Cognitive Complexity from 23 to the 15 allowed.
{
if (s.IsEmpty)
{
Expand Down Expand Up @@ -1740,6 +1740,7 @@
/// <param name="power">The power to raise the number to.</param>
/// <returns>A new instance of <see cref="PreciseNumber"/> that is the result of raising the current instance to the specified power.</returns>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the current instance is negative and <paramref name="power"/> is not an integer.</exception>
/// <exception cref="DivideByZeroException">Thrown when the current instance is zero and <paramref name="power"/> is negative.</exception>
/// <exception cref="OverflowException">Thrown when the result needs an exponent outside the range of an <see cref="int"/>.</exception>
/// <remarks>
/// An integer power is exact, by repeated squaring. A fractional power is
Expand All @@ -1754,7 +1755,7 @@
}
else if (Significand.IsZero)
{
return Zero;
return PowOfZero(power);
}
else if (IsUnit)
{
Expand Down Expand Up @@ -1797,6 +1798,17 @@
return FractionalPow(this, power, significantDigits);
}

/// <summary>
/// Returns zero raised to a non-zero power.
/// </summary>
/// <param name="power">The non-zero power.</param>
/// <returns>Zero, for a positive <paramref name="power"/>.</returns>
/// <exception cref="DivideByZeroException">Thrown when <paramref name="power"/> is negative, since that is one over zero, as for <c>One / Zero</c>.</exception>
private static PreciseNumber PowOfZero(PreciseNumber power) =>
power.Significand.Sign < 0
? throw new DivideByZeroException()
: Zero;

/// <inheritdoc/>
public static PreciseNumber operator -(PreciseNumber value) =>
Negate(value);
Expand Down
Loading