Download MetaTrader 5
To add comments, please log in or register
Use searching on MQL5.community. It is easy!
melgibson
113
melgibson 2014.02.21 16:41 

As far as I know long type can be converted to double without problem.

long longVar=130365841471008683;

double doubleVar=longVar;

Print("doubleVar:",doubleVar);

The result is: doubleVar:130365841471008690

Why?????????? Please help!

ydrol
594
ydrol 2014.02.21 16:52  

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.

melgibson
113
melgibson 2014.02.21 16:58  

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.

Alain Verleyen
Moderator
28652
Alain Verleyen 2014.02.21 16:59  

See documentation https://docs.mql4.com/basis/types/double

Double type has only 15 significant digits.

whroeder1
13641
whroeder1 2014.02.21 17:00  
melgibson: Why?????????? Please help!
  1. Exactly. https://docs.mql4.com/basis/types/double or Double-precision floating-point format - Wikipedia, the free encyclopedia
    the total precision is therefore 53 bits (approximately 16 decimal digits, 53 log 10 (2) ≈ 15.955

    130 365 841 471 008 683 (18 digits won't be accurate)

  2. Help you with what? Reality?
Alain Verleyen
Moderator
28652
Alain Verleyen 2014.02.21 17:01  
melgibson:

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.

Use file instead of GlobalVariables from Terminal.
whroeder1
13641
whroeder1 2014.02.21 17:22  
Why to you want to store it at all? If the terminal is restarted, no guarantee that it will be the same as before, and what use is it on any other chart?
melgibson
113
melgibson 2014.02.21 18:09  

Thanks for your help, I solved the problem.

Brian Nichols
7
Brian Nichols 2014.05.31 17:19  
melgibson:

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