math.fabs() converts its argument to float if it can (if it can't, it throws an exception). It then takes the absolute value, and returns the result as a float.

In addition to floats, abs() also works with integers and complex numbers. Its return type depends on the type of its argument.

In [7]: type(abs(-2))
Out[7]: int

In [8]: type(abs(-2.0))
Out[8]: float

In [9]: type(abs(3+4j))
Out[9]: float

In [10]: type(math.fabs(-2))
Out[10]: float

In [11]: type(math.fabs(-2.0))
Out[11]: float

In [12]: type(math.fabs(3+4j))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/home/npe/<ipython-input-12-8368761369da> in <module>()
----> 1 type(math.fabs(3+4j))

TypeError: can't convert complex to float
Answer from NPE on Stack Overflow
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_math_fabs.asp
Python math.fabs() Method
The math.fabs() method returns the absolute value of a number, as a float. Absolute denotes a non-negative number. This removes the negative sign of the value if it has any. Unlike Python abs(), this method always converts the value to a float value.
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ python โ€บ number_fabs.htm
Python math.fabs() Method
The Python math.fabs() method is used to calculate the float absolute value of a number. The result of this method is never negative; even if the number is a negative value, the method will return the negation of it.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-math-fabs-function
Python | math.fabs() function - GeeksforGeeks
February 20, 2023 - Syntax: math.fabs(x) Parameter: x: This is a numeric expression. Returns: the absolute value of the number. Time Complexity: O(1) Auxiliary Space: O(1) ... # Python code to demonstrate the working of fabs() # importing &quot;math&quot; for mathematical operations import math x = -33.7 # returning the fabs of 33.7 print (&quot;The fabs of 33.7 is : &quot;, end =&quot;&quot;) print (math.fabs(x))
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.1 โ€บ reference โ€บ generated โ€บ numpy.fabs.html
numpy.fabs โ€” NumPy v2.1 Manual
numpy.fabs(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature]) = <ufunc 'fabs'># Compute the absolute values element-wise. This function returns the absolute values (positive magnitude) of the data in x. Complex values are not handled, use absolute ...
Top answer
1 of 5
139

