天天看点

绝对值函数abs、fabs等的使用

求数字的绝对值,vc++提供的库函数的支持,当必须包含: #include <math.h>

其中又分好几种类型:abs、_abs64、fabs、fabsf、labs、_cabs。详细说明如下:      
//Calculate the absolute value.      
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 
);      
//Calculates the absolute value of the floating-point argument.      
double fabs( 
   double x 
);
float fabs(
   float x 
); // C++ only
long double fabs(
   long double x
); // C++ only
float fabsf( 
   float x 
);      
//Calculates the absolute value of a long integer.      
long labs(
      long n 
);      
//Calculates the absolute value of a complex number.
      
double _cabs( 
   struct _complex z 
);      
以上函数的原型说明来自MSDN2008,可以看出,ads()函数有很多重载形式。一般用ads()就可以满足要求(c++),其它的各种都是一些特例。
不嫌麻烦的也可以采用模板的形式来重新定义。      

继续阅读