Skip to content

Commit 610d7b0

Browse files
committed
Move zero-to-a-power into its own helper to keep Pow's complexity down [patch]
SonarCloud S3776 counted Pow at a cognitive complexity of 17 against a limit of 15 once the zero-base branch gained its sign check. The branch now calls PowOfZero, which holds that check. Behaviour is unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QK96a24CjVhUK9u1Ss2YNN
1 parent f0720fe commit 610d7b0

1 file changed

Lines changed: 12 additions & 4 deletions

File tree

‎PreciseNumber/PreciseNumber.cs‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1755,10 +1755,7 @@ public PreciseNumber Pow(PreciseNumber power)
17551755
}
17561756
else if (Significand.IsZero)
17571757
{
1758-
// Zero to a negative power is one over zero, which has no value here, as for One / Zero.
1759-
return power.Significand.Sign < 0
1760-
? throw new DivideByZeroException()
1761-
: Zero;
1758+
return PowOfZero(power);
17621759
}
17631760
else if (IsUnit)
17641761
{
@@ -1801,6 +1798,17 @@ public PreciseNumber Pow(PreciseNumber power)
18011798
return FractionalPow(this, power, significantDigits);
18021799
}
18031800

1801+
/// <summary>
1802+
/// Returns zero raised to a non-zero power.
1803+
/// </summary>
1804+
/// <param name="power">The non-zero power.</param>
1805+
/// <returns>Zero, for a positive <paramref name="power"/>.</returns>
1806+
/// <exception cref="DivideByZeroException">Thrown when <paramref name="power"/> is negative, since that is one over zero, as for <c>One / Zero</c>.</exception>
1807+
private static PreciseNumber PowOfZero(PreciseNumber power) =>
1808+
power.Significand.Sign < 0
1809+
? throw new DivideByZeroException()
1810+
: Zero;
1811+
18041812
/// <inheritdoc/>
18051813
public static PreciseNumber operator -(PreciseNumber value) =>
18061814
Negate(value);

0 commit comments

Comments
 (0)