Download MetaTrader 5

Substraction error... Digits doesn't work right...

To add comments, please log in or register
En
33
En 2014.11.10 12:27 

Hi ;-)

Once upon a time there was a boy, who wanted to (...) substract two double's... The result didn't satisfy him, so he went on a jurney to resolve his problem.
After he became very old, he still couldn't find what he was looking for, but he entered land of MQL forum where he presented his problem:

   double a = 1.37674;
   double b = 1.3761;
   double c = a - b;
   double d = NormalizeDouble (c,Digits);
   double e = MathRound (a/Point - b/Point) * Point;
   string f = DoubleToStr ( NormalizeDouble (a-b,5), 5);
   double f1 = StrToDouble (f);
   double g = StrToDouble( DoubleToStr ( NormalizeDouble (a-b,Digits), Digits) );

   Alert ("a = ",a,", b = ",b,", c = ",c,", d = ",d,", e = ",e,", f = ",f,", f1 = ",f1,", g = ",g);

What he got in result from this secret formula was:
a = 1.37674, b = 1.3761, c = 0.0006399999999999739, d = 0.0006400000000000001, e = 0.0006400000000000001, f = 0.00064, f1 = 0.0006400000000000001, g = 0.0006400000000000001

So if anyone of You,  can tell that poor man a way for make "double result = 0.00064", he would be verry thankfull ;-)

Yours sincerely...
Nurotic old man ;-)

SYED NAUFAL GADDAFI
1401
SYED NAUFAL GADDAFI 2014.11.10 12:32  
double d = NormalizeDouble (c,5);
Never try that ?
En
33
En 2014.11.10 12:35  

I tried...

More:

   double h = NormalizeDouble( 0.0006400000000000001,5);
   double i = NormalizeDouble( 0.0006400000000000001,4);
   double j = NormalizeDouble( 0.0006400000000000001,3);
   double k = NormalizeDouble( 0.0006400000000000001,2);
   double l = NormalizeDouble( 0.0006400000000000001,1);
   double m = NormalizeDouble( 0.0006400000000000001,0);

   Alert ("h = ",h,", i = ",i,", j = ",j,", k = ",k,", l = ",l,", m = ",m);

Result:
 h = 0.0006400000000000001, i = 0.0006, j = 0.001, k = 0.0, l = 0.0, m = 0.0

So it works from 4 digits... ;-)
But why not from 5?!?!... !@#!%!#@%@^@!

SYED NAUFAL GADDAFI
1401
SYED NAUFAL GADDAFI 2014.11.10 12:42  
 double d = NormalizeDouble (a-b,5);
En
33
En 2014.11.10 12:56  
deysmacro:

NormalizeDouble (a-b,5) = 0.0006400000000000001

If You have different result please tell me which version of MetaEditor You have... 

SYED NAUFAL GADDAFI
1401
SYED NAUFAL GADDAFI 2014.11.10 13:12  
Print(1.37674-1.37611);

Try that. I got myself 0.0006

Lol. XD

En
33
En 2014.11.10 13:53  
deysmacro:

Try that. I got myself 0.0006

Lol. XD

Cool... I have 0.0006300000000001305 :D
So it's problem with my MetaEditor.

Thanks! ;-) 

P.S.

Why didn't You get  0.00063? :/
What the LatinToString ($@^@$*#%@#%$@#%@^&**!!!!!!!!!) is happening?!?

SYED NAUFAL GADDAFI
1401
SYED NAUFAL GADDAFI 2014.11.10 14:09  

To be honest, I am not so sure myself. Hopefully someone could lighten up about this.


Don't worry. I don't think it is about MetaEditor though.

graziani
1967
graziani 2014.11.10 17:21  

This happens because of the floating-point format. This problem is also visible when comparing doubles/floats.

Solution is rounding and/or normalizing and/or using epsilon value, or avoiding the problem by using integers instead of floats when possible.

It is a common problem, google it.

whroeder1
14739
whroeder1 2014.11.10 18:34  
n-v: NormalizeDouble (a-b,5) = 0.0006400000000000001
  1. Because some values (like 1./10.) can not be exactly represented Double-precision floating-point format - Wikipedia, the free encyclopedia
  2. The == operand. - MQL4 forum https://forum.mql4.com/56002#801213 The == operand. - MQL4 forum
En
33
En 2014.11.11 19:38  

Ok, I know why it is happening, but I'm asking - do You know how to make it work?
I.E.
I have 2 BID prices from time an hour earlier:
a = 1.37674;
b = 1.3761;
and I wanted to test a strategy, where I want to use that spread between a and b to use it as stop loss in new Order.
So this is why I need in this case c = 0.00064
But I tried everything that I could find and I don't have this c that is correct.

So for me now this MetaEditor is useless, because I can't make an order because of simple subtraction.
"Calculated values of StopLoss, TakeProfit, and values of open prices for pending orders must be normalized with the accuracy, the value of which can be obtained by Digits()." - https://docs.mql4.com/convert/normalizedouble
BUT I CAN'T DO THAT, so if anyone can solve this problem than please help me...

123
To add comments, please log in or register