Home > Backend Development > C++ > In C/C++, the abs(), labs() and llabs() functions are translated as follows: The abs() function is used to return the absolute value of an integer. The labs() function is used to return the absolute value of a long integer. The llabs() function is used to return the absolute value of a long integer

In C/C++, the abs(), labs() and llabs() functions are translated as follows: The abs() function is used to return the absolute value of an integer. The labs() function is used to return the absolute value of a long integer. The llabs() function is used to return the absolute value of a long integer

WBOY
Release: 2023-08-26 13:49:02
forward
1022 people have browsed it

在C/C++中,abs()、labs()和llabs()函数的翻译如下:

abs()函数用于返回一个整数的绝对值。
labs()函数用于返回一个长整数的绝对值。
llabs()函数用于返回一个长长整数的绝对值

In the C cstdlib library, in addition to abs, there are different functions for obtaining absolute values. In C, abs is basically used for int type input, in C, for int, long, long long. Others are used for long, long long type data, etc. Let's see the usage of these functions.

abs() function

This function is used for int type data. So this returns the absolute value of the given parameter. The syntax is as follows.

int abs(int argument)
Copy after login

Example

#include <cstdlib>
#include <iomanip>
#include <iostream>
using namespace std;
main() {
   int x = -145;
   int y = 145;
   cout << "Absolute value of " << x << " is: " << abs(x) << endl;
   cout << "Absolute value of " << y << " is: " << abs(y) << endl;
}
Copy after login

Output

Absolute value of -145 is: 145
Absolute value of 145 is: 145
Copy after login

labs() function

This function is used for long type data. So this returns the absolute value of the given parameter. The syntax is as follows.

long labs(long argument)
Copy after login

Example

#include <cstdlib>
#include <iomanip>
#include <iostream>
using namespace std;
main() {
   long x = -9256847L;
   long y = 9256847L;
   cout << "Absolute value of " << x << " is: " << labs(x) << endl;
   cout << "Absolute value of " << y << " is: " << labs(y) << endl;
}
Copy after login

Output

Absolute value of -9256847 is: 9256847
Absolute value of 9256847 is: 9256847
Copy after login

llabs() function

This function is used for long long type data. So this returns the absolute value of the given parameter. The syntax is as follows.

long long labs(long long argument)
Copy after login

Example

#include <cstdlib>
#include <iomanip>
#include <iostream>
using namespace std;
main() {
   long long x = -99887654321LL;
   long long y = 99887654321LL;
   cout << "Absolute value of " << x << " is: " << llabs(x) << endl;
   cout << "Absolute value of " << y << " is: " << llabs(y) << endl;
}
Copy after login

Output

Absolute value of -99887654321 is: 99887654321
Absolute value of 99887654321 is: 99887654321
Copy after login

The above is the detailed content of In C/C++, the abs(), labs() and llabs() functions are translated as follows: The abs() function is used to return the absolute value of an integer. The labs() function is used to return the absolute value of a long integer. The llabs() function is used to return the absolute value of a long integer. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template