Python Math fabs() Method
The fabs() method returns the absolute value of a number, e.g., math.fabs(-10) returns 10.0.
The fabs() function is similar to the abs() function, but it differs in two ways:
- abs() is a built-in function. The fabs() function is defined in the math module.
- The fabs() function is only available for floating point and integer values. abs() can also be used with complex numbers.
Python Math fabs() Syntax
Here is the syntax of the fabs() method:
import math
math.fabs( x )
Note: fabs() is not directly accessible, you need to import the math module and call the method via a static object.
Python Math fabs() Parameters
- x – The numeric expression.
Python Math fabs() Return Value
Return the absolute value of the number.
Python Math fabs() Example#1
The following shows an example of using the fabs() method.
#!/usr/bin/python3
import math
x = -39.7
# returning the fabs of 39.7
print ("The fabs of 39.7 is : ", end ="")
print (math.fabs(x))
Output:
Python Math fabs() Example#2
#!/usr/bin/python3
import math
# prints the fabs using fabs() method
print ("math.fabs(-111.1) : ", math.fabs(-111.1))
print ("math.fabs(1055.96) : ", math.fabs(1055.96))
Output: