mirror of
https://github.com/CompassConnections/Compass.git
synced 2026-08-01 10:51:29 -04:00
* Test * Add pretty formatting * Fix Tests * Fix Tests * Fix Tests * Fix * Add pretty formatting fix * Fix * Test * Fix tests * Clean typeckech * Add prettier check * Fix api tsconfig * Fix api tsconfig * Fix tsconfig * Fix * Fix * Prettier
27 lines
593 B
TypeScript
27 lines
593 B
TypeScript
export function binarySearch(min: number, max: number, comparator: (x: number) => number) {
|
|
let mid = 0
|
|
let i = 0
|
|
while (true) {
|
|
mid = min + (max - min) / 2
|
|
|
|
// Break once we've reached max precision.
|
|
if (mid === min || mid === max) break
|
|
|
|
const comparison = comparator(mid)
|
|
if (comparison === 0) break
|
|
else if (comparison > 0) {
|
|
max = mid
|
|
} else {
|
|
min = mid
|
|
}
|
|
|
|
i++
|
|
if (i > 100000) {
|
|
throw new Error(
|
|
'Binary search exceeded max iterations' + JSON.stringify({min, max, mid, i}, null, 2),
|
|
)
|
|
}
|
|
}
|
|
return mid
|
|
}
|