Python fabs() 函數
描述
fabs() 方法返回數字的絕對值,如math.fabs(-10) 返回10.0。
語法
以下是fabs() 方法的語法:
import math math.fabs( x )
注意: fabs()是不能直接訪問的,需要導入math模塊,通過靜態對象調用該方法。
參數
- x -- 數值表達式。
返回值
返回數字的絕對值。實例
以下展示了使用fabs() 方法的實例:
#!/usr/bin/python # -*- coding: UTF-8 -*- import math # 导入数学模块 print "math.fabs(-45.17) : ", math.fabs(-45.17) print "math.fabs(100.12) : ", math.fabs(100.12) print "math.fabs(100.72) : ", math.fabs(100.72) print "math.fabs(119L) : ", math.fabs(119L) print "math.fabs(math.pi) : ", math.fabs(math.pi)
以上實例運行後輸出結果為:
math.fabs(-45.17) : 45.17 math.fabs(100.12) : 100.12 math.fabs(100.72) : 100.72 math.fabs(119L) : 119.0 math.fabs(math.pi) : 3.14159265359