mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-09-20 07:43:04 +08:00
added the condition to handle negative integers
This commit is contained in:
parent
1b0e27ab86
commit
4eb570341d
@ -19,5 +19,8 @@ describe('fastPowering', () => {
|
|||||||
expect(fastPowering(16, 16)).toBe(18446744073709552000);
|
expect(fastPowering(16, 16)).toBe(18446744073709552000);
|
||||||
expect(fastPowering(7, 21)).toBe(558545864083284000);
|
expect(fastPowering(7, 21)).toBe(558545864083284000);
|
||||||
expect(fastPowering(100, 9)).toBe(1000000000000000000);
|
expect(fastPowering(100, 9)).toBe(1000000000000000000);
|
||||||
|
expect(fastPowering(5, -5)).toBe(0.0003200000000000002);
|
||||||
|
expect(fastPowering(-5, 5)).toBe(-3125);
|
||||||
|
expect(fastPowering(-5, -5)).toBe(-0.0003200000000000002);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -13,6 +13,13 @@ export default function fastPowering(base, power) {
|
|||||||
// Anything that is raised to the power of zero is 1.
|
// Anything that is raised to the power of zero is 1.
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(power<0){
|
||||||
|
power=power*-1;
|
||||||
|
base=1/base;
|
||||||
|
// console.log(base);
|
||||||
|
fastPowering(base,power);
|
||||||
|
}
|
||||||
|
|
||||||
if (power % 2 === 0) {
|
if (power % 2 === 0) {
|
||||||
// If the power is even...
|
// If the power is even...
|
||||||
|
Loading…
Reference in New Issue
Block a user