首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::abs(int)

Defined in header <cstdlib>

?

?

Defined in header <cmath>

?

(since C++17)

int abs( int n );

?

?

long abs( long n );

?

?

long long abs( long long n );

?

(since C++11)

Defined in header <cstdlib>

?

?

long labs( long n );

?

?

long long llabs( long long n );

?

(since C++11)

Defined in header <cinttypes>

?

?

std::intmax_t abs( std::intmax_t n );

?

(since C++11)

std::intmax_t imaxabs( std::intmax_t n );

?

(since C++11)

计算整数的绝对值。如果返回类型不能表示结果,则行为未定义。

参数

n

-

integer value

返回值

绝对值n%28i.e.|n|%29,如果它是可代表的。

注记

在2%27 s补码系统中,最大负值的绝对值超出了范围,例如32位2%27 s补码类型int,int。[医]最小值为-2147483648,但预期结果2147483648大于int。[医]最高值是2147483647。

二次

代码语言:javascript
复制
#include <iostream>
#include <cstdlib>
#include <climits>
 
int main()
{
    std::cout << "abs(+3) = " << std::abs(3) << '\n'
              << "abs(-3) = " << std::abs(-3) << '\n';
 
//  std::cout << abs(INT_MIN)); // undefined behavior on 2's complement systems
}

二次

产出:

二次

代码语言:javascript
复制
abs(+3) = 3
abs(-3) = 3

二次

另见

abs(float) fabs

absolute value of a floating point value (|x|) (function)

abs(std::complex)

returns the magnitude of a complex number (function template)

abs(std::valarray)

applies the function std::abs to each element of valarray (function template)

为abs,实验室,llab编写文档

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com