两者 abs() 函数用于求一个数的绝对值,即去掉一个数的负号。
null
abs()的语法:
abs(number)
fabs()的语法:
math.fabs(number)
两者都将返回一个数字的绝对值。
区别在于数学。fabs(number)将始终返回浮点数,即使参数是整数,而abs()将根据参数返回浮点或整数。
如果参数是复数,abs()将返回幅值部分,而fabs()将返回错误。 要使用fabs()函数,我们需要导入库“math”,而abs()函数是Python的标准库。
Python3
# Python code to demonstrate working # of fabs() and abs() import math ################################# # When the argument is an integer# ################################# number = - 10 # abs() will return an integer as # the argument is an integer print ( abs (number)) # fabs() will return a floating point number print (math.fabs(number)) ########################################### # When the input is a floating point number# ########################################### number = - 12.08 # abs() will return an floating point number # as the argument is a floating point number print ( abs (number)) # fabs() will return a floating point number print (math.fabs(number)) #################################### # When the input is a complex number# #################################### number = complex ( 3 , 4 ) # abs() will return the magnitude print ( abs (number)) # fabs() will return an error # print(math.fabs(number)) |
输出:
1010.012.0812.085.0
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END