Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d25ded2

Browse files
committedApr 4, 2021
Added condition for negative power values
1 parent 8335707 commit d25ded2

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed
 

‎src/algorithms/math/fast-powering/fastPowering.js

+7
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ export default function fastPowering(base, power) {
1414
return 1;
1515
}
1616

17+
if (power < 0) {
18+
//if power is negative, then powerNext must be inverted
19+
const powerNext = power * -1;
20+
const baseNext = 1 / base;
21+
return fastPowering(baseNext, powerNext);
22+
}
23+
1724
if (power % 2 === 0) {
1825
// If the power is even...
1926
// we may recursively redefine the result via twice smaller powers:

0 commit comments

Comments
 (0)
Please sign in to comment.