-1

I've tried some calculations on c, my code is below

#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);
}

Result2 is the correct result. So why result and result2 are different? What am i doing is wrong here?

jbm
  • 3,063
  • 1
  • 16
  • 25
Cem Sönmez
  • 506
  • 1
  • 3
  • 15

1 Answers1

7

abs is for int:

int abs(int j);

while fabs or fabsf is what you shall use:

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

EDITED:

After changing abs to fabs, a local test gives:

[pengyu@GLaDOS tmp]$ gcc a.c -lm -Wall -pedantic
[pengyu@GLaDOS tmp]$ ./a.out 
Result  = 0.8272
Result2 = 0.8272
starrify
  • 14,307
  • 5
  • 33
  • 50