Lessons‎ > ‎old lesson page‎ > ‎logs and exponents‎ > ‎

iterative program to compute log in arbitrary bases

 

 

def ilog(base, val, reqPrecision=0.00001):

 expVal, logVal = 1.0, 0.0

  while (expVal * base) <= val:

    expVal *= base

    logVal += 1

  precision = 1.0

  while precision > reqPrecision:

    precision /= 2

    base = math.sqrt(base)

    if (expVal * base) <= val:

      expVal *= base

      logVal += precision

  return logVal


Comments