Skip to content

Commit

Permalink
Added trailing 0s for divide by 0 with precision
Browse files Browse the repository at this point in the history
should close issue #57
  • Loading branch information
Jsoto22 committed Apr 6, 2024
1 parent d1a6506 commit 78be210
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
3 changes: 3 additions & 0 deletions src/divide.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
});
24 changes: 15 additions & 9 deletions src/divide.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -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('.', '');
Expand Down
14 changes: 13 additions & 1 deletion src/pow.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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('-');
Expand Down

0 comments on commit 78be210

Please sign in to comment.