import mpmath


def _solve(func):
    """
    Solve func(nc) = 0.  func must be an increasing function.
    """
    with mpmath.extradps(5):
        # We could just as well call the variable `x` instead of `nc`, but we
        # always call this function with functions for which nc (the
        # noncentrality parameter) is the variable for which we are solving.
        nc = mpmath.mp.one
        value = func(nc)
        if value == 0:
            return nc

        # Multiplicative factor by which to increase or decrease nc when
        # searching for a bracketing interval.
        factor = mpmath.mpf(2)
        # Find a bracketing interval.
        if value > 0:
            nc /= factor
            while func(nc) > 0:
                nc /= factor
            lo = nc
            hi = factor*nc
        else:
            nc *= factor
            while func(nc) < 0:
                nc *= factor
            lo = nc/factor
            hi = nc

        # lo and hi bracket the solution for nc.
        nc = mpmath.findroot(func, (lo, hi), solver='illinois')
        return nc