dichotomy

implementation of the dichotomic search algorithm. Supports promises

Usage no npm install needed!

<script type="module">
  import dichotomy from 'https://cdn.skypack.dev/dichotomy';
</script>

README

dichotomy(f, [a], [b]) ⇒ number | Promise.<number>

dichotomy.js Dichotomy algorithm: given a function f : x -> f(x) and two numbers a, b find y such that a < y < b and f(y) = 0

Kind: global function
Returns: number | Promise.<number> - y, such that f(y) == 0

Param Type Default Description
f function a function that takes a number and returns a number or a Promise
[a] Number -Number.MAX_VALUE Start of the search interval
[b] Number Number.MAX_VALUE End of the search interval

Example

dichotomy(x => x*x-2, 0, 10) == Math.sqrt(2)
dichotomy(x => Promise.resolve(x*x-2), 0, 10) == Promise.resolve(Math.sqrt(2))