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
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().

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-fabs-vs-abs
Python | fabs() vs abs() - GeeksforGeeks
August 9, 2021 - In case the argument is a complex number, abs() will return the magnitude part whereas fabs() will return an error. To use the fabs() function we need to import the library "math" while the abs() function comes with the standard library of Python.
๐ŸŒ
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 ...
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ python-fabs-vs-abs
Python - fabs() vs abs()
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.
๐ŸŒ
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.
๐ŸŒ
IncludeHelp
includehelp.com โ€บ python โ€บ abs-vs-fabs-methods.aspx
Difference between abs() and fabs() methods in Python
In python, abs() method and fabs() method both are used to find the absolute value of a number.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ What-is-difference-in-abs-and-fabs-in-Python
What is difference in abs() and fabs() in Python?
Both abs() and fabs() represent the mathematical functions which give us the absolute value of numbers. But there is a subtle difference between both of them which we can explore in the exmaples below. Example The abs() functions ret
๐ŸŒ
Server Academy
serveracademy.com โ€บ blog โ€บ python-absolute-value-abs-tutorial
Python Absolute Value Abs() Tutorial - Server Academy
Now that you have a solid understanding of absolute values in Python, you can apply this simple but powerful tool to your projects, making your code cleaner and more efficient! Experiment with abs() and math.fabs() to see how absolute values can simplify your calculations and improve data processing.
Find elsewhere
๐ŸŒ
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.
๐ŸŒ
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 ...
๐ŸŒ
CodeSpeedy
codespeedy.com โ€บ home โ€บ abs() vs fabs() function in python
abs() vs fabs() method in Python - CodeSpeedy
February 23, 2020 - This method accepts a number as input and returns its absolute value. See the below implementation of the abs() method in Python. number = 5.7 print(abs(number)) number = -18.9 print(abs(number)) number = 5 print(abs(number)) number = -4 print(abs(number)) ... Now letโ€™s see how fabs() method behaves.
๐ŸŒ
Quora
quora.com โ€บ What-is-the-difference-between-abs-and-fabs-function
What is the difference between abs() and fabs() function? - Quora
The only difference between both of them is, abs() is used to calculate the absolute value for integer type numbers whereas fabs() are used for floating type numbers. abs() function is use un...
๐ŸŒ
Learn Coding Fast
learncodingfast.com โ€บ home โ€บ python fabs() function
Python fabs() function | Learn Coding Fast
September 1, 2020 - Any complex numbers or non-numerical values are ignored. Here are some topics weโ€™ll cover in todayโ€™s post: ... The fabs() function is a built-in function in the Python math module.
๐ŸŒ
Edureka Community
edureka.co โ€บ home โ€บ community โ€บ categories โ€บ python โ€บ python - abs vs fabs
Python - abs vs fabs | Edureka Community
September 19, 2018 - I noticed that in python there are two similar looking methods for finding the absolute value of a number ... .fabs(-5) How do these methods differ?
๐ŸŒ
Tutorial Gateway
tutorialgateway.org โ€บ python-fabs
Python fabs Function
March 26, 2025 - In this fabs example, we are going to find the absolute values of different data types and display the output. First, we declared a tuple and a list. Next, we used the fabs Function directly on both the Positive integer and negative integer. The first two Python statements find the corresponding absolute values, then we used the Function on Tuple and List items.
๐ŸŒ
Curict
en.curict.com โ€บ item โ€บ 65 โ€บ 65e4203.html
Python - Getting the Absolute Value
To get the absolute value, you can use the built-in function abs, the fabs function from the math module, and the fabs function from the cmath module.
๐ŸŒ
TradingCode
tradingcode.net โ€บ python โ€บ math โ€บ absolute-value
How to get absolute value of numbers in Python? โ€ข TradingCode
Python has two ways to get the absolute value of a number: The built-in abs() function returns the absolute value. The math.fabs() function also returns the absolute value, but as a floating-point value.
๐ŸŒ
Python Pool
pythonpool.com โ€บ home โ€บ blog โ€บ python absolute value | abs() function with examples
Python Absolute Value | abs() Function With Examples - Python Pool
December 30, 2023 - Python absolute value means removing any negative sign in front of a number and thinking of all numeric values as positive (or zero). However, in Python, we can get the absolute value of any number by inbuilt functions, which are abs() and fabs().