fabs versus abs?

I would like to know why there's 3 math functions to calculate the absolute value of a number: fabs(), labs() and abs(), this last one is for integral types. I think this is a completely stupid thing. It doesn't matter what kind of number we have, the steps to achieve the absolute value are the same, I must say. Well, if programming in C++, someone says, it's more difficult than programming in other high level languages, one of the reason is this, it provides many unnecessary things.

Do you agree?
Last edited on
C++ provides overload of abs() for all arithmetic types.

fabs() and labs() are provided for backward compatibility with C, which does not have function overloading and had to have different fucntions for different types.

So, use abs() and forget about others

EDIT: you also forgot llabs() and imaxabs()
the steps to achieve the absolute value are the same
Actually, no. For floating point types it is usually something like:
1
2
3
4
float fabs(float x) //assuming 32bit single precision IEEE 754 float
{
    return x & 0x7FFFFFFF;
}
When for integrals it is:
1
2
3
4
int abs(int x) //Assuming 32bit 2s complement number
{
    return ~--x;
}
Last edited on
Topic archived. No new replies allowed.