Python3 fabs()函式

2019-10-16 23:11:53
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