Python Math.fabs()方法
我们使用Python中的math
模块来实现各种数学功能。其中一个方法是math.fabs()
方法,它与math.abs()
方法类似。
这个方法,math.fabs()
,以浮点数的形式返回绝对值。它去掉了数字的负号,这意味着math.fabs()
方法返回的是非负数。
这个方法在Python 2.6及以上版本中可用。在以前的版本中,我们使用内置的abs()
方法来返回绝对值。
我们使用float()
方法来获得浮点数的答案。math.fabs()
方法解决了同时使用这两种方法的问题,我们只使用一种方法。
Pythonmath.fabs()
方法的语法
math.fabs(number)
参数
number |
这是一个必要的参数,是一个数字值。 |
返回
该方法以浮点数的形式返回指定数值的绝对值。当输入的不是一个数字时,会产生TypeError
。
示例代码:使用math.fabs()
方法
要使用fabs()
方法,我们需要导入与之相关的静态对象,称为math
。在下面的例子中,我们将不同类型的数据传递给math.fabs()
方法,并在控制台输出其结果。
fabs()
表示float绝对值方法,该方法将数字值作为所需参数,并返回绝对值,即float中的非负数。我们用不同类型的输入值如正/负整数和浮点数测试该方法 。math.fabs()
这个方法是Python中理论和表示函数的一部分。
# import math static object for fabs() method
import math
# returns floating nonnegative value for each type of specified data
def float_absolute(number):
return math.fabs(number)
# positive integer
print ("The floating absolute value of 208 is", float_absolute(208))
# negative decimal number
print ("The floating absolute value of -75.23 is", float_absolute(-75.23))
# postive decimal number
print ("The floating absolute value of 90.66 is", float_absolute(90.66))
print ("The floating absolute value of math.pi is", float_absolute(math.pi))
输出:
# output for different types of inputs
The floating absolute value of 208 is 208.0
The floating absolute value of -75.23 is 75.23
The floating absolute value of 90.66 is 90.66
The floating absolute value of math.pi is 3.141592653589793
示例代码:math.fabs()
和abs()
方法之间的区别
abs()
方法是一个内置的实用函数,而fabs()
方法是Python 2.6中引入的math
模块方法。在下面的例子中,当我们向fabs()
方法传递整数或浮点数时,它总是以浮点数返回结果。
而在abs()
方法中,当我们传递整数值时,它返回整数值;同样地,当我们提供浮动值时,它返回浮动值。
import math
# test for negative integer values
print ("The result of fabs method for integer value is ", math.fabs(-97))
print ("The result of abs method for integer value is ", abs(-97))
# test for positive float values
print ("The result of fabs method for float value is ", math.fabs(101.8))
print ("The result of abs method for float value is ", abs(101.8))
输出:
# Difference between abs and fabs method with different inputs
The result of fabs method for integer value is 97.0
The result of abs method for integer value is 97
The result of fabs method for float value is 101.8
The result of abs method for float value is 101.8
相似之处是两个方法都只接受整数和浮点数形式的输入。第二个例子显示,当我们传递除整数或浮点数以外的任何其他类型的数据时,该方法返回TypeError
。
import math
# returns TypeError for string data type
def check_type(value):
return math.fabs(value)
print ("For a string value ", math.fabs("Hello"))
输出:
TypeError: must be real number, not str
示例代码:向math.fabs()
方法传递一个负数输入
在下面的例子中,我们从用户那里获得两个输入。第一个输入是一个负的十进制数字,另一个是一个正的整数。
这两个输入都被传递给math.fabs()
方法,然后对两个数字进行求和。
import math
# two inputs for positive and negative numbers
input1 = float(input("Enter a negative float number:"))
input2 = int(input("Enter a positive integer: "))
print("The absolute value of the first input is", math.fabs(input1))
print("The absolute value of the second input is", math.fabs(input2))
total = input1 + float(input2)
print("The sum of both inputs is", total)
print("The absolute value of the sum is", math.fabs(total))
输出:
Enter a negative float number:-8.96
Enter a positive integer: 34
The absolute value of the first input is 8.96
The absolute value of the second input is 34.0
The sum of both inputs is 25.04
The absolute value of the sum is 25.04
示例代码:解决使用math.fabs()
方法时的name 'math' is not defined
错误
下面的例子在Python代码中使用了from math import *
。解释器返回一个错误信息,说math
模块没有定义。
# incorrect import type
from math import *
print("The absolute value of 1235 is", math.fabs(1235))
输出:
# fabs() method returns error on incorrect import type
NameError: name 'math' is not defined
为了解决这个错误,我们需要通过使用import math
直接导入math
模块。
# correct import for the use of math module
import math
print("The absolute value of 1235 is", math.fabs(1235))
输出结果:
The absolute value of 1235 is 1235.0