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永遠會接受並傳回整數的值

注意事項注意事項

Both abs(INT_MIN) and _abs64(INT_MIN) return a value of 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 );
}
  

.NET Framework 對等用法

System::Math::Abs

請參閱

參考

資料轉換

浮點支援

_cabs

fabs fabsf

labs