Python log() 函數

Python 數字 Python 數字


描述

log() 返回 x 的自然對數。


語法

以下是 log() 方法的語法:

import math

math.log(x[, base])

注意:log()是不能直接訪問的,需要導入 math 模組,通過靜態對象調用該方法。


參數

  • x -- 數值運算式。
  • base -- 可選,底數,默認為 e。

  • 返回值

    返回 x 的自然對數,x>0。

    實例

    以下展示了使用 log() 方法的實例:

    實例(Python 2.0+)

    #!/usr/bin/python # -*- coding: UTF-8 -*- import math # 導入 math 模組 print "math.log(100.12) : ", math.log(100.12) print "math.log(100.72) : ", math.log(100.72) print "math.log(119L) : ", math.log(119L) print "math.log(math.pi) : ", math.log(math.pi) # 設置底數 print "math.log(10,2) : ", math.log(10,2)

    以上實例運行後輸出結果為:

    math.log(100.12) :  4.60636946656
    math.log(100.72) :  4.61234438974
    math.log(119L) :  4.77912349311
    math.log(math.pi) :  1.14472988585
    math.log(10,2) :  3.32192809489
    

    Python 數字 Python 數字