Open In App

abs(), labs(), llabs() functions in C/C++

Last Updated : 08 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The std::abs(), std::labs() and std::llabs() in C++ are built-in functions that are used to find the absolute value of any number that is given as the argument. Absolute value is the value of a number without any sign. These functions are defined inside the <cmath> header file.

In this article, we will learn how to use std::abs(), std::labs() and std::llabs() in C++.

std::abs()

The std::abs() in C++ is used to find the absolute value of given number. The value returned by std::abs() function is of type int.

Syntax

std::abs(num)

Parameters

  • num: Number whose absolute value we have to find.

Return Value

  • Returns the absolute value of given number as integer.

Example of std::abs()

C++
// C++ program to demonstrate the use of
// std::abs() function
#include <bits/stdc++.h>
using namespace std;

int main() {

    // Finding the absolute value of
    // given integers
    cout << "abs(22) = " << abs(22)
    << endl;
    cout << "abs(-43) = " << abs(-43)
    << endl;

    return 0;
}

Output
abs(22) = 22
abs(-43) = 43

Time Complexity: O(1)
Auxiliary Space: O(1)

Problem with Absolute Values in C++

In C++, integer type ranges from INT_MIN = 2147483647 (considering 32-bit compiler) to INT_MAX = 2147483647. As we can see, converting the INT_MIN to its absolute value will result in a value larger than the max value of the int type. So, it will be returned to its 2's complement. This happens with every integer type whether it's a long int or a long long int. So, it is recommended to check for this case when determining the absolute values for ints.

std::labs()

The std::labs() in C++ is also used to find the absolute value of given number similar to abs() function but it returns the absolute value as long int as compared to int for abs() function.

Syntax

std::labs(num)

Parameters

  • num: Number whose absolute value we have to find.

Return Value

  • Returns the absolute value of given number as long integer.

Example of std::labs()

C++
// C++ program to demonstrate the use of
// std::abs() function
#include <bits/stdc++.h>
using namespace std;

int main() {

    // Finding the absolute value of given
    // long values
  	cout << "labs(227787989) = "
  	<< labs(27787989L) << endl;

    cout << "labs(-438989898) = "
    << labs(-438989898L) << endl;

    return 0;
}

Output
labs(227787989) = 27787989
labs(-438989898) = 438989898

Time Complexity: O(1)
Auxiliary Space: O(1)

std::llabs()

The std::llabs() in C++ does the same as abs() and labs() function. It returns the absolute value of given number but in the form of long long integer.

Syntax

llabs(num)

Parameters

  • num: Number whose absolute value we have to find.

Return Value

  • Returns the absolute value of given number as long long integer.

Example of std::llabs()

C++
// C++ program to demonstrate the use of
// std::llabs() function
#include <bits/stdc++.h>
using namespace std;

int main() {

    // Finding the absolute value of given
    // long long values
    cout << "llabs(22778798908) = "
    << llabs(22778798908LL)  << endl;
    cout << "llabs(-43898989878) = "
    << llabs(-43898989878LL)  << endl;

    return 0;
}

Output
llabs(22778798908) = 22778798908
llabs(-43898989878) = 43898989878

Time Complexity: O(1)
Auxiliary Space: O(1)


Next Article

Similar Reads