English

## Typecasting: Long to Double

 10 melgibson 2014.02.21 17: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!
 484 ydrol 2014.02.21 17: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.
 10 melgibson 2014.02.21 17: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.
 Moderator3206 angevoyageur 2014.02.21 17:59 #   See documentation https://docs.mql4.com/basis/types/double Double type has only 15 significant digits.
 13122 WHRoeder 2014.02.21 18:00 #   melgibson: Why?????????? Please help! Exactly. https://docs.mql4.com/basis/types/double or Double-precision floating-point format - Wikipedia, the free encyclopediathe total precision is therefore 53 bits (approximately 16 decimal digits, 53 log 10 (2) ≈ 15.955130 365 841 471 008 683 (18 digits won't be accurate) Help you with what? Reality?
 Moderator3206 angevoyageur 2014.02.21 18: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.
 13122 WHRoeder 2014.02.21 18: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?
 10 melgibson 2014.02.21 19:09 #   Thanks for your help, I solved the problem.
 3 bnichols 2014.05.31 19: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.