MT4 Double significant digits precision arithmetic representation -- BINARY64 or DECIMAL64

51

Hello Gurus,

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

https://en.wikipedia.org/wiki/Double-precision_floating-point_format

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.

Kind regards,

Cosm

Double-precision floating-point format - Wikipedia
• en.wikipedia.org
Double-precision floating-point format is a computer number format that occupies 8 bytes (64 bits) in computer memory and represents a wide, dynamic range of values by using a floating point. Double-precision floating-point format usually refers to binary64, as specified by the IEEE 754 standard, not to the 64-bit decimal format decimal64. In...
2769

Real Types (double, float) - Data Types - Language Basics - MQL4 Reference
• docs.mql4.com
Real Types (double, float) - Data Types - Language Basics - MQL4 Reference
51

Drazen Penic:

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

 Type Size in bytes Minimal Positive Value Maximum Value C++ Analog float 4 1.175494351e-38 3.402823466e+38 float double 8 2.2250738585072014e-308 1.7976931348623158e+308 double

WHILE ON FOLLOWING  LINK I CAN SEE THE DIFFERENCE IN EXPONENTIAL RANGE VALUES FOR DECIMAL IS DIFFERENT.

https://en.wikipedia.org/wiki/Decimal64_floating-point_format

"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.

Also

"Decimal64 floating point is a relatively new decimal floating-point format, formally introduced in the 2008 version[1] of IEEE 754 as well as with ISO/IEC/IEEE 60559:2011.[2]"

And

"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.[5]

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.

Floating point - Wikipedia
• en.wikipedia.org
In computing, floating point is the formulaic representation that approximates a real number so as to support a trade-off between range and precision. A number is, in general, represented approximately to a fixed number of significant digits (the significand) and scaled using an exponent in some fixed base; the base for the scaling is normally...
51

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..

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.

-fp (Specify Floating-Point Behavior)
• msdn.microsoft.com
Specifies floating-point behavior in a source code file. precise The default. Improves the consistency of floating-point tests for equality and inequality by disabling optimizations that could change the precision of floating-point calculations. (Maintaining specific precision is required for strict ANSI conformance.) By default, in code for...
51

Also ,