tags:

views:

125

answers:

1

I assume that abs and fabs are behaving different when using math.h. But when I use just cmath and std::abs, do I have to use std::fabs? or fabs? Or isn't this defined?

+13  A: 

In C++, it's always sufficient to use std::abs; it's overloaded for all the numerical types.

In C, abs only works on integers, and you need fabs for floating point values. These are available in C++ (along with all of the C library), but there's no need to use them.

Mike Seymour
Is this on every platform the case? Esp. Windows and Mac OS X? Or is it at least in the C++ standard?
brubelsabs
@brubelsabs: yes. There is no need for a separate fabs function in C++ since C++ has function overloading (abs can be defined for numerous types and it is in C++). It is also guaranteed by the standard. Of course if you dig around find some outdated compiler over 10 years old, you might find one that doesn't support it.
It's in the C++ Standard, so it's the case on every platform with a decent compiler, including Windows and Mac OS X. Clause 26.5 says that, in addition to the `int` version from the C library, there are overloads for `long`, `float`, `double` and `long double`. Clause 26.2.7 also defines an overload for `complex`.
Mike Seymour