Typecasting: Long to Double

 

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!

 

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.

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

Thanks for your help, I solved the problem.

 
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.

 
Brian Nichols:


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
}


Any type in globalvariables.

Reason: