I ran some simple tests on abs() and fabs() functions and I don't understand what are the advantages of using fabs(), if it is:
1) slower
2) works only on floats
3) will throw an exception if used on a different type
In [1]: %timeit abs(5)
10000000 loops, best of 3: 86.5 ns per loop
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
it's even slower on floats!
From where I am standing the only advantage of using fabs() is to make your code more readable, because by using it, you are clearly stating your intention of working with float/double point values
Is there any other use of fabs()?
From an email response from Tim Peters:
It seems there is no advantageous reason to use
fabs
. Just useabs
for virtually all purposes.