1. 程式人生 > 程式設計 >c語言中abs()和fabs()的區別點整理

c語言中abs()和fabs()的區別點整理

(1)abs()是對整數取絕對值,而fabs()是對浮點數取絕對值。

(2)函式原型:

int abs(int x)
double fabs(double x)

(3)標頭檔案:

abs(): #include <stdlib.h>
fabs(): #include <math.h>

c語言fabs是什麼意思?

fabs函式是一個求絕對值的函式,求出x的絕對值,和數學上的概念相同,函式原型是extern float fabs(float x),用法是#include <math.h>。

fabs()函式的宣告:double fabs(double x)。其中引數x 是浮點值,這個函式返回x的絕對值。程式碼示例如下:

int main (){

int a,b;

a = 1234;

b = -344;

printf("The absolute value of %d is %lf",a,fabs(a));

printf("The absolute value of %d is %lf",b,fabs(b));

return(0);}

編譯和執行上面的程式,這將產生以下結果:

The absolute value of 1234 is 1234.000000

The absolute value of -344 is 344.000000

以上就是我們小編整理了多篇後的內容,希望能夠幫助到大家。