bubucodex
bubucodex

Reputation: 121

Difference of two variables having same values yields non-zero result

I am trying to evaluate a mathematical expression in C. The expression is a follows : enter image description here

Following is the code that I wrote to evaluate the mathematical expression :

int main()
{  
    
     double x,y,A1,B1,C1,result;

     x = 8.1500e-07;
     y = 7.9714e-08;

     A1 = (1-exp(-2*x))/(2*x);
     B1 = 2/(x*( 1 + pow((y/x),2)) );
     C1 = 1 - ( exp(-x)*( cos(y) - (y/x)*sin(y) ));
    
     result = 1 + A1 - B1*C1;
     
     printf("1 + A1 = %e\n",1+A1);

     printf("B1*C1 = %e\n",B1*C1);

     printf("result = %e\n",result);

     


}

It appears that, when separately evaluated, 1+A1 is evaluated to be equal to B1C1, but 1+A1-B1C1 turns out to be a very small negative value. Here is the result : Outcome of the C program shown above. I want to know whether I can regard this to be as some error in computation in analogy with error in experiment and safely consider the result to be zero ? Or, am I missing something ?

Upvotes: -1

Views: 127

Answers (1)

KamilCuk
KamilCuk

Reputation: 141523

That values are the same to the 6th decimal digit doesn't mean they are the same.

 printf("1 +A1 = %.20e\n",1+A1);
 printf("B1*C1 = %.20e\n",B1*C1);

prints:

1 +A1 = 1.99999918502629325801e+00
B1*C1 = 1.99999918503684992466e+00

Upvotes: 3

Related Questions