The math.fabs()
function returns the absolute value of a number.
The absolute value of a given number, is the number's distance from zero. So for example, the absolute value of 6 is 6, and the absolute value of -6 is also 6, since they are both 6 units away from zero.
math.fabs(x)
Where, x
is the number whose absolute value is to be determined.
The function returns the absolute value of x
as a floating point value.
import math
>>> math.fabs(3)
3.0
>>> math.fabs(-3)
3.0
>>> math.fabs(-3.142)
3.142
>>> fabs(10)
10
>>> fabs(-50)
50
>>> fabs(100.11)
100.11
As shown above, the absolute value of a positive number is equal to the number itself, while the absolute value of a negative number is equal to its positive counterpart.