Skip to content

Commit

Permalink
Minor patches and testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Jsoto22 committed Apr 8, 2024
1 parent 8937880 commit 5830249
Show file tree
Hide file tree
Showing 7 changed files with 326 additions and 96 deletions.
43 changes: 42 additions & 1 deletion src/compareTo.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { compareTo } from "./compareTo";
import { compareTo, equals, greaterThan, lessThan } from "./compareTo";

describe("compareTo", function () {
it("should be defined", function () {
Expand Down Expand Up @@ -66,4 +66,45 @@ describe("compareTo", function () {
it("should: -0, 0 = 0", function () {
expect(compareTo("-0", "0")).toBe(0);
});

describe('Wrapper functions', function () {
describe('lessThan', function () {
it("should: -1 < 0 is true", function () {
expect(lessThan("-1", "0")).toBeTruthy()
});
it("should: 0 < 0 is false", function () {
expect(lessThan("0", "0")).toBeFalsy()
});
it("should: -1 =< 0 is true", function () {
expect(lessThan("-1", "0", true)).toBeTruthy()
});
it("should: 0 =< 0 is true", function () {
expect(lessThan("0", "0", true)).toBeTruthy()
});
});
describe('greaterThan', function () {
it("should: 1 > 0 is true", function () {
expect(greaterThan("1", "0")).toBeTruthy()
});
it("should: 0 < 0 is false", function () {
expect(greaterThan("0", "0")).toBeFalsy()
});
it("should: -1 >= 0 is true", function () {
expect(greaterThan("1", "0", true)).toBeTruthy()
});
it("should: 0 >= 0 is true", function () {
expect(greaterThan("0", "0", true)).toBeTruthy()
});
});
describe('equals', function () {
it("should: 1 = 0 is false", function () {
expect(equals("1", "0")).toBeFalsy()
});
it("should: 0 = 0 is true", function () {
expect(equals("0", "0")).toBeTruthy()
});
});
});
});


16 changes: 4 additions & 12 deletions src/compareTo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,10 @@ export function equals(left: string, right: string){
return (compareTo(stripTrailingZero(left), stripTrailingZero(right)) === 0)
}

export function isZero(number: string) {
return (compareTo(stripTrailingZero(abs(number)), '0') === 0)
export function isExatclyZero(number: string) {
return equals(stripTrailingZero(abs(number)), '0')
}

export function isOne(number: string) {
return (compareTo(stripTrailingZero(abs(number)), '1') === 0)
}

export function isNotZero(number: string) {
return !isZero(number)
}

export function isNotOne(number: string) {
return !isOne(number)
export function isExatclyOne(number: string) {
return equals(stripTrailingZero(abs(number)), '1')
}
139 changes: 88 additions & 51 deletions src/pow.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { pow, nthRoot } from "./pow";
import { pow, nthRoot, sqRoot, cbRoot, root4, root5, root10, exp } from "./pow";

describe("Pow", function () {

Expand Down Expand Up @@ -33,64 +33,64 @@ describe("Pow", function () {
});
})

describe('handle fractional base', function() {
describe('handle fractional base', function () {

it("should: 2.5^2 = 6.25", function(){
it("should: 2.5^2 = 6.25", function () {
expect(pow(2.5, 2)).toBe("6.25");
});

it("should: -2.5^2 = 6.25", function(){
it("should: -2.5^2 = 6.25", function () {
expect(pow(-2.5, 2)).toBe("6.25");
});

it("should: -2.5^3 = 6.25", function(){
it("should: -2.5^3 = 6.25", function () {
expect(pow(-2.5, 3)).toBe("-15.625");
});
})

describe('should handle fractional exponent', function() {
describe('should handle fractional exponent', function () {

it("should: 2^2.5 = 5.65685424949238019520675489683879", function(){
it("should: 2^2.5 = 5.65685424949238019520675489683879", function () {
expect(pow(2, 2.5, 32)).toBe("5.65685424949238019520675489683879");
});

it("should: 2^.5 = 1.4142135623730950488016887242097", function(){
it("should: 2^.5 = 1.4142135623730950488016887242097", function () {
expect(pow(2, .5, 32)).toBe("1.4142135623730950488016887242097");
});

it("should: 2.5^2.2 = 7.50702771238394520776312433499478", function(){
it("should: 2.5^2.2 = 7.50702771238394520776312433499478", function () {
expect(pow(2.5, 2.2, 32)).toBe("7.50702771238394520776312433499478");
});

it("should: 2.5^-2.2 = 0.13320851318429970246653555493722", function(){
it("should: 2.5^-2.2 = 0.13320851318429970246653555493722", function () {
expect(pow(2.5, -2.2, 32)).toBe("0.13320851318429970246653555493722");
});

it("should: -2.5^2.2 = -7.50702771238394520776312433499478", function(){
it("should: -2.5^2.2 = -7.50702771238394520776312433499478", function () {
expect(pow(-2.5, 2.2, 32)).toBe("-7.50702771238394520776312433499478");
});

it("should: -2.5^-2.2 = -0.13320851318429970246653555493722", function(){
it("should: -2.5^-2.2 = -0.13320851318429970246653555493722", function () {
expect(pow(-2.5, -2.2, 32)).toBe("-0.13320851318429970246653555493722");
});

})

describe('should handle powers of 10', function() {
describe('should handle powers of 10', function () {

it("should: 10^2 = 100", function(){
it("should: 10^2 = 100", function () {
expect(pow(10, 2, 32, undefined)).toBe("100");
});

it("should: 10^-2 = .01", function(){
it("should: 10^-2 = .01", function () {
expect(pow(10, -2, 32, undefined)).toBe("0.01");
});

it("should: 10^.2 = 1.58489319246111348520210137339151", function(){
it("should: 10^.2 = 1.58489319246111348520210137339151", function () {
expect(pow(10, .2, 32, undefined)).toBe("1.58489319246111348520210137339151");
});

it("should: 10^-.2 = 0.63095734448019324943436013662234", function(){
it("should: 10^-.2 = 0.63095734448019324943436013662234", function () {
expect(pow(10, -.2, 32, undefined)).toBe("0.63095734448019324943436013662234");
});

Expand Down Expand Up @@ -135,40 +135,77 @@ describe("Pow", function () {
});
})

describe('Roots', function(){

describe('nthRoot', function(){

it("should: 4root2 = 2", function () {
expect(nthRoot(4, 2)).toBe("2");
});

it("should: 9root2 = 9", function () {
expect(nthRoot(81, 2)).toBe("9");
});

it("should: 452root2 = 21.260291625", function () {
expect(nthRoot(452, 2)).toBe("21.260291625");
});

it("should: 452root2 = 21.260...", function () {
expect(nthRoot(452, 2, 32)).toBe("21.260291625469298815998243829858628");
});

it("should: 45.76root3 = 3.576805615", function () {
expect(nthRoot(45.76, 3)).toBe("3.576805615");
});

it("should: 45.76root3 = 3.576...", function () {
expect(nthRoot(45.76, 3, 32)).toBe("3.576805614696509204554520668745324");
});

})

describe('Errors and Exceptions', function () {
it("should throw error", function () {
expect(() => nthRoot(4, 3.5)).toThrowError();
});
});

describe('Roots', function () {

describe('nthRoot', function () {

it("should: 4root2 = 2", function () {
expect(nthRoot(4, 2)).toBe("2");
});

it("should: 9root2 = 9", function () {
expect(nthRoot(81, 2)).toBe("9");
});

it("should: 452root2 = 21.260291625", function () {
expect(nthRoot(452, 2)).toBe("21.260291625");
});

it("should: 452root2 = 21.260...", function () {
expect(nthRoot(452, 2, 32)).toBe("21.260291625469298815998243829858628");
});

it("should: 45.76root3 = 3.576805615", function () {
expect(nthRoot(45.76, 3)).toBe("3.576805615");
});

it("should: 45.76root3 = 3.576...", function () {
expect(nthRoot(45.76, 3, 32)).toBe("3.576805614696509204554520668745324");
});

});

describe('sqRoot', function () {
it("sqRoot(4) = '2'", function () {
expect(sqRoot(4)).toBe('2');
});
});

describe('cbRoot', function () {
it("cbRoot(27) = '3'", function () {
expect(cbRoot(27)).toBe('3');
});
});

describe('root4', function () {
it("root4(16) = '2'", function () {
expect(root4(16)).toBe('2');
});
});

describe('root5', function () {
it("root5(243) = '3'", function () {
expect(root5(243)).toBe('3');
});
});

describe('root10', function () {
it("root10(1024) = '2'", function () {
expect(root10(1024)).toBe('2');
});
});

describe('Errors and Exceptions', function () {
it("should throw error", function () {
expect(() => nthRoot(4, 3.5)).toThrowError();
});
});
});

describe('Exponentials', function () {
it("exp(4) = '54.59815003314423907811026120286067772760909039824031401964928069315468271580552520804729940109651850639200230030607285363846251536'", function () {
expect(exp(4)).toBe('54.59815003314423907811026120286067772760909039824031401964928069315468271580552520804729940109651850639200230030607285363846251536');
});
});
14 changes: 7 additions & 7 deletions 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, isZero, lessThan } from "./compareTo";
import { compareTo, greaterThan, isExatclyOne, isExatclyZero, lessThan } from "./compareTo";
import { divide } from "./divide";
import { modulus } from "./modulus";
import { multiply } from "./multiply";
Expand Down Expand Up @@ -55,15 +55,15 @@ export function pow(base: number | string, exponent: number | string, percision:
exponent = exponent.toString();
base = base.toString();

if(isZero(exponent)){
if(isExatclyZero(exponent)){
return '1'
}

if(!exponent.includes('-') && isOne(exponent)){
if(!exponent.includes('-') && isExatclyOne(exponent)){
return base
}

if(isZero(base) && exponent.includes('-') && isOne(exponent)){
if(isExatclyZero(base) && exponent.includes('-') && isExatclyOne(exponent)){
throw Error('0^(-1) is undefined');
}

Expand All @@ -83,7 +83,7 @@ export function pow(base: number | string, exponent: number | string, percision:
negate = !negate;
}

if (isNotZero(remainder)) {
if (!isExatclyZero(remainder)) {

if(negativeBase && !negativeBase10){
negate = !negate
Expand All @@ -108,7 +108,7 @@ export function pow(base: number | string, exponent: number | string, percision:
exponent = abs(exponent)

while (greaterThan(exponent, '0')) {
if (isOne(modulus(exponent, 2))) { result = multiply(result, base) }
if (isExatclyOne(modulus(exponent, 2))) { result = multiply(result, base) }
base = multiply(base, base);
exponent = roundOff(divide(exponent, 2), 0, RoundingModes.FLOOR);
}
Expand Down Expand Up @@ -159,7 +159,7 @@ export function cbRoot(base: string|number, percision = 32) {

export function root4(base: string|number, percision = 32) {
percision = Math.max(percision, 32);
return nthRoot(base, 4, percision);
return sqRoot(sqRoot(base, percision), percision);
}

export function root5(base: string|number, percision = 32) {
Expand Down
52 changes: 52 additions & 0 deletions src/round.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,58 @@ describe("round", function () {
});
});

describe("test rounding mode UNNECESSARY", function () {
it("should round(5.5, 1, UNNECESSARY) = 5.5", function () {
expect(roundOff("5.5", 1, RoundingModes.UNNECESSARY)).toBe("5.5");
});
it("should round(6.5, 1, UNNECESSARY) = 6.5", function () {
expect(roundOff("6.5", 1, RoundingModes.UNNECESSARY)).toBe("6.5");
});
it("should round(2.6, 1, UNNECESSARY) = 2.6", function () {
expect(roundOff("2.6", 1, RoundingModes.UNNECESSARY)).toBe("2.6");
});
it("should round(1.15, 2, UNNECESSARY) = 1.15", function () {
expect(roundOff("1.15", 2, RoundingModes.UNNECESSARY)).toBe("1.15");
});
it("should round(1.15, 3, UNNECESSARY) = 1.15", function () {
expect(roundOff("1.15", 3, RoundingModes.UNNECESSARY)).toBe("1.15");
});
it("should round(23000.00, -3, UNNECESSARY) = 23000.00", function () {
expect(roundOff("23000.00", -3, RoundingModes.UNNECESSARY)).toBe("23000.00");
});
it("should round(23000.00, -2, UNNECESSARY) = 23000.00", function () {
expect(roundOff("23000.00", -2, RoundingModes.UNNECESSARY)).toBe("23000.00");
});
it("should round(23000.00, 0, UNNECESSARY) = 23000.00", function () {
expect(roundOff("23000.00", 0, RoundingModes.UNNECESSARY)).toBe("23000.00");
});
it("should round(745, 0, UNNECESSARY) = 745", function () {
expect(roundOff("745", 0, RoundingModes.UNNECESSARY)).toBe("745");
});

describe('Errors and Exceptions', function () {
it("round(0.3487, 2, UNNECESSARY) should throw error", function () {
expect(() => roundOff("0.3487", 2, RoundingModes.UNNECESSARY)).toThrowError();
});
it("round(245.53158, 3, UNNECESSARY) should throw error", function () {
expect(() => roundOff("245.53158", 3, RoundingModes.UNNECESSARY)).toThrowError();
});
it("round(3654, -2, UNNECESSARY) should throw error", function () {
expect(() => roundOff("3654", -2, RoundingModes.UNNECESSARY)).toThrowError();
});
it("round(3600.74, -2, UNNECESSARY) should throw error", function () {
expect(() => roundOff("3600.74", -2, RoundingModes.UNNECESSARY)).toThrowError();
});
it("round(23000.01, 0, UNNECESSARY) should throw error", function () {
expect(() => roundOff("23000.01", 0, RoundingModes.UNNECESSARY)).toThrowError();
});
it("round(745, 0, UNNECESSARY) should throw error", function () {
expect(() => roundOff("745.1", 0, RoundingModes.UNNECESSARY)).toThrowError();
});
});

});

describe("test rounding mode HALF_EVEN - default", function () {
it("should round(5.5, 0) = 6", function () {
expect(roundOff("5.5", 0)).toBe("6");
Expand Down
Loading

0 comments on commit 5830249

Please sign in to comment.