
在c++的cstdlib库中,除了abs之外,还有不同的获取绝对值的函数。在 c 中,abs 基本上用于 int 类型输入,在 c++ 中,用于 int、long、long long。其他的用于long、long long类型数据等。让我们看看这些函数的用法。
abs()函数
该函数用于int类型数据。所以这返回给定参数的绝对值。语法如下。
int abs(int argument)
示例
#include #include #include 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 #include #include 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 #include #include 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()函数用于返回一个长长整数的绝对值的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1443417.html
微信扫一扫
支付宝扫一扫