math.fabs() 方法是 Python 的 math 模块中的一个函数,用于计算一个数的绝对值。

以下是 math.fabs() 方法的基本语法:
math.fabs(x)

  •  x: 一个数字。


返回值是一个浮点数,表示给定数字的绝对值。

以下是一些示例:
import math

# 计算 -5 的绝对值
result1 = math.fabs(-5)
print(result1)  # 输出 5.0

# 计算 3.14 的绝对值
result2 = math.fabs(3.14)
print(result2)  # 输出 3.14

# 计算 0 的绝对值
result3 = math.fabs(0)
print(result3)  # 输出 0.0

在这个例子中,math.fabs() 函数分别计算了 -5、3.14 和 0 的绝对值。这个函数在需要获取数字的绝对值时非常有用。


转载请注明出处:http://www.zyzy.cn/article/detail/367/Python3