首页 > 代码库 > python 实现 math.log(x,base)

python 实现 math.log(x,base)

python 用闭包实现math.log(x,base)

 

#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys,math
import random
import pprint





def log(n,d):
    i = 0
    status = 0
    while True:
        if d**i==n:
            status=1
            break
        elif d**i<n<d**(i+1):
            break
        i+=1

    def test(level=100):
        if status == 1:
            return i
        start= 0
        k = i
        m = i+1
        while start<level:

            if n<d**((k+m)/2):
                m = (k+m)/2
            elif n==n<d**((k+m)/2):
                return (k+m)/2
            else:
                k = (k+m)/2
            start+=1
        return (k+m)/2
    return test


a = log(200,10)
print(a(20))

  

python 实现 math.log(x,base)