Skip to content

bisect

bisect(f, lower, upper, options?): number

Defined in: packages/math-analysis/src/bisect.ts:40

Finds where a function crosses zero, by repeatedly halving an interval known to contain the crossing.

You give it two points where f has opposite signs. Somewhere between them the function must pass through zero, so the answer is somewhere in that interval; bisection tries the midpoint, keeps whichever half still straddles zero, and repeats. Slow but incapable of failing — unlike Newton’s method, which needs a derivative and can wander off from a bad start with no warning.

Use it when reliability matters more than speed, when f is expensive to differentiate, or when you have no idea what the function looks like between the endpoints.

The error after n steps is at most (upper − lower) / 2ⁿ, so each step buys one bit. Reaching 1e-12 from a bracket of width 2 takes about 41.

O(log₂((upper − lower) / tolerance)) calls to f, capped at maxIterations; O(1) memory.

(x) => number

The continuous function whose root is sought.

number

Lower end of the bracket.

number

Upper end of the bracket.

ConvergenceOptions

Termination criteria. tolerance is the absolute bracket width to stop at, defaulting to 1e-12; maxIterations defaults to 200.

number

An approximation of the root.

bisect((x) => x * x - 2, 0, 2); // 1.414213562372879, against √2 = 1.4142135623730951
bisect((x) => x * x - 2, 2, 3); // throws RangeError — no sign change
```;
## Throws
When `f(lower)` and `f(upper)` have the same sign, so
nothing guarantees a root between them.
## See
[Root finding](https://anthochamp.pages.grenadine.io/node-essentials/topics/math/calculus/root-finding/)