abs() vs fabs() speed difference and advantage of fabs() abs() vs fabs() speed difference and advantage of fabs() python python

abs() vs fabs() speed difference and advantage of fabs()


From an email response from Tim Peters:

Why does math have an fabs function? Both it and the abs builtin function wind up calling fabs() for floats. abs() is faster to boot.

Nothing deep -- the math module supplies everything in C89's standard libm (+ a few extensions), fabs() is a std C89 libm function.

There isn't a clear (to me) reason why one would be faster than the other; sounds accidental; math.fabs() could certainly be made faster (as currently implemented (via math_1), it endures a pile of general-purpose "try to guess whether libm should have set errno" boilerplate that's wasted (there are no domain or range errors possible for fabs())).

It seems there is no advantageous reason to use fabs. Just use abs for virtually all purposes.