float instead of double?

 

hey again guys. can we store our price information in float type instead of double?

we need maximum 5 decimal for price. and its faster in huge calculations.

is there any problems with using that for EAs with lots of variables?

 
Birkimson Varzxorani: and its faster in huge calculations.

Do not assume - you must test.
          Which is faster - Floating-Point or Integer arithmetic? - Expert Advisors and Automated Trading - MQL5 programming forum - Page 11

 
Birkimson Varzxorani:

hey again guys. can we store our price information in float type instead of double?

we need maximum 5 decimal for price. and its faster in huge calculations.

is there any problems with using that for EAs with lots of variables?

I don't recommend to use the float TYPE. Typecasting the return values of mql functions from double to float will introduce round off errors. 

Also it is better to use double as some calculations actually need more than 5 digits to preserve accuracy.

For example if multiply two 5-digits prices, the result needs at least 10 digits of precision. (1.12345 * 1.12345 = 1.2621399025).

Regarding the speed of the code, actually there are far more important things to optimize (e.g., the calculation algorithm and data structures) rather than the type of numeric variables. 

For financial calculations, it is best to use the 128-bit decimal type (not supported by MQL) or at least 64-bit double type. 

Float type (32-bits) is primarily used in heavy graphic calculations, where accuracy is not critical, in order to decrease the size of code in memory.

void OnStart()
  {
   double d = 1.12345 * 1.12345;
   float  f = 1.12345 * 1.12345;
   
   Print("d = ", d);
   Print("f = ", f);
  }

//--- output
// d = 1.2621399025
// f = 1.26214
 

In some cases for heavy calculation, better to convert to int. So, normalize to int. Do the calculation, de-normalize to double.

Keeping it aligned to SYMBOL_MODE_TICK_SIZE will ensure your prices are always right.

 
Enrique Dangeroux:

In some cases for heavy calculation, better to convert to int. So, normalize to int. Do the calculation, de-normalize to double.

Keeping it aligned to SYMBOL_MODE_TICK_SIZE will ensure your prices are always right.

There are two concerns against that approach:

1. The large overhead in conversion between integer and double types will cancel the possible benefits.

2. Calculations involving integer division are normally truncated, and that will lead to loss of precision. 

(for example, to calculate the simple moving average we divide the sum of prices by the ma_period).

FYI, floating-point calculations are done by the math co-processor (FPU). Their speeds have enormously improved over the last few years. Now, FP calculations are assumed to be as fast as (or even faster than) integer calculations.

 
amrali: Now, FP calculations are assumed to be as fast as (or even faster than) integer calculations.

Thus, my post above (#1) proving it.

Reason: