error in the difference between two double numbers

 

it's a simple question, try to make the difference between two double numbers and tell me if you also get an error in the result.
for example I am subtracting the current and previous value of a sma and the result is wrong.

bool isSlowSMALessThanPrevoious(){

   double actualSMAPrice = ExtFastSMABuffer[0];
   double previousSMAPrice = ExtFastSMABuffer[1];
   
   printf("previousSMAPrice=" + previousSMAPrice + " actualSMAPrice=" + actualSMAPrice + " difference=" + (previousSMAPrice - actualSMAPrice) );
   
   if( (previousSMAPrice - actualSMAPrice ) > 0.0001 )
      return true;
   else
      return false;
}
previousSMAPrice=1.126476000000007 actualSMAPrice=1.126550000000007 difference=-7.399999999990747e-05
WOW
 
Andrea Di:

it's a simple question, try to make the difference between two double numbers and tell me if you also get an error in the result.
for example I am subtracting the current and previous value of a sma and the result is wrong.

WOW
Nope it is correct:

1.126476 - 1.12655 =
-0.000074 = 
-7.4*10^-5(times ten to the negative power of five)  =   
-7.4 e-05 

 
Andrea Di:

it's a simple question, try to make the difference between two double numbers and tell me if you also get an error in the result.
for example I am subtracting the current and previous value of a sma and the result is wrong.

WOW
https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

That's how floating point works.
 
pennyhunter #:
Nope it is correct:

1.126476 - 1.12655 =
-0.000074 = 
-7.4*10^-5(times ten to the negative power of five)  =   
-7.4 e-05 

ok that's true, and works thanks!
Reason: