英文文檔:
abs(x)
Return the absolute value of a number. The argument may be an integer or a floating point number. If the argument is a complex number, its magnitude is returned
說明:
1. 返回數(shù)字的絕對值,參數(shù)可以是整數(shù)响鹃、浮點數(shù)或者復(fù)數(shù)
2. 如果參數(shù)是一個復(fù)數(shù)涂滴,此方法返回此復(fù)數(shù)的絕對值(此復(fù)數(shù)與它的共軛復(fù)數(shù)的乘積的平方根)
示例:
>>> help(abs)
Help on built-in function abs in module builtins:
abs(x, /)
Return the absolute value of the argument.
>>> abs(2)#正整數(shù)
>>> abs(-2)#負(fù)整數(shù)
>>> abs(0)#0
>>> abs(2.3)#正浮點數(shù)
2.3
>>> abs(-2.3)#負(fù)浮點數(shù)
2.3
>>> c1 = complex(1,1)
>>> c1 #復(fù)數(shù)
(1+1j)
>>> abs(c1) #復(fù)數(shù)絕對值
1.4142135623730951
>>> c2 = complex(-1,-1)
>>> c2 #復(fù)數(shù)
(-1-1j)
>>> abs(c2) #復(fù)數(shù)絕對值
1.4142135623730951
>>>