Python3 fabs() Function
Description
The fabs() method returns the absolute value of a number as a floating-point number, e.g., math.fabs(-10) returns 10.0.
The fabs() function is similar to the abs() function, but there are two differences:
abs() is a built-in function. The fabs() function is defined in the math module.
The fabs() function only works on floating-point and integer values. abs() can also be used with complex numbers.
Syntax
The following is the syntax of the fabs() method:
import math math.fabs( x )
Note:fabs() cannot be accessed directly; you need to import the math module and call this method via the static object.
Parameters
- x -- a numeric expression, can be an integer, floating-point number, etc. The function returns the absolute value of x, i.e., a non-negative number.
Return Value
Returns the absolute value of the number.Examples
The following demonstrates an example of using the fabs() method:
Example 1
x = -1.5
y = math.fabs(x)
print(y) # Output 1.5
x = 10
y = math.fabs(x)
print(y) # Output 10.0
In the above examples,fabs(-1.5)the absolute value of -1.5 is returned as 1.5,fabs(10)and the absolute value of 10 is returned as 10.0. Note that the return value is always a floating-point number, even if the argument passed is an integer.
After running the above examples, the output is:
1.5 10.0
Example 2
import math # Import the math module
print ("math.fabs(-45.17) : ", math.fabs(-45.17))
print ("math.fabs(100.12) : ", math.fabs(100.12))
print ("math.fabs(100.72) : ", math.fabs(100.72))
print ("math.fabs(math.pi) : ", math.fabs(math.pi))
After running the above examples, the output is:
math.fabs(-45.17) : 45.17 math.fabs(100.12) : 100.12 math.fabs(100.72) : 100.72 math.fabs(math.pi) : 3.141592653589793Other Extensions
Python3 Numbers