From 78be2105ad6f7518d768eead3a8e94f4c6f76a56 Mon Sep 17 00:00:00 2001 From: Jsoto22 Date: Sat, 6 Apr 2024 19:14:42 -0400 Subject: [PATCH] Added trailing 0s for divide by 0 with precision should close issue #57 --- src/divide.spec.ts | 3 +++ src/divide.ts | 24 +++++++++++++++--------- src/pow.ts | 14 +++++++++++++- 3 files changed, 31 insertions(+), 10 deletions(-) diff --git a/src/divide.spec.ts b/src/divide.spec.ts index 0c7d83e..9416b53 100644 --- a/src/divide.spec.ts +++ b/src/divide.spec.ts @@ -100,4 +100,7 @@ describe("divide", function () { it(".102 / .0383292 = 2.66115651", function () { expect(divide(".102", ".0383292", 8)).toBe("2.66115651"); }); + it("divide(0, 2, 3) should add trailing 0s", function () { + expect(divide(0, 2, 3)).toBe("0.000"); + }); }); diff --git a/src/divide.ts b/src/divide.ts index 9991659..8f85524 100644 --- a/src/divide.ts +++ b/src/divide.ts @@ -1,21 +1,27 @@ import { add, trim } from './add'; import { roundOff } from './round'; -export function divide(dividend, divisor, precission = 8) { - if (divisor == 0) { - throw new Error('Cannot divide by 0'); +export function divide(dividend: string | number, divisor: string | number, precission?:number) { + // Convert to string + if (typeof dividend == 'number' || typeof divisor == 'number') { + dividend = dividend.toString(); + divisor = divisor.toString(); } - dividend = dividend.toString(); - divisor = divisor.toString(); + // Return 0 + if (divisor == '0') { + return '0' + (!precission)? '': '.' + new Array(precission).join('0'); + } + + // Set default precission + if(typeof precission == 'undefined'){ + precission = 8; + } // remove trailing zeros in decimal ISSUE#18 dividend = dividend.replace(/(\.\d*?[1-9])0+$/g, "$1").replace(/\.0+$/, ""); divisor = divisor.replace(/(\.\d*?[1-9])0+$/g, "$1").replace(/\.0+$/, ""); - if (dividend == 0) - return '0'; - let neg = 0; if (divisor[0] == '-') { divisor = divisor.substring(1); @@ -51,7 +57,7 @@ export function divide(dividend, divisor, precission = 8) { let prec = 0, dl = divisor.length, rem = '0', quotent = ''; let dvnd = (dividend.indexOf('.') > -1 && dividend.indexOf('.') < dl) ? dividend.substring(0, dl + 1) : dividend.substring(0, dl); dividend = (dividend.indexOf('.') > -1 && dividend.indexOf('.') < dl) ? dividend.substring(dl + 1) : dividend.substring(dl); - + if (dvnd.indexOf('.') > -1) { let shift = dvnd.length - dvnd.indexOf('.') - 1; dvnd = dvnd.replace('.', ''); diff --git a/src/pow.ts b/src/pow.ts index f3be939..70a6652 100644 --- a/src/pow.ts +++ b/src/pow.ts @@ -1,5 +1,5 @@ import { abs } from "./abs"; -import { compareTo, greaterThan, isNotZero, isOne, lessThan } from "./compareTo"; +import { compareTo, greaterThan, isNotZero, isOne, isZero, lessThan } from "./compareTo"; import { divide } from "./divide"; import { modulus } from "./modulus"; import { multiply } from "./multiply"; @@ -55,6 +55,18 @@ export function pow(base: number | string, exponent: number | string, percision: exponent = exponent.toString(); base = base.toString(); + if(isZero(exponent)){ + return '1' + } + + if(!exponent.includes('-') && isOne(exponent)){ + return base + } + + if(isZero(base) && exponent.includes('-') && isOne(exponent)){ + throw Error('0^(-1) is undefined'); + } + const remainder = abs(modulus(exponent)); const reciprical = exponent.includes('-'); const negativeBase = base.includes('-');