Skip to content

Commit c51539a

Browse files
committed
New version after a user requested a change.
1 parent 309d9e4 commit c51539a

File tree

6 files changed

+30
-33
lines changed

6 files changed

+30
-33
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
A quick implementation of the Bisection Method in TypeScript.
33
To install run the below command:
44
```
5-
npm install @flyn_nick/bisection-method@1.0.3
5+
npm install @flyn_nick/bisection-method@1.0.4
66
```

dist/bisection.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
export declare function bisection(f: ((x: number) => number) | string, min: number, max: number, maxIter?: number, annoyingConsoleLogs?: boolean): number | [number, number] | 'ERROR';
1+
export declare function bisection(f: ((x: number) => number) | string, min: number, max: number, maxIter?: number, annoyingConsoleLogs?: boolean, epsilon?: number): number | [number, number] | 'ERROR';
22
export declare function testBisectionFunction(): void;

dist/bisection.js

Lines changed: 10 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/bisection.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
{
2-
"name": "@flyn-nick/bisection-method",
2+
"name": "@flyn_nick/bisection-method",
33
"description": "A quick and inefficient implementation of the bisection method in TS.",
4-
"version": "1.0.3",
4+
"version": "1.0.4",
55
"main": "./dist/index.js",
66
"types": "dist/index.d.ts",
77
"scripts": { "test": "echo \"ERROR: Sorry, no test included 😢.\" && exit 0" },
8-
"repository": {
9-
"type": "git",
10-
"url": "https://github.com/FlyN-Nick/Bisection-Method-TypeScript.git"
11-
},
128
"keywords": [ "bisection", "bisection method" ],
139
"author": "Nicholas Assaderaghi <nickassader@gmail.com> (https://github.com/FlyN-Nick)",
1410
"license": "MIT",
1511
"bugs": { "url": "https://github.com/FlyN-Nick/Bisection-Method-TypeScript/issues" },
16-
"homepage": "https://github.com/FlyN-Nick/Bisection-Method-TypeScript",
17-
"publishConfig": { "registry": "https://npm.pkg.github.com/" }
12+
"homepage": "https://github.com/FlyN-Nick/Bisection-Method-TypeScript"
1813
}

src/bisection.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,42 +16,43 @@ function convert(f: ((x: number) => number) | string, x: number): number
1616
* Things that may cause this to be unsuccessfull/error:
1717
* #1: Root is not within given range.
1818
* #2: Function changes signs more than once within given range.
19-
* #3: The given minimum is greater than the given maximum.
20-
* #4: There are x within given range where f(x) is undefined.
19+
* #3: There are x within given range where f(x) is undefined.
2120
*
2221
* @param {(x: number) => any} f - function you want to find the root of.
2322
* @param {number} min - min of given range.
2423
* @param {number} max - max of given range.
2524
* @param {bool} annoyingConsoleLogs - (optional) really annoying console log ever iteration.
26-
* @param {number} maxIter - (optional) max number of iterations
25+
* @param {number} maxIter - (optional) max number of iterations.
26+
* @param {number} epsilon - (optional) tolerance for finding root.
2727
*/
28-
export function bisection(f: ((x: number) => number) | string, min: number, max: number, maxIter=100, annoyingConsoleLogs=false): number | [number, number] | 'ERROR'
28+
export function bisection(f: ((x: number) => number) | string, min: number, max: number, maxIter=100, annoyingConsoleLogs=false, epsilon=0): number | [number, number] | 'ERROR'
2929
{
3030
console.log("---------------------------------------------------------------");
3131

3232
if (min > max)
3333
{
34-
console.log("You seem to have mixed the min and max...");
35-
return 'ERROR';
34+
let tmp = max;
35+
max = min
36+
min = tmp;
3637
}
37-
else if (min == max && convert(f, min) == 0)
38+
if (min == max && convert(f, min) == 0)
3839
{
39-
console.log("Wow, the given min and max were the same and were the root. Kinda seems like this was on purpose...");
40+
console.log("Min=max=root.");
4041
return min;
4142
}
4243
else if (min == max)
4344
{
44-
console.log("Wow, the given min and max were the same but were not the root. Kinda seems like this was on purpose...");
45+
console.log("Min=maxroot.");
4546
return 'ERROR';
4647
}
4748
else if (convert(f, min) == 0)
4849
{
49-
console.log("Wow, the lower bound of the given range was the root. Kinda seems like this was on purpose...");
50+
console.log("Min=root.");
5051
return min;
5152
}
5253
else if (convert(f, max) == 0)
5354
{
54-
console.log("Wow, the upper bound of the given range was the root. Kinda seems like this was on purpose...");
55+
console.log("Max=root.");
5556
return max;
5657
}
5758

@@ -84,9 +85,9 @@ export function bisection(f: ((x: number) => number) | string, min: number, max:
8485
}
8586
max = guess;
8687
}
87-
else
88+
else if (Math.abs(convert(f, guess) - 0) <= epsilon)
8889
{
89-
console.log(`Root: ${guess}.\nIterations it took: ${iter}.`);
90+
console.log(`Root: ${guess}.\nIterations it took: ${iter}.`);
9091
return guess;
9192
}
9293
if (annoyingConsoleLogs)

0 commit comments

Comments
 (0)