Python - abs vs fabs

Python

Python Problem Overview


I noticed that in python there are two similar looking methods for finding the absolute value of a number:

First

abs(-5)

Second

import math
math.fabs(-5)

How do these methods differ?

Python Solutions


Solution 1 - Python

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

Solution 2 - Python

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().

Solution 3 - Python

math.fabs() always returns float, while abs() may return integer.

Solution 4 - Python

abs() : Returns the absolute value as per the argument i.e. if argument is int then it returns int, if argument is float it returns float. Also it works on complex variable also i.e. abs(a+bj) also works and returns absolute value i.e.math.sqrt(((a)**2)+((b)**2)

math.fabs() : It only works on the integer or float values. Always returns the absolute float value no matter what is the argument type(except for the complex numbers).

Solution 5 - Python

The difference between abs() and fabs() is:

abs(): Calculate the absolute value element-wise. But the element is typed(int) then it will return type(int) and if the argument is typed float it will return typed(float). Simple whatever you passed it will return the same type of data. > abs() method also work with complex data

For Example:

x = np.array([-12,-19,4])
abs_x = np.abs(x) 
print(abs_x)    #output: [12 19 4]

fabs(): Calculate the absolute value element-wise. But this method returns the value always in float. > So the main difference between abs() and fabs() is: > > fabs() always return float and fabs() cannot handled complex value.

For Example:

x = np.array([-12,-19,4])
abs_x = np.fabs(x) 
print(abs_x)  #output: [12.0 19.0  4.0]

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMateusz JagiełłoView Question on Stackoverflow
Solution 1 - PythonNPEView Answer on Stackoverflow
Solution 2 - PythonK ZView Answer on Stackoverflow
Solution 3 - PythonTadeckView Answer on Stackoverflow
Solution 4 - PythonRahul TaloleView Answer on Stackoverflow
Solution 5 - PythonHaroon HayatView Answer on Stackoverflow