Using fabs for double and integers.

Jan 11, 2013 at 8:37am
Hello,

I was trying to create a generic class and I wanted to know if the fabs() can be applied on both floating point and integer values? Please advise.

Thanks.
Jan 11, 2013 at 9:58am
In short: it can. When you pass integers to fabs() they will be promoted to double.
Jan 11, 2013 at 1:27pm
C doesn't have overloading so they created different functions to work with all types, abs, labs, fabs, fabsf, etc.

C++ has added overloads to abs and fabs to make it work with all integer and floating point types. Before C++11 there was no integer overloads of fabs so if you tried to pass an integer to it it would give you an error.
Last edited on Jan 11, 2013 at 1:28pm
Jan 11, 2013 at 2:41pm
it would give you an error.

It won't
http://www.learncpp.com/cpp-tutorial/44-type-conversion-and-casting/

I just tried it on not supporting C++11 compiler. Works fine.
Jan 11, 2013 at 2:53pm
The problem is that if you have an overloaded function and you pass a value that doesn't match any of the functions exactly but could implicitly be converted to match more than one of the functions. In that case it doesn't know which function to choose so it gives an error. Some compilers added extra fabs overloads earlier but it was not standard until C++11.
Topic archived. No new replies allowed.