mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-11-14 06:52:59 +08:00
Compare commits
17 Commits
17f5219d95
...
5aa20e0b3b
Author | SHA1 | Date | |
---|---|---|---|
|
5aa20e0b3b | ||
|
310343e0e1 | ||
|
2c221989b1 | ||
|
604e9cdd4e | ||
|
43b806f89c | ||
|
f7bb28a0fd | ||
|
b3da0ab5ef | ||
|
5daae15052 | ||
|
5ed34c7d05 | ||
|
506aefbc78 | ||
|
3b1fcd8333 | ||
|
a64c587080 | ||
|
0cb77d0454 | ||
|
c82ceabee8 | ||
|
e076e12dee | ||
|
aad101181c | ||
|
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);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -14,6 +14,12 @@ export default function fastPowering(base, power) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (power < 0) {
|
||||||
|
const powerNext = power * -1;
|
||||||
|
const baseNext = 1 / base;
|
||||||
|
return fastPowering(baseNext, powerNext);
|
||||||
|
}
|
||||||
|
|
||||||
if (power % 2 === 0) {
|
if (power % 2 === 0) {
|
||||||
// If the power is even...
|
// If the power is even...
|
||||||
// we may recursively redefine the result via twice smaller powers:
|
// we may recursively redefine the result via twice smaller powers:
|
||||||
|
Loading…
Reference in New Issue
Block a user