Python math.fabs() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
math module

Definition

math.fabs(x) returns the absolute value (distance from zero) of a real number x, always as a float.

Unlike Python's built-in abs(), which preserves int types and accepts complex numbers, math.fabs() rejects complex values with TypeError and converts integers such as -7 to 7.0.

math.fabs() is a function in Python's standard math module: import it with import math and call math.fabs(...), or import the name directly with from math import fabs.

Syntax

Import math and pass any real number.

python
import math

mag = math.fabs(x)
CallWhat it does
math.fabs(-7)Returns 7.0 (float, unlike abs(-7)).
math.fabs(-3.5)Returns 3.5.
math.fabs(-0.0)Returns 0.0 (sign bit cleared).
math.fabs(float("-inf"))Returns inf.

For general Python code, built-in abs() is preferred because it works on ints, floats, complex numbers, Decimal, and custom objects.

Function signatures
python
math.fabs(x, /)
# returns: float |x| (real numbers only)

Parameters

ParameterDescription
xRequired positional real number (int or float). Complex numbers raise TypeError.

Return Value

Return ValueDescription
floatNon-negative magnitude of x as a float.

Python Examples

Example 1 — Compute Mean Absolute Sensor Error

Take non-negative errors with math.fabs() and average them accurately with math.fsum().

python
import math

errors = [-1.2, 0.5, -2.3, 1.0]
total_abs = math.fsum(math.fabs(e) for e in errors)
mean_err = total_abs / len(errors)
print(f"total={total_abs:.2f} mean={mean_err:.2f}")

How It Works

  1. Convert signs to magnitudes. Calling math.fabs() turns negative readings like -1.2 and -2.3 into positive floats.
  2. Exact float summation. Combining with math.fsum() totals the four magnitudes cleanly to 5.00.
  3. Mean error. Dividing by four readings gives an average absolute error of 1.25.

Example 2 — Compare math.fabs() With Built-in abs()

Check return type differences on integers and see how complex numbers behave.

python
import math

print(math.fabs(-7), type(math.fabs(-7)).__name__)
print(abs(-7), type(abs(-7)).__name__)
print(abs(3 + 4j))

try:
    math.fabs(3 + 4j)
except TypeError:
    print("math.fabs() rejects complex numbers")

How It Works

  1. Always returns float. Passing integer -7 to math.fabs() gives 7.0, whereas built-in abs(-7) keeps int.
  2. Complex support differs. Built-in abs(3 + 4j) returns Euclidean magnitude 5.0.
  3. Strict real input. math.fabs() raises TypeError on complex arguments.

Example 3 — Clear Negative Zero and Inspect Special Floats

Confirm math.fabs(-0.0) produces positive zero and handle infinity and NaN.

python
import math

pos_zero = math.fabs(-0.0)
print(pos_zero, math.copysign(1.0, pos_zero))
print(math.fabs(float("-inf")))
print(math.isnan(math.fabs(float("nan"))))

How It Works

  1. Clears negative sign bit. Checking with math.copysign() verifies math.fabs(-0.0) has a positive sign bit.
  2. Infinity turns positive. Negative infinity becomes positive inf.
  3. NaN remains NaN. Checking with math.isnan() confirms NaN stays NaN.

Frequently Asked Questions

abs() is built in, keeps ints as ints, works on complex numbers, and delegates to __abs__. math.fabs() always returns a float and only accepts real numbers.
5.0 (a float).
No, passing a complex number raises TypeError. Use built-in abs() instead.
Positive 0.0 with its sign bit cleared.
If given a huge Python integer larger than the maximum 64-bit float (~1.8e308). Built-in abs() never overflows on ints.

Next: math.factorial()

Return n! as an exact integer for non-negative integer n.

factorial() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful