2015-10-17 73 views
-1

我試過基於C一些計算,我的代碼如下math.h方法工作不同?

#include <stdio.h> 
#include <math.h> 

int main(void) 
{ 
    float a = 1.8; 
    float b = 3.3; 
    float c = 1.5; 
    float d = 5.9; 
    float e = 2.6; 

    float result = a/(a + (abs(a - b)/(b + (pow(c, 5)/(e + (pow(d, 2)/(b + (pow(c, 3)/4)))))))); 
    float result2 = a/(a + ((-1) * (a-b))/(b + ((c * c * c * c * c)/(e + ((d * d)/(b + ((c * c * c)/4))))))); 
    printf("Result = %.4f\n", result); 
    printf("Result2 = %.4f\n", result2); 
    system("pause"); 
    return(0); 
} 

結果2是正確的結果。那麼爲什麼結果和結果2是不同的?我在這裏做錯了什麼?

+1

可以共享成果 – Haris

+4

'ABS(AB)'是!='(-1)*(AB)' – wrangler

+0

@wrangler A和B的當前設置,是的,它等於 –

回答

7

absint

int abs(int j); 

fabsfabsf是你應該使用什麼:

double fabs(double x); 
float fabsf(float x); 

編輯:

改變absfabs,本地測試後給出:

[[email protected] tmp]$ gcc a.c -lm -Wall -pedantic 
[[email protected] tmp]$ ./a.out 
Result = 0.8272 
Result2 = 0.8272 
+0

這似乎是正確的解決方案,謝謝 –