Python3 fabs() 函數(shù)

Python3 數(shù)字 Python3 數(shù)字


描述

fabs() 方法返回數(shù)字的絕對值,如math.fabs(-10) 返回10.0。

fabs() 函數(shù)類似于 abs() 函數(shù),但是他有兩點區(qū)別:

  • abs() 是內(nèi)置函數(shù)。 fabs() 函數(shù)在 math 模塊中定義。

  • fabs() 函數(shù)只對浮點型跟整型數(shù)值有效。 abs() 還可以運用在復(fù)數(shù)中。


語法

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

import math

math.fabs( x )

注意:fabs()是不能直接訪問的,需要導(dǎo)入 math 模塊,通過靜態(tài)對象調(diào)用該方法。


參數(shù)

  • x -- 數(shù)值表達(dá)式。

返回值

返回數(shù)字的絕對值。

實例

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

#!/usr/bin/python3
import math   # 導(dǎo)入 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(math.pi) : ", math.fabs(math.pi))

以上實例運行后輸出結(jié)果為:

math.fabs(-45.17) :  45.17
math.fabs(100.12) :  100.12
math.fabs(100.72) :  100.72
math.fabs(math.pi) :  3.141592653589793

Python3 數(shù)字 Python3 數(shù)字