abs, _abs64
计算绝对值。
int abs(
int n
);
long abs(
long n
); // C++ only
double abs(
double n
); // C++ only
long double abs(
long double n
); // C++ only
float abs(
float n
); // C++ only
__int64 _abs64(
__int64 n
);
参数
- n
整数值。
返回值
abs 函数返回参数的绝对值。 无错误返回。
备注
由于 C++ 允许重载,可以调用 abs重载。 在 c. 程序, abs 总是采用并返回 int。
备注
abs(INT_MIN) 和 _abs64(INT_MIN) 返回 INT_MIN的值。虽然这是,唯一时候 abs 和 _abs64 返回负值,这意味着 abs 和 _abs64 不能用于确保一个正整数值。
要求
实例 |
必需的头 |
---|---|
abs |
math.h |
_abs64 |
stdlib.h |
示例
此过程计算并显示几个数字的绝对值。
// crt_abs.c
// This program demonstrates the user of the abs function
// by computing and displaying the absolute values of
// several numbers.
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main( void )
{
int ix = -4,
iy;
long lx = -41567L,
ly;
double dx = -3.141593,
dy;
__int64 wx = -1, wy;
// absolute 64 bit integer value
wy = _abs64( wx );
printf_s( "The absolute value of %I64x is %I64x\n", wx, wy);
// absolute 32 bit integer value
iy = abs( ix );
printf_s( "The absolute value of %d is %d\n", ix, iy);
// absolute long integer value
ly = labs( lx );
printf_s( "The absolute value of %ld is %ld\n", lx, ly);
// absolute double value
dy = fabs( dx );
printf_s( "The absolute value of %f is %f\n", dx, dy );
}