Skip to content

fix(maths): handle negative exponents and prevent stack overflow in PowerUsingRecursion - #7768

Open
spidervirus wants to merge 1 commit into
TheAlgorithms:masterfrom
spidervirus:fix/power-using-recursion
Open

spidervirus wants to merge 1 commit into
TheAlgorithms:masterfrom
spidervirus:fix/power-using-recursion

Conversation

@spidervirus

Copy link
Copy Markdown
Contributor
  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized it.
  • All filenames are in PascalCase.
  • All functions and variable names follow Java naming conventions.
  • All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
  • All new algorithms include a corresponding test class that validates their functionality.
  • All new code is formatted with clang-format -i --style=file path/to/your/file.java

Description

In com.thealgorithms.maths.PowerUsingRecursion.power(double base, int exponent):

  1. Calling with a negative exponent (e.g., power(2.0, -3)) resulted in an infinite recursive loop causing StackOverflowError, because it decremented exponent - 1 indefinitely.
  2. Linear recursion caused StackOverflowError on large exponents.
  3. Zero base with negative exponent was not guarded against division by zero.

Fix

  • Implemented mathematical negative exponent handling: base^(-e) = 1 / base^e.
  • Added check throwing ArithmeticException when base == 0.0 with negative exponent.
  • Handled Integer.MIN_VALUE safely without integer negation overflow.
  • Added divide-and-conquer recursion (O(log n) stack depth) for even exponents to avoid stack exhaustion.
  • Added comprehensive JUnit 5 parameterized tests covering negative exponents, boundary values, zero base, and large exponents.

@codecov-commenter

codecov-commenter commented Nov 19, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.44%. Comparing base (e79b0f1) to head (9b53036).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #7768      +/-   ##
============================================
+ Coverage     82.42%   82.44%   +0.01%     
- Complexity     8508     8513       +5     
============================================
  Files           847      847              
  Lines         26289    26300      +11     
  Branches       5133     5138       +5     
============================================
+ Hits          21670    21684      +14     
+ Misses         3828     3825       -3     
  Partials        791      791              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@spidervirus
spidervirus force-pushed the fix/power-using-recursion branch from 1d529b9 to 9b53036 Compare November 19, 2026 11:54
@spidervirus
spidervirus marked this pull request as ready for review November 19, 2026 11:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants