While binary search is often performed on an ordered array to check
whether a given element is stored in that array, it can also be employed to
compute the inverse of an increasing or decreasing function on integers. In
the following code, the defined function bsearch_fun returns
an integer i0 such that f(i) <= x0 holds for i ranging from lb to i,
inclusively, and x0 < f(i) holds for i ranging from i+1 to ub, inclusively:
As an example, the following function
isqrt is defined based
on
bsearch_fun to compute the integer square root of a given
natural number, that is, the largest integer whose square is less than or
equal to the given natural number:
Note that the function
uint_of_int is for casting a signed
integer into an unsigned integer and the function
square
returns the square of its argument.
Please find on-line
the entire code in this section plus some additional code for testing.