I am trying to transfer some complex MQL calculations on a c++ dll.
I' ve run into a rounding problem when used complex arithmetic operations on a c++ dll version VS 2015 32bit X86 compiler, trying to port some code from mq4 for more speed. The transferred functions return slightly different results on their last significant digits and the problem increases as the number of loop counter increases making the first significant digits influenced. This is not acceptable when calculation price levels with 5 significant digits. In the first loops the results are identical , but after a certain number there is an exponential deviation that affecting the result.
Results can be reproduced in a loop of lets say 1000 times of number calculations by calling trigonometric or pow functions (or any other complex calculation storing its value incrementally on a at least 15 significant digits double.
So is it Binary64 or Decimal 64?
The loss of accuracy problem has been described many times and here is the summary of standards
My question is how can I match the MQL Double data type - double precision arithmetic output - with a native or managed external compiled library or c++ or c# compiler.
Should I go with open Pascal or C# for my dll? Anyone with experience on the topic about compiler arithmetic compatibility?
I run on a windows 10 64 bit but i have chosen compiler setting for compatibility with with win8.
Are there any special settings or requirements or library to use for the external compiler? I remember from an article here referring to a VS6 compiler as the base for some mathematical functions where called from the MT4. Is it true?
It is imperative to get the exactly same representation and output from both worlds.
An insight of metatrader developer / specialists would be great.
Read the documentation: https://docs.mql4.com/basis/types/double
That's a good reference as starting point, however does not solve the problem. From my link above "decimal64 is a decimal floating-point computer numbering format that occupies 8 bytes (64 bits) in computer memory. It is intended for applications where it is necessary to emulate decimal rounding exactly, such as financial and tax computations."
From you link to mql doc, I can conclude that the BINARY 64 is used internally for MT4. This is because
Size in bytes
Minimal Positive Value
WHILE ON FOLLOWING LINK I CAN SEE THE DIFFERENCE IN EXPONENTIAL RANGE VALUES FOR DECIMAL IS DIFFERENT.
"Decimal64 supports 16 decimal digits of significand and an exponent range of −383 to +384, i.e. ±0.000000000000000×10−383 to ±9.999999999999999×10384. "
So my reasonable assumption is that MT4 uses the BINARY64 and not the DECIMAL64. However it seems that the c++ math standard library or compiler 2015, have chosen or swap automatically for absolute precision. In my case this is something I want to avoid, as I need to match the MT4.
"Decimal64 floating point is a relatively new decimal floating-point format, formally introduced in the 2008 version of IEEE 754 as well as with ISO/IEC/IEEE 60559:2011."
"Double-precision floating-point format usually refers to binary64, as specified by the IEEE 754 standard, not to the 64-bit decimal format decimal64."
Also from my links i can see that used to be or can be a problem
C and C++
C and C++ offer a wide variety of arithmetic types. Double precision is not required by the standards (except by the optional annex F of C99, covering IEEE 754 arithmetic), but on most systems, the double type corresponds to double precision. However, on 32-bit x86 with extended precision by default, some compilers may not conform to the C standard and/or the arithmetic may suffer from double-rounding issues.
Hence, i think that problem's spectrum has become more narrow now and need to find out how to compile c++ with BINARY instead from probably DECIMAL.
I hope someone will find something. It is important for the community to know about it when using formulas / libraries with complex computations.
The results are unexpected and the precision difference can make or break a system. In my case the perfect precision breaks my system under development..so need more control on the issues.
Ok , i found something more, but needs either testing or an answer from MQL developers.
The aim is to emulate the calculation precission of the current MQL build using a c++ compiler.
From big M web site i found the link..
/fp (Specify Floating-Point Behavior).
Soluition to the problem can be that some flags from visual studio must be set at code generation settings (Floating mode - :Default is precise) and maybe a combination with Enable Floating Point Exceptions.
There are rules as outlined at the above doc.
Any insights from the development team of MQL to their settings when compiling would be an advantage.
this can be helpful