abs()是一個內建的函式。 fabs() 在math模組中定義。
fabs() 函式只適用於浮點和整數。abs() 複數也適用。
import math math.fabs( x )
註:此函式無法直接存取,所以我們需要匯入 math 模組,然後用數學靜態物件呼叫這個函式。
x -- 這是一個數位值
#!/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