在c++的cstdlib库中,除了abs之外,还有不同的获取绝对值的函数。在 c 中,abs 基本上用于 int 类型输入,在 c++ 中,用于 int、long、long long。其他的用于long、long long类型数据等。让我们看看这些函数的用法。
abs()函数该函数用于int类型数据。所以这返回给定参数的绝对值。语法如下。
int abs(int argument)
示例#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;}
输出absolute value of -145 is: 145absolute value of 145 is: 145
labs() 函数该函数用于长类型数据。所以这返回给定参数的绝对值。语法如下。
long labs(long argument)
示例#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;}
输出absolute value of -9256847 is: 9256847absolute value of 9256847 is: 9256847
llabs()函数该函数用于long long类型数据。所以这返回给定参数的绝对值。语法如下。
long long labs(long long argument)
示例#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;}
输出absolute value of -99887654321 is: 99887654321absolute value of 99887654321 is: 99887654321
以上就是在c/c++中,abs()、labs()和llabs()函数的翻译如下:abs()函数用于返回一个整数的绝对值。labs()函数用于返回一个长整数的绝对值。llabs()函数用于返回一个长长整数的绝对值的详细内容。