Log to the base 2 in python

前端 未结 11 1181
刺人心
刺人心 2020-12-07 15:22

How should I compute log to the base two in python. Eg. I have this equation where I am using log base 2

import math
e = -(t/T)* math.log((t/T)[, 2])
         


        
11条回答
  •  萌比男神i
    2020-12-07 15:44

    http://en.wikipedia.org/wiki/Binary_logarithm

    def lg(x, tol=1e-13):
      res = 0.0
    
      # Integer part
      while x<1:
        res -= 1
        x *= 2
      while x>=2:
        res += 1
        x /= 2
    
      # Fractional part
      fp = 1.0
      while fp>=tol:
        fp /= 2
        x *= x
        if x >= 2:
            x /= 2
            res += fp
    
      return res
    

提交回复
热议问题