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

ParameterRequiredDescription
xYesA 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)
Run Code Copy

Output

fabs(x) : 8.0

2. 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)
Run Code Copy

Output

fabs(x) : 8.586

3. 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)
Run Code Copy

Output

fabs(x) : inf

Summary

In this Python Math tutorial, we learned the syntax of, and examples for math.fabs() function.

Code copied to clipboard successfully 👍