Python 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.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours

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

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours