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 ...
๐ŸŒ
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 โ€บ python-fabs-vs-abs
Python - fabs() vs abs()
July 22, 2020 - The result of this method is never negative; even if the number is a negative value, the method will return the negation of it. Unlike the abs() method, the fabs() method will always produce the result in float type; and it does not accept complex ...
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ What-is-difference-in-abs-and-fabs-in-Python
What is difference in abs() and fabs() in Python?
March 2, 2020 - 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
November 12, 2024 - 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
๐ŸŒ
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 ...
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ python โ€บ number_fabs.htm
Python math.fabs() Method
The result of this method is never negative; even if the number is a negative value, the method will return the negation of it. Unlike the abs() method, the fabs() method will always produce the result in float type; and it does not accept complex ...
๐ŸŒ
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 ...
๐ŸŒ
Learn Coding Fast
learncodingfast.com โ€บ home โ€บ python fabs() function
Python fabs() function | Learn Coding Fast
September 1, 2020 - It accepts a number and returns the absolute value of that number as a FLOAT. The absolute value of a number refers to the value of the number without its sign. If we pass an invalid input to the fabs() function (e.g. if the input is a string), ...
๐ŸŒ
sqlpey
sqlpey.com โ€บ python โ€บ python-abs-vs-fabs
Top 2 Methods to Understand Python's abs vs fabs for Absolute Values
August 9, 2021 - For more details on math.fabs(), refer to Pythonโ€™s math library documentation . Meanwhile, abs() is more versatile. This function is compatible with integers, floats, and complex numbers, returning the absolute value without altering the data type.
๐ŸŒ
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?
๐ŸŒ
Cach3
w3schools.com.cach3.com โ€บ python โ€บ ref_math_fabs.asp.html
Python math.fabs() Method - W3Schools
Unlike abs(), this method always converts the value to a float value. It will throw an exception if it can't. math.fabs(x) โฎ Math Methods ยท Tabs Dropdowns Accordions Side Navigation Top Navigation Modal Boxes Progress Bars Parallax Login Form HTML Includes Google Maps Range Sliders Tooltips ...
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
Codecademy
codecademy.com โ€บ docs โ€บ python โ€บ math module โ€บ math.fabs()
Python | Math Module | math.fabs() | Codecademy
September 11, 2024 - 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.
๐ŸŒ
Curict
en.curict.com โ€บ item โ€บ 65 โ€บ 65e4203.html
Python - Getting the Absolute Value
February 6, 2024 - import math Absolute value (floating-point type) = math.fabs(<integer type>) Absolute value (floating-point type) = math.fabs(<floating-point type>) If you pass an integer type to the abs function, it returns the absolute value as an integer type.