Python – abs vs fabs
Question:
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?
Answers:
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
math.fabs()
always returns float, while abs()
may return integer.
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()
.
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).
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]
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?
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
math.fabs()
always returns float, while abs()
may return integer.
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()
.
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).
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()
andfabs()
is:
fabs()
always return float andfabs()
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]