1

I would like to know the difference of abs,labs and fabs. This is under the cstdlib header file. So, I got confused to use these words in cpp programming.

2 Answers2

1
int abs(int n)

This function returns the absolute value of n being n an int variable.

long int labs(long int n)

This function returns the absolute value of parameter n as long int type instead of int.

float fabs(float n)

This function returns the absolute value of parameter n as float type variable.

There are still these overloads for double and long double types:

double fabs (double x); //for double variables
long double fabs (long double x); //for long double variables

For these overloads the compiler will choose the correct version deppending on the parameter type.

The difference is clear, you should use the correct version of abs() deppending on the parameter variable type you are using, the wrong usage may result in undefined behavior namely due to signed integer overflow.

anastaciu
  • 23,467
  • 7
  • 28
  • 53
1

the only difference is the return type and the parameter type. abs() takes an integer as a parameter and also return an integer, fabs() takes and returns a floating point type like a float or double labs() takes and returns a long int. So simply use the appropriate one for the data you're dealing with. check this link https://learntechway.com/abs-fabs-labs-in-c/