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
20 changes: 20 additions & 0 deletions SignificantNumber.Test/SignificantNumberRangeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,26 @@ public void Exp_ResultBelowDoubleRange_ReturnsNonzeroMagnitude()
Assert.AreEqual(Parse("5.07595889755E-435"), result);
}

[TestMethod]
[DataRow("2E308", "1")]
[DataRow("-1E400", "1")]
[DataRow("1E308", "1")]
[DataRow("-3E400", "1")]
public void Pow_NegativeOneToAnEvenIntegerBeyondDoubleRange_ReturnsOne(string power, string expected) =>
Assert.AreEqual(Parse(expected), SignificantNumber.NegativeOne.Pow(Precise(power)));

[TestMethod]
public void Pow_NegativeOneToAnOddIntegerBeyondDoubleRange_ReturnsNegativeOne()
{
PreciseNumber oddPower = Precise("2" + new string('0', 310) + "1");
Assert.AreEqual(SignificantNumber.NegativeOne, SignificantNumber.NegativeOne.Pow(oddPower));
Assert.AreEqual(SignificantNumber.NegativeOne, SignificantNumber.NegativeOne.Pow(PreciseNumber.Negate(oddPower)));
}

[TestMethod]
public void Pow_NegativeOneToANonIntegerPower_StillThrowsArgumentException() =>
Assert.ThrowsExactly<ArgumentException>(() => SignificantNumber.NegativeOne.Pow(Precise("0.5")));

[TestMethod]
public void Pow_PowerOfTenBeyondExponentRange_ThrowsOverflowException()
{
Expand Down
5 changes: 5 additions & 0 deletions SignificantNumber/SignificantNumber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@
/// <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>
/// <exception cref="OverflowException">Thrown when the result's power of ten is too large or too small to represent.</exception>
public SignificantNumber Pow(PreciseNumber power)

Check warning on line 592 in SignificantNumber/SignificantNumber.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

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

Check warning on line 592 in SignificantNumber/SignificantNumber.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

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

Check warning on line 592 in SignificantNumber/SignificantNumber.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

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

Check warning on line 592 in SignificantNumber/SignificantNumber.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

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

Check warning on line 592 in SignificantNumber/SignificantNumber.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

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

Check failure on line 592 in SignificantNumber/SignificantNumber.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

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

See more on https://sonarcloud.io/project/issues?id=ktsu-dev_SignificantNumber&issues=AaFWTh3wlt7p8zC_rKE7&open=AaFWTh3wlt7p8zC_rKE7&pullRequest=136
{
if (Equal(power, Zero))
{
Expand All @@ -605,6 +605,11 @@
{
return One;
}
else if (Equal(this, NegativeOne) && PreciseNumber.IsInteger(power))
{
// (-1)^n is exact for any integer n, however far beyond the range of a double it is.
return PreciseNumber.IsEvenInteger(power) ? One : NegativeOne;
}

bool isNegativeBase = PreciseNumber.IsNegative(Value);
if (isNegativeBase && !PreciseNumber.IsInteger(power))
Expand Down Expand Up @@ -709,7 +714,7 @@
double magnitude = Math.Abs(exponent);
int lostDigits = !double.IsFinite(magnitude)
? DoubleSignificantDigits
: magnitude > 1 ? (int)Math.Ceiling(Math.Log10(magnitude)) : 0;

Check warning on line 717 in SignificantNumber/SignificantNumber.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Extract this nested ternary operation into an independent statement.

Check warning on line 717 in SignificantNumber/SignificantNumber.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Extract this nested ternary operation into an independent statement.

Check warning on line 717 in SignificantNumber/SignificantNumber.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Extract this nested ternary operation into an independent statement.

Check warning on line 717 in SignificantNumber/SignificantNumber.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Extract this nested ternary operation into an independent statement.

Check warning on line 717 in SignificantNumber/SignificantNumber.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Extract this nested ternary operation into an independent statement.

return int.Max(1, int.Min(significantDigits, DoubleSignificantDigits - lostDigits));
}
Expand Down
Loading