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 - The difference is that math.fabs(number) will always return a floating-point number even if the argument is an integer, whereas abs() will return a floating-point or an integer depending upon the argument.
🌐
IncludeHelp
includehelp.com › python › abs-vs-fabs-methods.aspx
Difference between abs() and fabs() methods in Python
fabs() method can accept positive or negative integer value, positive or negative float value and returns the absolute value of float type. ... # Python code to demonstrate example of # fabs() method # importing math module import math a = 10 # +ve integer b = -10 # -ve integer c = 10.23 # ...
🌐
TutorialsPoint
tutorialspoint.com › python-fabs-vs-abs
Python - fabs() vs abs()
July 22, 2020 - Unlike the abs() method, the fabs() method will always produce the result in float type; and it does not accept complex numbers as its arguments. Note − This function is not accessible directly, so we need to import math module and then we ...
🌐
Note.nkmk.me
note.nkmk.me › home › python
Get Absolute Values in Python: abs(), math.fabs() | note.nkmk.me
May 11, 2025 - math.fabs() also returns the absolute value, but unlike abs(), it always returns a floating-point number (float).
🌐
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.
🌐
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.
Find elsewhere
🌐
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?
🌐
Server Academy
serveracademy.com › blog › python-absolute-value-abs-tutorial
Python Absolute Value Abs() Tutorial - Server Academy
November 12, 2024 - Python’s math library provides more advanced mathematical functions, including some ways to work with absolute values. While abs() handles most cases, math.fabs() specifically returns the absolute value of a floating-point number.
🌐
Learn Coding Fast
learncodingfast.com › home › python fabs() function
Python fabs() function | Learn Coding Fast
September 1, 2020 - If we pass a complex number to the fabs() function, we get an error. In contrast, if we pass it to the abs() function, we get what is known as the magnitude of the number, given by the formula below: ... To create a complex number in Python, you can use kj or kJ to represent the imaginary component, where k is a constant and j (or J) stands for square root -1.
🌐
Quora
quora.com › What-is-the-difference-between-abs-and-fabs-function
What is the difference between abs() and fabs() function? - Quora
... IT Consultant living in Germany, doing "it" since the 8-bit aera · Author has 1.3K answers and 8.3M answer views · 6y · abs() will yield the absolute values of integers, fabs() will yield the absolute values of floating point values.
🌐
Tutorialspoint
tutorialspoint.com › python › number_fabs.htm
Python math.fabs() Method
Unlike the abs() method, the fabs() method will always produce the result in float type; and it does not accept complex numbers as its arguments. Note − This function is not accessible directly, so we need to import math module and then we ...
🌐
Sololearn
sololearn.com › en › Discuss › 1799301 › qus10what-is-the-difference-between-abs-and-fabs-functions
Qus10:-What is the difference between abs() and fabs() functions? | Sololearn: Learn to code for FREE!
Hi if you have a float number, you have to use fabs() if you have an integer number, you have to use abs() exemple with fabs() : fabs(239.14) return 239.14 fabs(-239.14) return 239.14 exemple with abs() : abs(239) return 239 abs(-239) return 239
🌐
Alibaba Cloud
topic.alibabacloud.com › a › the-difference-between-font-colorredabsfont--and-mathfabs--in-python_1_29_30245161.html
The difference between ABS () and Math.fabs () in Python
June 28, 2018 - Description: The method in Python fabs(x) Returns the absolute value of x . Although similar to abs() a function, the following differences exist between the two functions: abs()is a built-in function that is fabs() defined in a math module.
🌐
sqlpey
sqlpey.com › python › python-abs-vs-fabs
Top 2 Methods to Understand Python's abs vs fabs for Absolute Values
November 23, 2024 - Explore the differences between Python's abs and math.fabs methods for finding absolute values.
🌐
Sololearn
sololearn.com › en › Discuss › 2784928 › fabs-and-abs
fabs and abs | Sololearn: Learn to code for FREE!
May 15, 2021 - ... f in fabs stands for float The difference is that math.fabs(number) will always return a floating point number even if the argument is integer, whereas abs() will return a floating point or an integer depending upon the argument.
🌐
YouTube
youtube.com › how to fix your computer
PYTHON : Python - abs vs fabs - YouTube
PYTHON : Python - abs vs fabs [ Gift : Animated Search Engine : https://www.hows.tech/p/recommended.html ] PYTHON : Python - abs vs fabs Note: The informati...
Published   December 6, 2021
Views   51
🌐
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 ...
🌐
Brainly
brainly.in › computer science › secondary school
what is the difference between math.abs() and math.fabs()? give examples .python se​ - Brainly.in
April 6, 2020 - The difference is that math. fabs(number) will always return a floating point number even if the argument is integer, whereas abs() will return a floating point or an integer depending upon the argument.