As far as I know long type can be converted to double without problem.
The result is: doubleVar:130365841471008690
Why?????????? Please help!
Because there arent enough bits for the main number. A double is stored in exponential format as two numbers ( A * 2 ^ B )
The format is IEE754 (also here )
The number of bits allocated for 'A' determines the size of integer than can be safely stored.
32 bit int's can be safely stored, but 64bit longs cant.
Approx 15 significant decimal digits can be stored safely in a double.
Thank you for your answer.
The problem is that I'd like to store the ChartID() in GlobalVariables.
ChartId(() returns long but GlobalVariables stores double. Because the conversion problem I get back wrong value.
See documentation https://docs.mql4.com/basis/types/double
Double type has only 15 significant digits.
130 365 841 471 008 683 (18 digits won't be accurate)
Thanks for your help, I solved the problem.
For the record I ran into the same issue in a portfolio trading system, wanting to send a custom (chart) event to an indicator on a specific chart from EAs running on a number of other charts.
The workaround I used was to disassemble the target ChartID() into 2 long variables like this:
long topID = ChartID()/1000000000;
long bottomID = ChartID() - topID*1000000000;
The 2 long variables are then converted to doubles without loss of digits, set as 2 global variables on indicator initialization and reassembled by the EA's that require the ID.
To add comments, please log in or register