Python fabs() 函数
描述
fabs() 函数返回数字的绝对值,如 math.fabs(-5) 返回 5.0.
与 abs() 函数不同的是, fabs() 函数返回的是浮点型的值。
语法
import math
math.fabs(x)
注意:fabs() 是不能直接访问的,需要导入 math 模块,通过静态对象调用该方法。
参数
- x 数值表达式
返回值
返回数值的绝对值。
实例
# -*- coding: UTF-8 -*-
# import math # 导入数学模块
print "math.fabs(-45) : ", math.fabs(-45)
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) : 45.0
math.fabs(100.12) : 100.12
math.fabs(100.72) : 100.72
math.fabs(119L) : 119.0
math.fabs(math.pi) : 3.14159265359