Python math.fabs() - Absolute Value
Python math.fabs()
math.fabs(x) function returns the absolute value of x.
Syntax
The syntax to call fabs() function is
math.fabs(x)where
| Parameter | Required | Description |
|---|---|---|
| x | Yes | A numeric value. |
Examples
1. Absolute value of an integer
In the following program, we find the absolute value of integer number in the variable x.
Python Program
import math
x = -8
result = math.fabs(x)
print('fabs(x) :', result)Output
fabs(x) : 8.02. Absolute value of a floating point number
In the following program, we find the absolute value of a floating point number -8.586.
Python Program
import math
x = -8.586
result = math.fabs(x)
print('fabs(x) :', result)Output
fabs(x) : 8.5863. Absolute value of negative infinity
In the following program, we find the absolute value of negative infinity.
Python Program
import math
x = -math.inf
result = math.fabs(x)
print('fabs(x) :', result)Output
fabs(x) : infSummary
In this Python Math tutorial, we learned the syntax of, and examples for math.fabs() function.