C++ cstdlib labs and llabs


The C++ &lt’;cstdlib> labs and llabs compute the absolute value of the argument passed.The declaration of the function is given below.

1 long int labs( long int j );
2 long long int llabs( long long int j );

labs
Parameters:
j – The value to be computed to absolute value.

Return type
long int -The absolute value of the argument.

llabs
Parameters:
long long -The value to be computed to absolute value.

Return type
long long int-The absolute value of the argument passed.

Some points to note:

i) Use ‘llabs’ to support larger values for the return type.

ii) If the return value is not representable by the return type the output is undefined.

Code example

cout<< “labs( -4567 )=” << labs( -4567 ) << endl ;

cout<< “labs( -9288327278238328 )=” << labs( -9288327278238328 ) << endl ; ///Output is undefined

cout<< “llabs( -9288327278238328 )=” << llabs( -9288327278238328 ) << endl ; ///Output is valid

cout<< “llabs( -999999999998839999239999231999999 )=” << llabs( -999999999998839999239999231999999 ) << endl ; ///Output is undefined

Output

labs( -4567 )=4567
labs( -9288327278238328 )=939137656 (undefined )
llabs( -9288327278238328 )= -9288327278238328
llabs( -999999999998839999239999231999999 )= 6233766678070673407 (undefined )