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
Python Examples Python Compiler ... print(math.fabs(-7)) Try it Yourself » · The math.fabs() method returns the absolute value of a number, as a float....
🌐
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-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))
🌐
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....
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().

🌐
Tutorial Gateway
tutorialgateway.org › python-fabs
Python fabs Function
March 26, 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 value of Tuple Item = %.2f' %math.fabs(Tup[3])) print('Absolute value of List Item = %.2f' %math.fabs(Lis[2])) print('Absolute value of Multiple Number = %.2f' %math.fabs(10 + 20 - 40)) print('Absolute value of String Number = ', math.fabs('Hello'))
🌐
Python
docs.python.org › 3 › library › math.html
math — Mathematical functions
1 month ago - This module provides access to common mathematical functions and constants, including those defined by the C standard. These functions cannot be used with complex numbers; use the functions of the ...
🌐
GeeksforGeeks
geeksforgeeks.org › python-fabs-vs-abs
Python | fabs() vs abs() - GeeksforGeeks
August 9, 2021 - Python | fabs() vs abs() Last Updated : 09 Aug, 2021 · Comments · Improve · Suggest changes · Like Article · Like · Report · Both the abs() and the fabs() function is used to find the absolute value of a number, i.e., remove the negative ...
Find elsewhere
🌐
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.
🌐
W3Schools
w3schools.com › c › ref_math_fabs.php
C Math fabs() Function
The fabs() function is defined in the <math.h> header file. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com · HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
🌐
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 ...
🌐
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.
🌐
Learn Coding Fast
learncodingfast.com › home › python fabs() function
Python fabs() function | Learn Coding Fast
September 1, 2020 - Here are some topics we’ll cover in today’s post: ... The fabs() function is a built-in function in the Python math module.
🌐
Narkive
cython-users.narkive.com › syBP6PfX › how-can-i-use-fabs-from-math-h-in-my-cython-code
How can I use fabs from math.h in my cython code.
Hmm, right, math.pxd is actually lacking a lot of declarations: http://en.wikipedia.org/wiki/Math.h · Post by Vishal To do this I tried to add the signature of this function to math.pxd in libc folder of Cython, as th last indented line of the pxd file, double fabs(double x) Sure, should work.
🌐
Python Examples
pythonexamples.org › python-math-fabs-absolute-value
Python math.fabs() - Absolute Value
Hyperbolic Functions · Python Math sinh() Python Math cosh() Python Math tanh() Python Math asinh() Python Math acosh() Python Math atanh() math.fabs(x) function returns the absolute value of x. The syntax to call fabs() function is · math.fabs(x) where · In the following program, we find ...
🌐
GeeksforGeeks
geeksforgeeks.org › python-number-fabs-method
fabs() method of Python’s math Library | GeeksforGeeks
October 23, 2020 - Syntax : fabs(x) Parameters : x : Number whose magnitude has to be computed. 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.
🌐
Cach3
w3schools.com.cach3.com › python › ref_math_fabs.asp.html
Python math.fabs() Method - W3Schools
#Import math Library import math #Remove - sign of given number print(math.fabs(-66.43)) print(math.fabs(-7)) Try it Yourself » · 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.
🌐
Interactive Chaos
interactivechaos.com › en › python › function › mathfabs
math.fabs | Interactive Chaos
January 20, 2021 - Home > Educational Resources > ... · math · Syntax · math.fabs(number) Description · The math.fabs function returns the absolute value of a number, which must be an integer or real (complex numbers are not supported)....
🌐
Google Groups
groups.google.com › g › cython-users › c › HvxE6X08Nh4
How can I use fabs from math.h in my cython code.
February 18, 2011 - Hmm, right, math.pxd is actually lacking a lot of declarations: ... > To do this I tried to add the signature of this function to math.pxd > in libc folder of Cython, as th last indented line of the pxd file, > like so: > > double fabs(double x)