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
🌐
Tutorialspoint
tutorialspoint.com › python › number_fabs.htm
Python math.fabs() Method
import math # Create positive Integer ... in this example, we are creating number objects with negative values and try to use the fabs() method to calculate their absolute values of float type....
🌐
W3Schools
w3schools.com › python › ref_math_fabs.asp
Python math.fabs() Method
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Certificate Python Training ... #Import math Library import math #Print absolute values from numbers print(math.fabs(-66.43)) print(math.fabs(-7)) Try it Yourself »
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, one of the most powerful, versatile, and in-demand programming languages today. With CertificateWith Certificate · Beginner Friendly.Beginner Friendly24 hours24 hours · math.fabs(n) n: A numeric value for which the absolute value is to be computed. Below is an example of math.fabs() to return the floating-point absolute of the given value: import math ·
🌐
Tutorial Gateway
tutorialgateway.org › python-fabs
Python fabs Function
May 11, 2025 - import math Tup = (1, 2, 3, -4 , 5) # Tuple Declaration Lis = [-1, 2, -3.5, -4 , 5] # List Declaration print('Absolute value of Positive Number = %.2f' %math.fabs(10)) print('Absolute value of Negative Number = %.2f' %math.fabs(-15)) print('Absolute ...
🌐
Note.nkmk.me
note.nkmk.me › home › python
Get Absolute Values in Python: abs(), math.fabs() | note.nkmk.me
May 11, 2025 - class MyClass: def __abs__(self): return 100 mc = MyClass() # math.fabs(mc) # TypeError: must be real number, not MyClass
🌐
IncludeHelp
includehelp.com › python › math-fabs-method-with-example.aspx
math.fabs() method with example in Python
Example: Input: a = -10 b = 10.23 # function call print(math.fabs(a)) print(math.fabs(b)) Output: 10.0 10.23 · # Python code to demonstrate example of # math.fabs() method # importing math module import math # numbers a = 10 # +ve integer b = 10.23 # +ve float c = -10 # -ve integer d = -10.23 ...
🌐
Python
docs.python.org › 3 › library › math.html
math — Mathematical functions
1 month ago - 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 ...
Find elsewhere
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-math-fabs-function
Python | math.fabs() function - GeeksforGeeks
March 26, 2025 - # Python code to demonstrate the working of fabs() # importing &quot;math&quot; for mathematical operations import math # prints the fabs using fabs() method print (&quot;math.fabs(-13.1) : &quot;, math.fabs(-13.1)) print (&quot;math.fabs(101.96) ...
🌐
GeeksforGeeks
geeksforgeeks.org › python-fabs-vs-abs
Python | fabs() vs abs() - GeeksforGeeks
October 23, 2020 - The absolute value of any number is always positive it removes the negative sign of a number in Python. Example:Input: -29Output: 29Python abs() Function SyntaxThe abs() function in Python has the following syntax:Syntax: abs(number)number: Intege ·
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.fabs.html
numpy.fabs — NumPy v2.1 Manual
October 23, 2020 - Absolute values including complex types. Examples · >>> import numpy as np >>> np.fabs(-1) 1.0 >>> np.fabs([-1.2, 1.2]) array([ 1.2, 1.2]) On this page
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-number-fabs-method
fabs() method of Python's math Library - GeeksforGeeks
August 9, 2021 - 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 ... # Python3 code to demonstrate # the working of fabs() # for fabs() import math # initializing integer x = -2 # initializing float y = -2.00 # printing magnitude and type of output # type conversion to float print("The magnitude of integer is : ", end ="") print(math.fabs(x)) print("The type of output is : ", end ="") print(type(math.fabs(x))) print("\r") # printing magnitude and type of output print("The magnitude of float is : ", end ="") print(math.fabs(y)) print("The type of output is : ", end ="") print(type(math.fabs(y))) Output :
🌐
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.
🌐
Cach3
w3schools.com.cach3.com › python › ref_math_fabs.asp.html
Python math.fabs() Method - W3Schools
Python Examples Python Exercises Python Quiz Python Certificate ... #Import math Library import math #Remove - sign of given number print(math.fabs(-66.43)) print(math.fabs(-7)) Try it Yourself »
🌐
Python Examples
pythonexamples.org › python-math-fabs-absolute-value
Python math.fabs() - Absolute Value
June 30, 2012 - In the following program, we find the absolute value of a floating point number -8.586. import math x = -8.586 result = math.fabs(x) print('fabs(x) :', result)
🌐
Cach3
tutorialspoint.com.cach3.com › python3 › number_fabs.htm
Python 3 Number fabs() Method - Tutorials Point
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 value of x. The following example shows the usage of the fabs() method. Live Demo · #!/usr/bin/python3 import math # This will import math module print ("math.fabs(-45.17) : ", math.fabs(-45.17)) print ("math.fabs(100.12) : ", math.fabs(100.12)) print ("math.fabs(100.72) : ", math.fabs(100.72)) print ("math.fabs(math.pi) : ", math.fabs(math.pi))
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.fabs.html
numpy.fabs — NumPy v2.4 Manual
January 14, 2024 - Examples · Try it in your browser! >>> import numpy as np >>> np.fabs(-1) 1.0 >>> np.fabs([-1.2, 1.2]) array([ 1.2, 1.2]) Go BackOpen In Tab ·
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.fabs.html
numpy.fabs — NumPy v2.2 Manual
March 21, 2024 - Absolute values including complex types. Examples · >>> import numpy as np >>> np.fabs(-1) 1.0 >>> np.fabs([-1.2, 1.2]) array([ 1.2, 1.2]) On this page
🌐
Pythontic
pythontic.com › modules › math › fabs
Python math module - fabs function | Pythontic.com
If we draw a number line the absolute value of a number tells the magnitude of its distance from zero, without considering the direction. Hence both the numbers -6 and 6 will be at a distance of 6 from zero. ceil · comb · copysign · cos · degrees · dist · factorial · floor · fmod · ...