Nota
O acesso a esta página requer autorização. Podes tentar iniciar sessão ou mudar de diretório.
O acesso a esta página requer autorização. Podes tentar mudar de diretório.
Calcula o valor absoluto do argumento.
Sintaxe
int abs( int n );
long labs( long n );
long long llabs( long long n );
__int64 _abs64( __int64 n );
long abs( long n ); // C++ only
long long abs( long long n ); // C++ only
double abs( double n ); // C++ only
long double abs( long double n ); // C++ only
float abs( float n ); // C++ only
Parâmetros
n
Valor numérico.
Valor retornado
As funções abs, labs, llabs e _abs64 retornam o valor absoluto do parâmetro n. Não há retorno de erro.
Comentários
Como C++ permite sobrecarga, é possível chamar sobrecargas de abs que usam e retornam valores long, long long, float, double e long double. Essas sobrecargas são definidas no cabeçalho <cmath>. Em um programa C, abs sempre toma e retorna um int.
Específico da Microsoft: o intervalo de inteiros negativos representáveis em qualquer tipo integral é maior do que o intervalo de inteiros positivos representáveis nesse tipo. Portanto, é possível fornecer um argumento para essas funções que não podem ser convertidas. Se o valor absoluto do argumento não puder ser representado pelo tipo de retorno, as abs funções retornarão o valor do argumento inalterado. Especificamente, abs(INT_MIN) retorna INT_MIN, labs(LONG_MIN) retorna LONG_MIN, llabs(LLONG_MIN) retorna LLONG_MIN e _abs64(_I64_MIN) retorna _I64_MIN. Efetivamente, as abs funções não podem ser usadas para garantir um valor positivo.
Requisitos
| Rotina | Cabeçalho C necessário | Cabeçalho C++ necessário |
|---|---|---|
abs, labs, llabs |
<math.h> ou <stdlib.h> |
<cmath>, <cstdlib>, <stdlib.h> ou <math.h> |
_abs64 |
<stdlib.h> |
<cstdlib> ou <stdlib.h> |
Para usar as versões sobrecarregadas do abs no C++, você deve incluir o cabeçalho <cmath>.
Exemplo
O programa calcula e exibe os valores absolutos de vários números.
// crt_abs.c
// Build: cl /W3 /TC crt_abs.c
// This program demonstrates the use of the abs function
// by computing and displaying the absolute values of
// several numbers.
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <limits.h>
int main( void )
{
int ix = -4;
long lx = -41567L;
long long llx = -9876543210LL;
__int64 wx = -1;
// absolute 32 bit integer value
printf_s("The absolute value of %d is %d\n", ix, abs(ix));
// absolute long integer value
printf_s("The absolute value of %ld is %ld\n", lx, labs(lx));
// absolute long long integer value
printf_s("The absolute value of %lld is %lld\n", llx, llabs(llx));
// absolute 64 bit integer value
printf_s("The absolute value of 0x%.16I64x is 0x%.16I64x\n", wx,
_abs64(wx));
// Integer error cases:
printf_s("Microsoft implementation-specific results:\n");
printf_s(" abs(INT_MIN) returns %d\n", abs(INT_MIN));
printf_s(" labs(LONG_MIN) returns %ld\n", labs(LONG_MIN));
printf_s(" llabs(LLONG_MIN) returns %lld\n", llabs(LLONG_MIN));
printf_s(" _abs64(_I64_MIN) returns 0x%.16I64x\n", _abs64(_I64_MIN));
}
The absolute value of -4 is 4
The absolute value of -41567 is 41567
The absolute value of -9876543210 is 9876543210
The absolute value of 0xffffffffffffffff is 0x0000000000000001
Microsoft implementation-specific results:
abs(INT_MIN) returns -2147483648
labs(LONG_MIN) returns -2147483648
llabs(LLONG_MIN) returns -9223372036854775808
_abs64(_I64_MIN) returns 0x8000000000000000
Confira também
Conversão de dados
Suporte matemático e de ponto flutuante
_cabs
fabs, fabsf, fabsl
imaxabs