🌐
Tutorialspoint
tutorialspoint.com › python › number_fabs.htm
Python math.fabs() Method
Therefore, 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. import math # Create negative Integer and Float objects inr = -34 flt = -154.32 # Calculate the absolute values of the objects abs_int = math.fabs(inr) abs_flt = math.fabs(flt) # Print the values print("Absolute Value of an Integer:", abs_int) print("Absolute Value of an Float:", abs_flt)
🌐
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 »
🌐
Python
docs.python.org › 3 › library › math.html
math — Mathematical functions
1 month ago - math.fabs(x)¶ · Return the absolute value of x. math.floor(x)¶ · Return the floor of x, the largest integer less than or equal to x. If x is not a float, delegates to x.__floor__, which should return an Integral value. math.fma(x, y, z)¶ ...
🌐
Note.nkmk.me
note.nkmk.me › home › python
Get Absolute Values in Python: abs(), math.fabs() | note.nkmk.me
May 11, 2025 - NumPy: Calculate the absolute value element-wise (np.abs, np.fabs) math.fabs() also returns the absolute value, but unlike abs(), it always returns a floating-point number (float).
🌐
Codecademy
codecademy.com › docs › python › math module › math.fabs()
Python | Math Module | math.fabs() | Codecademy
September 11, 2024 - 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. Passing any non-numeric data type will raise a TypeError.
🌐
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 "math" for mathematical operations import math x = -33.7 # returning the fabs of 33.7 print ("The fabs of 33.7 is : ", end ="") print (math.fabs(x))
🌐
Tutorial Gateway
tutorialgateway.org › python-fabs
Python fabs Function
March 26, 2025 - Within the last statement, we tried the fabs Function on the String value, and it returns TypeError. 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'))
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().

🌐
Python Examples
pythonexamples.org › python-math-fabs-absolute-value
Python math.fabs() - Absolute Value
Discover how to use the math.fabs() function in Python to calculate the absolute value of a number. This tutorial covers the syntax, parameters, and examples, including calculations for integers, floating-point numbers, and even negative infinity.
Find elsewhere
🌐
Learn Coding Fast
learncodingfast.com › home › python fabs() function
Python fabs() function | Learn Coding Fast
September 1, 2020 - In the code above, we first import the math module on line 1. Next, we define a function called myFabs() that has one parameter – myList. Inside the function, we first declare a variable called result and assign an empty list to it. Next, we use a for loop to loop through myList. Inside the for loop, we use a try-except statement. Inside the try block, we try to pass i as an argument to the fabs() function.
🌐
Server Academy
serveracademy.com › blog › python-absolute-value-abs-tutorial
Python Absolute Value Abs() Tutorial - Server Academy
November 12, 2024 - Here, abs() calculates the magnitude ... values. While abs() handles most cases, math.fabs() specifically returns the absolute value of a floating-point number....
🌐
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.
🌐
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 ...
🌐
Python FAQ
pythonfaq.net › home › how to calculate absolute values with math.fabs in python
How to calculate absolute values with math.fabs in Python
September 19, 2025 - Calculate absolute values in Python using math.fabs for float outputs, handle types with type checks, and manage precision for Decimals and numpy arrays.
🌐
Pythontic
pythontic.com › modules › math › fabs
Python math module - fabs function | Pythontic.com
fabs(aNumber_in) Returns the absolute value of a Number. 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 · ...
🌐
GeeksforGeeks
geeksforgeeks.org › python-fabs-vs-abs
Python | fabs() vs abs() - GeeksforGeeks
August 9, 2021 - # Python code to demonstrate working # of fabs() and abs() import math ################################# # When the argument is an integer# ################################# number = -10 # abs() will return an integer as # the argument is an integer print(abs(number)) # fabs() will return a floating point number print(math.fabs(number)) ########################################### # When the input is a floating point number# ########################################### number = -12.08 # abs() will return an floating point number # as the argument is a floating point number print(abs(number)) # f
🌐
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 »
🌐
Interactive Chaos
interactivechaos.com › en › python › function › mathfabs
math.fabs | Interactive Chaos
January 20, 2021 - The math.fabs function returns the absolute value of a number, which must be an integer or real (complex numbers are not supported).
🌐
GeeksforGeeks
geeksforgeeks.org › python-number-fabs-method
fabs() method of Python’s math Library | GeeksforGeeks
October 23, 2020 - The error on passing string is :") print(str(e)) print("\r") try : print("The absolute value of complex is :") print(math.fabs(d)) except Exception as e: print("Error !! The error on passing complex is :") print(str(e)) Output : The absolute value of string is : Error !! The error on passing string is : a float is required The absolute value of complex is : Error !! The error on passing complex is : can't convert complex to float ... Python has math library and has many functions regarding it. One such function is exp(). This method is used to calculate the power of e i.e.
🌐
KooR.fr
koor.fr › Python › API › python › math › fabs.wp
KooR.fr - Fonction fabs - module math - Description de quelques librairies Python
Machine Learning avec Scikit-Learn Voir le programme détaillé Module « math » Python 3.13.2 · def fabs(value: int|float) -> float · La fonction calcule la valeur absolue du nombre passé en argument (entier ou flottant).