Examples: Working with Doubles in MQL4

 

New article Working with Doubles in MQL4 has been published:

In this note we will consider a typical programming errors, that occurs while working with double numbers in MQL4 programs.

Author: MetaQuotes Software Corp.

 

Great article! I was getting a lot of error code # 1's in my OrderModify calls for trailing stops. I kinda figured it was due to calling the function with no change in the parameters, but wasn't sure how to fix it. Using NormalizeDouble in my check for trailing stops took care of the problem.


Also was getting error code 4107 from my OrderSend function calls when using Strategy Tester. It seems Strategy Tester is using historical values at 5 decimal points, when I'm testing a currency pair with 4 decimal points. Using NormalizeDouble(Ask,Digits), or NormalizeDouble(Bid,Digits) in my OrderSend function took care of the error. Cool!

 

I am new to mql programming and the issue 1 discussed here( Verifying the numerical numbers) was a show stopper for me. Lucky I saw this within 6 months after starting mql programming. This I hope is sorted in mql5 and never heard of in any other high level languages that I am familiar with.

 
forexrun:

I am new to mql programming and the issue 1 discussed here( Verifying the numerical numbers) was a show stopper for me. Lucky I saw this within 6 months after starting mql programming. This I hope is sorted in mql5 and never heard of in any other high level languages that I am familiar with.

The same is true in all programming languages which are using double data type.
 
Hello and thanks for all this information,

I am a big problem now, since it had never required

I need to work and pass the result in a "double" a "string", but this number is 106 decimal, need them all, as I do?

put the link of what I put in the forum, where I explain what I need, thank you very much aid they can offer me

 https://www.mql5.com/en/forum/160483 


I hope you can help me. 

 
abcmql4:
Hello and thanks for all this information,

I am a big problem now, since it had never required

I need to work and pass the result in a "double" a "string", but this number is 106 decimal, need them all, as I do?

put the link of what I put in the forum, where I explain what I need, thank you very much aid they can offer me

 https://www.mql5.com/en/forum/160483 


I hope you can help me. 

You can not do that that way (double has 15 significant digits)

If you need only the string you can declare it as a string right away and then you will not lose information - like this :

   string SE = "result  = 1.23456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678";      

But any conversion to and from a double will leave you with only 15 accurate digits result

You can also check this : https://en.wikipedia.org/wiki/List_of_arbitrary-precision_arithmetic_software  for arbitrary precision arithmetic libraries and software capable of doing it

 

The function double MathModCorrect(double a, double b) generates an error if the "a" and "b" parameters are below one (1). To be able to work with numbers below one (1) you can use the following function:

double MathModCorrect(double a, double b)
  {
   int i = 1;
   double El_Menor;
   if(a > b)
      El_Menor = b;
   else
      El_Menor = a;
   while(!El_Menor == int(El_Menor))
     {
      a = a * i;
      b = b * i;
      El_Menor = El_Menor * i;
      i = i * 10;
     }
   int tmpres = a / b;
   return((a - tmpres * b) / i);
  }
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
  • www.mql5.com
//| Expert initialization function                                   | //| Expert deinitialization function                                 | //| Expert tick function                                             | //| test1                                                            |...
Reason: