Float / Double / DoubleToStr...

 

Hey guys,

I try to optimize my indicators a little bit by implementing new MQL4 code. I have some general questions about data types. When I need a high or low of a bar, I used the data type double. But now I see that float uses only 4 bytes instead of 8 bytes used by double. When it comes to prices, can I just replace double with float? And what happens when I need to convert it into a string? Can I still use DoubleToStr, even when I use float?

 
  1. Ignore the char, short, float, etc unless you are dealing with passing a structure to some API
  2. Why are you concerned with 4 bytes?
  3. A Float has ~7 digits of accuracy. so a (5 digit broker's) JPY price has 100.001 has 6 digits. Do you really want to loose precision on price?
  4. a float will be cast to double and passed to DoubleToStr.
 

Ok, then I will forget about optimizing the code. My idea was to get more familiar with the new MQL4 and therefore I wanted my old indicators to be adapted to new MQL4. My girl-friend is learning Java at the university right now and we talked about different data types and how to cast them. That was the reason why I generally thought about optimizing my code. I didn't know that Float has ~7 digits of accuracy. So you are absolutely right not to use it.

Thanks!

 
mar:

Ok, then I will forget about optimizing the code. My idea was to get more familiar with the new MQL4 and therefore I wanted my old indicators to be adapted to new MQL4. My girl-friend is learning Java at the university right now and we talked about different data types and how to cast them. That was the reason why I generally thought about optimizing my code. I didn't know that Float has ~7 digits of accuracy. So you are absolutely right not to use it.

Thanks!


Even the Java developers have a saying "first make it working then optimize - if you still have time left" :)
 

Decimals have much higher precision and are usually used within financial applications that require a high degree of accuracy. Decimals are much slower (up to 20X times in some tests) than a double/float. Decimals and Floats/Doubles cannot be compared without a cast whereas Floats and Doubles can. Decimals also allow the encoding or trailing zeros.

Float - 7 digits (32 bit)

Double-15-16 digits (64 bit)

Decimal -28-29 significant digits (128 bit)

The main difference is Floats and Doubles are binary floating point types and a Decimal will store the value as a floating decimal point type. So Decimals have much higher precision and are usually used within monetary (financial) applications that require a high degree of accuracy. But in performance wise Decimals are slower than double and float types.

What is the difference between Decimal, Float and Double in .NET? Decimal vs Double vs Float
  • net-informations.com
The Decimal, Double, and Float variable types are different in the way that they store the values. Precision is the main difference where float is a single precision (32 bit) floating point data type, double is a double precision (64 bit) floating point data type and decimal is a 128-bit floating point data type. Float - 32 bit (7 digits...
Reason: