In article <h2l36k$q5...@reader1.panix.com>, kj  <no.em...@please.post> wrote:
>
>This seemed straightforward enough, until I realized that, to be
>useful to my students in their homework, this _binary_search function
>had to handle the case in which the passed function was monotonically
>decreasing in the specified interval...
>
>def _binary_search(lo, hi, func, target, epsilon):
>    assert lo < hi
>    assert epsilon > 0
>    sense = cmp(func(hi), func(lo))
>    if sense == 0:
>        return None
>    target_plus = sense * target + epsilon
>    target_minus = sense * target - epsilon
>    while True:
>        param = (lo + hi) * 0.5
>        value = sense * func(param)
>        if value > target_plus:
>            hi = param
>        elif value < target_minus:
>            lo = param
>        else:
>            return param
>
>       if lo == hi:
>           return None
>
>My question is: is the business with sense and cmp too "clever"?

First of all, cmp() is gone in Python 3, unfortunately, so I'd avoid
using it.  Second, assuming I understand your code correctly, I'd change
"sense" to "direction" or "order".
-- 
Aahz (a...@pythoncraft.com)           <*>         http://www.pythoncraft.com/

"as long as we like the same operating system, things are cool." --piranha
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to