math.fabs()

Anonymous contributor's avatar
Anonymous contributor
Published Sep 11, 2024
Contribute to Docs

The math.fabs() function takes a numeric value and returns its absolute value as a float. Unlike math.abs(), which can return the absolute value in the same type as the input, math.fabs() always converts the result to a float. Passing any non-numeric data type will raise a TypeError.

Syntax

math.fabs(n)
  • n: A numeric value for which the absolute value is to be computed.

Example

Below is an example of math.fabs() to return the floating-point absolute of the given value:

import math
print(math.fabs(-154.2))

The above code prints the following output:

154.2

Codebyte Example

Run the following code to understand how the math.fabs() function works:

Code
Output
Loading...

All contributors

Contribute to Docs

Learn Python on Codecademy