fabs()方法/函數返回x的絕對值。雖然類似於abs()函數,但是兩個函數之間有些差異
-
abs()是一個內置的函數。 fabs() 在math模組中定義。
-
fabs() 函數只適用於浮點和整數。abs() 複數也適用。
語法
以下是fabs() 函數/方法的語法:
import math math.fabs( x )
注:此函數無法直接訪問,所以我們需要導入 math 模組,然後用數學靜態對象調用這個函數。
參數
-
x -- 這是一個數字值
返回值
這個方法返回x的絕對值。
實例
下麵的例子顯示fabs() 方法/函數方法的使用。
#!/usr/bin/python3 import math # This will import math module 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(math.pi) : ", math.fabs(math.pi))
當我們運行上面的程式,它會產生以下結果:
math.fabs(-45.17) : 45.17 math.fabs(100) : 100.0 math.fabs(100.72) : 100.72 math.fabs(math.pi) : 3.141592653589793