This algorithm finds a given value n
by iterating between a lower bound and upper bound, similar to binary search. How can I improve this algorithm? A few details. The value n
is almost always less than 1.0, but this isn't a boundary condition. It is, however, never less than 0.
def approx_value(n, lower_bound, upper_bound, precision = 0.001):
approx = (lower_bound + upper_bound) / 2.0
while (abs(approx - n) > precision):
if n > approx:
lower_bound = approx
else:
upper_bound = approx
approx = (lower_bound + upper_bound) / 2.0
return approx
def approx_value_no_bounds(n, precision = 0.0001):
approx = 1.0
while (abs(approx - n) > precision):
if approx > n: #decrease volatility
return approx_value(n, 0.0, approx)
else:
approx *= 2
return approx
I know this seems odd because the value of n
is already supplied, but the algorithm is not complete. Basically, n
is the solution of a complex equation that cannot be solved for approx
in closed form, so I'm doing it iteratively. Eventually, this algorithm will compare the value of the function when using approx
with the value n
and return approx
is it approximates the input variable well enough.
I'm hoping to keep the running time in O(log(n))
, which is why I modelled it somewhat after binary search, albeit only somewhat because the upper bound is not necessarily known immediately.