math.fabs
Python math.fabs()方法 (Python math.fabs() method)
math.fabs() method is a library method of math module, it is used to get the absolute value of a number, it accepts a number (that can be either positive integer/float or negative integer/float) and returns an absolute value in the float type.
math.fabs()方法是数学模块的库方法,用于获取数字的绝对值,它接受数字(可以为正整数/浮点数或负整数/浮点数)并返回绝对值在浮点类型。
Syntax of math.fabs() method:
math.fabs()方法的语法:
math.fabs(n)
Parameter(s): n – a number or a numeric expression.
参数: n-数字或数字表达式。
Return value: float – it returns a float value, which is an absolute value of given number/numeric expression n.
返回值: float-返回浮点值,它是给定数字/数字表达式n的绝对值。
Example:
例:
Input:
a = -10
b = 10.23
# function call
print(math.fabs(a))
print(math.fabs(b))
Output:
10.0
10.23
Python代码演示math.fabs()方法的示例 (Python code to demonstrate example of math.fabs() method)
# Python code to demonstrate example of
# math.fabs() method
# importing math module
import math
# numbers
a = 10 # +ve integer
b = 10.23 # +ve float
c = -10 # -ve integer
d = -10.23 # -ve float
# printing absolute values
print("fabs(a): ", math.fabs(a))
print("fabs(b): ", math.fabs(b))
print("fabs(c): ", math.fabs(c))
print("fabs(d): ", math.fabs(d))
Output
输出量
fabs(a): 10.0
fabs(b): 10.23
fabs(c): 10.0
fabs(d): 10.23
翻译自: https://www.includehelp.com/python/math-fabs-method-with-example.aspx
math.fabs