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.
-
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.955
130 365 841 471 008 683 (18 digits won't be accurate)
- Help you with what? Reality?
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.
Thanks for your help, I solved the problem.
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.
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.
Genius!
#property strict #include <TypeToBytes.mqh> // https://www.mql5.com/en/code/16280 void OnStart() { long Long1 = ChartID(); double Temp = 0; _W(Temp) = Long1; GlobalVariableSet("GlobalName", Temp); long Long2 = 0; _W(Long2) = GlobalVariableGet("GlobalName"); Print(Long1); // 131954865932598513 Print(Long2); // 131954865932598513 }
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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!