Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - abs vs fabs

Tags:

python

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?

like image 246
Mateusz Jagiełło Avatar asked May 27 '12 07:05

Mateusz Jagiełło


People also ask

Is Python slow on abs?

abs() is slow compared to the manual formula.

What does fabs () do in Python?

fabs() method returns the absolute value of a number, as a float. Absolute denotes a non-negative number. This removes the negative sign of the value if it has any. Unlike Python abs(), this method always converts the value to a float value.

What does abs () mean in Python?

Python abs() Function The abs() function returns the absolute value of the specified number.

Does abs work with floats in Python?

For integer numbers, the abs() function returns the absolute value of the given number. For floating point numbers, the abs() function returns the absolute value of the given number.


2 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 
like image 163
NPE Avatar answered Oct 16 '22 03:10

NPE


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

like image 38
K Z Avatar answered Oct 16 '22 03:10

K Z