math.fabs() converts its argument to float if it can (if it can't, it throws an exception). It then takes the absolute value, and returns the result as a float.

In addition to floats, abs() also works with integers and complex numbers. Its return type depends on the type of its argument.

In [7]: type(abs(-2))
Out[7]: int

In [8]: type(abs(-2.0))
Out[8]: float

In [9]: type(abs(3+4j))
Out[9]: float

In [10]: type(math.fabs(-2))
Out[10]: float

In [11]: type(math.fabs(-2.0))
Out[11]: float

In [12]: type(math.fabs(3+4j))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/home/npe/<ipython-input-12-8368761369da> in <module>()
----> 1 type(math.fabs(3+4j))

TypeError: can't convert complex to float
2 of 5
10

Edit: as @aix suggested, a better (more fair) way to compare the speed difference:

In [1]: %timeit abs(5)
10000000 loops, best of 3: 86.5 ns per loop

In [2]: from math import fabs

In [3]: %timeit fabs(5)
10000000 loops, best of 3: 115 ns per loop

In [4]: %timeit abs(-5)
10000000 loops, best of 3: 88.3 ns per loop

In [5]: %timeit fabs(-5)
10000000 loops, best of 3: 114 ns per loop

In [6]: %timeit abs(5.0)
10000000 loops, best of 3: 92.5 ns per loop

In [7]: %timeit fabs(5.0)
10000000 loops, best of 3: 93.2 ns per loop

In [8]: %timeit abs(-5.0)
10000000 loops, best of 3: 91.8 ns per loop

In [9]: %timeit fabs(-5.0)
10000000 loops, best of 3: 91 ns per loop

So it seems abs() only has slight speed advantage over fabs() for integers. For floats, abs() and fabs() demonstrate similar speed.


In addition to what @aix has said, one more thing to consider is the speed difference:

In [1]: %timeit abs(-5)
10000000 loops, best of 3: 102 ns per loop

In [2]: import math

In [3]: %timeit math.fabs(-5)
10000000 loops, best of 3: 194 ns per loop

So abs() is faster than math.fabs().

๐ŸŒ
Codecademy
codecademy.com โ€บ docs โ€บ python โ€บ math module โ€บ math.fabs()
Python | Math Module | math.fabs() | Codecademy
September 11, 2024 - Learn the basics of Python 3.12, ... Friendly.Beginner Friendly24 hours24 hours ยท math.fabs(n) n: A numeric value for which the absolute value is to be computed....
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python
Get Absolute Values in Python: abs(), math.fabs() | note.nkmk.me
May 11, 2025 - In Python, you can get the absolute value of a number using either the built-in abs() function or the math module's fabs() function. Built-in Functions - abs() โ€” Python 3.13.3 documentation math.fabs ...
๐ŸŒ
Learn Coding Fast
learncodingfast.com โ€บ home โ€บ python fabs() function
Python fabs() function | Learn Coding Fast
September 1, 2020 - If we have a try block without an except block, Python gives us a syntax error and the code will not run. Most of the time, when the task in the try block fails, we want to do something in the except block (for instance, we may want to print an error statement). However, in our myFabs() function, we do not want to do anything when the fabs() function fails in the try block.
Find elsewhere
๐ŸŒ
Tutorial Gateway
tutorialgateway.org โ€บ python-fabs
Python fabs Function
March 26, 2025 - The Python fabs function is one of the Math function which returns the absolute value of the specified expression or an individual number.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-number-fabs-method
fabs() method of Python's math Library - GeeksforGeeks
October 23, 2020 - Returns : Returns the magnitude of the element passed in the function. Always returns a floating point number, irrespective of the data type of the argument number. Code #1 : Code to demonstrate fabs() working ...
๐ŸŒ
Cach3
tutorialspoint.com.cach3.com โ€บ python3 โ€บ number_fabs.htm
Python 3 Number fabs() Method
fabs() function works only on float and integer whereas abs() works with complex number also. ... Note โˆ’ This function is not accessible directly, so we need to import the math module and then we need to call this function using the math static object. ... This method returns the absolute ...
๐ŸŒ
Javatpoint
javatpoint.com โ€บ fabs-in-python
Fabs in Python - Javatpoint
Fabs in Python with python, tutorial, tkinter, button, overview, entry, checkbutton, canvas, frame, environment set-up, first python program, operators, etc.
๐ŸŒ
IncludeHelp
includehelp.com โ€บ python โ€บ math-fabs-method-with-example.aspx
math.fabs() method with example in Python
math.fabs() method is a library method of math module, it is used to get the absolute value of a number, it accepts a number (that can be either positive integer/float or negative integer/float) and returns an absolute value in the float type. ... Parameter(s): n โ€“ a number or a numeric ...
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ math.html
math โ€” Mathematical functions
Return the ceiling of x, the smallest integer greater than or equal to x. If x is not a float, delegates to x.__ceil__, which should return an Integral value. math.fabs(x)ยถ ยท Return the absolute value of x. math.floor(x)ยถ ยท Return the floor ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python-fabs-vs-abs
Python | fabs() vs abs() - GeeksforGeeks
August 9, 2021 - In Python, math module contains a number of mathematical operations, which can be performed with ease using the module. math.fabs() function returns the absolute value of the number.
๐ŸŒ
Python Examples
pythonexamples.org โ€บ python-math-fabs-absolute-value
Python math.fabs() - Absolute Value
Discover how to use the math.fabs() function in Python to calculate the absolute value of a number. This tutorial covers the syntax, parameters, and examples, including calculations for integers, floating-point numbers, and even negative infinity.
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ reference โ€บ generated โ€บ numpy.fabs.html
numpy.fabs โ€” NumPy v2.4 Manual
numpy.fabs(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature]) = <ufunc 'fabs'># Compute the absolute values element-wise. This function returns the absolute values (positive magnitude) of the data in x. Complex values are not handled, use absolute ...
๐ŸŒ
Pythontic
pythontic.com โ€บ modules โ€บ math โ€บ fabs
Python math module - fabs function | Pythontic.com
The fabs function returns the absolute value of a number. It returns the quantity, magnitude without the direction on the numberline
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python โ€บ numpy
NumPy: Calculate the absolute value element-wise (np.abs, np.fabs) | note.nkmk.me
January 14, 2024 - Posted: 2024-01-14 | Tags: Python, NumPy ยท You can calculate the absolute value element-wise in a NumPy array (ndarray) using np.abs(), np.absolute(), or np.fabs(). Note that np.abs() is simply an alias for np.absolute(). Additionally, the built-in abs() function can be used.
๐ŸŒ
Cach3
w3schools.com.cach3.com โ€บ python โ€บ ref_math_fabs.asp.html
Python math.fabs() Method
The math.fabs() method returns the absolute value of a number. Absolute denotes a non-negative number. This removes the negative sign of the value if it has any. Unlike abs(), this method always converts the value to a float value. It will throw an exception if it can't. ... Tabs Dropdowns ...