Compartir a través de


abs, _abs64

Calcula el valor absoluto.

int abs( 
   int 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
__int64 _abs64( 
   __int64 n 
);

Parámetros

  • n
    Valor entero.

Valor devuelto

La función abs devuelve el valor absoluto de su parámetro. No se devuelve ningún error.

Comentarios

Como C++ permite las sobrecargas, puede llamar a las sobrecargas de abs que toman y devuelven los valores long, long long, float, double, y long double. En un programa C, abs siempre y devuelve un int.

Específicos de Microsoft

Puesto que el intervalo de enteros negativos que puede representarse mediante cualquier tipo entero es mayor que el intervalo de enteros positivos que se pueden representar usando ese tipo, es posible proporcionar un argumento a abs que no se pueda convertir. Si el valor absoluto del argumento no lo puede representar el tipo de valor devuelto, las funciones abs devuelven el valor del argumento sin modificar. Específicamente, abs(INT_MIN) devuelve INT_MIN, labs(LONG_MIN) devuelve LONG_MIN, llabs(LLONG_MIN) devuelve LLONG_MIN y _abs64(_I64_MIN) devuelve _I64_MIN. Esto significa que las funciones abs no se pueden utilizar para garantizar un valor positivo.

Fin de Específicos de Microsoft

Requisitos

Rutina

Encabezado necesario

abs

<math.h>

_abs64

<stdlib.h>

Ejemplo

Este programa calcula y muestra los valores absolutos de varios 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));
}
  

Equivalente en .NET Framework

System::Math::Abs

Vea también

Referencia

Conversión de datos

Compatibilidad con el punto flotante

_cabs

fabs, fabsf

labs, llabs

imaxabs