標準/拡張機能 | C/C++ | 依存項目 |
---|---|---|
ISO C |
両方 |
#include <stdlib.h>
int abs(int n);
long abs(long n); /* C++ only */
#include <math.h>
double abs(double n); /* C++ only */
float abs(float n); /* C++ only */
long double abs(long double n); /* C++ only */
float absf(float n);
long double absl(long double n);
#define __STDC_WANT_DEC_FP__
#include <math.h>
_Decimal32 abs(_Decimal32 x); /* C++ only */
_Decimal64 abs(_Decimal64 x); /* C++ only */
_Decimal128 abs(_Decimal128 x); /* C++ only */
#define _TR1_C99
#include <inttypes.h>
intmax_t abs(intmax_t n);
#define _TR1_C99
#include <stdlib.h>
long long abs(long long n);
関数 abs()、absf()、および absl() は、引数 n の絶対値を戻します。
int バージョンの abs() の場合、使用できる最小の整数は INT_MIN+1 です (INT_MIN は、limits.h ヘッダー・ファイルに定義されているマクロです)。例えば、z/OS® XL C/C++ コンパイラーの場合、INT_MIN+1 は -2147483648 です。
double、float、および long double バージョンの abs() の場合、使用できる最小の値は、それぞれ DBL_MIN+1、FLT_MIN+1、および LDBL_MIN+1 です (浮動小数点マクロ定数は、float.h ヘッダー・ファイルに定義されています)。
入力した値を整数として表せない場合、abs()、absf()、および absl() 関数は、入力した値と同じ値を戻します。
C++ の特殊な動作: C++ アプリケーションの場合、abs() は、long、float、および long double 型についても多重定義されます。
絶対値を表すことができる場合、戻り値は絶対値です。
表すことができない場合、入力値を戻します。
errno 値は定義されていません。
⁄* CELEBA02
This example calculates the absolute value of an integer
x and assigns it to y.
*⁄
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int x = -4, y;
y = abs(x);
printf("The absolute value of %d is %d.¥n", x, y);
}
The absolute value of -4 is 4.