Skip to content

fix(math): handle negative powers in fast powering algorithm - #2179

Open
fauzan171 wants to merge 1 commit into
trekhleb:masterfrom
fauzan171:fix/fast-powering-negative-power
Open

fauzan171 wants to merge 1 commit into
trekhleb:masterfrom
fauzan171:fix/fast-powering-negative-power

Conversation

@fauzan171

Copy link
Copy Markdown
Contributor

Problem

The fastPowering function enters infinite recursion when called with a negative power value.

fastPowering(2, -1); // RangeError: Maximum call stack size exceeded
fastPowering(2, -2); // RangeError: Maximum call stack size exceeded

This happens because negative powers never hit the base case (power === 0) due to the floor division logic — Math.floor(-1 / 2) is -1, not 0.

Fix

Added handling for negative powers by computing the reciprocal before recursing:

x^(-n) = 1 / x^n

This converts the negative power to a positive one and computes the reciprocal.

Test Coverage

Added test cases for negative powers:

  • fastPowering(2, -1) → 0.5
  • fastPowering(2, -2) → 0.25
  • fastPowering(2, -3) → 0.125
  • fastPowering(4, -1) → 0.25
  • fastPowering(5, -2) → 0.04
  • fastPowering(10, -1) → 0.1

All existing tests continue to pass.

The fastPowering function previously entered infinite recursion when
called with a negative power value, since negative powers never hit
the base case (power === 0) due to the floor division logic.

Added handling for negative powers by computing the reciprocal:
x^(-n) = 1 / x^n, then recursively computing x^n with the positive
exponent.

Added test cases for negative powers.

@daltino daltino left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR addresses a clear bug by adding support for negative powers in the fast powering algorithm. The implementation is concise, follows the existing coding style, and is well-tested with sufficient test cases covering edge cases like different bases and negative exponents. All tests pass, and no unnecessary changes were introduced.

moshejs added a commit to moshejs/javascript-algorithms that referenced this pull request Oct 25, 2026
Remove the changes that duplicate open PRs opened before this one, to
avoid double review and merge conflicts:

- fastPowering negative exponents (trekhleb#2179, trekhleb#591, trekhleb#682)
- LinkedList.insert tail update (trekhleb#2106, trekhleb#2153)
- MinHeapAdhoc/MaxHeapAdhoc constructor `this` binding (trekhleb#1124)
- stronglyConnectedComponents leaving the graph reversed (trekhleb#2153)
- BST README "prent" typo (trekhleb#890)
- Extended Euclidean algorithm addition (trekhleb#200)

The other README corrections in the touched files are kept. The
BinaryTreeNode.replaceChild and BST remove fixes (also in trekhleb#2118 and
trekhleb#2103) stay, since the AVL remove fix and its invariant test need them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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