From 4eb570341d539936651f7c70c798ac8ff3b9f73e Mon Sep 17 00:00:00 2001 From: nikikalwar Date: Tue, 15 Dec 2020 07:12:46 +0530 Subject: [PATCH] added the condition to handle negative integers --- .../math/fast-powering/__test__/fastPowering.test.js | 3 +++ src/algorithms/math/fast-powering/fastPowering.js | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/src/algorithms/math/fast-powering/__test__/fastPowering.test.js b/src/algorithms/math/fast-powering/__test__/fastPowering.test.js index 0a5da756..ae8683df 100644 --- a/src/algorithms/math/fast-powering/__test__/fastPowering.test.js +++ b/src/algorithms/math/fast-powering/__test__/fastPowering.test.js @@ -19,5 +19,8 @@ describe('fastPowering', () => { expect(fastPowering(16, 16)).toBe(18446744073709552000); expect(fastPowering(7, 21)).toBe(558545864083284000); 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); }); }); diff --git a/src/algorithms/math/fast-powering/fastPowering.js b/src/algorithms/math/fast-powering/fastPowering.js index 4f4a6b35..66036799 100644 --- a/src/algorithms/math/fast-powering/fastPowering.js +++ b/src/algorithms/math/fast-powering/fastPowering.js @@ -13,6 +13,13 @@ export default function fastPowering(base, power) { // Anything that is raised to the power of zero is 1. return 1; } + + if(power<0){ + power=power*-1; + base=1/base; + // console.log(base); + fastPowering(base,power); + } if (power % 2 === 0) { // If the power is even...