Double and Float variables accuracy issue

 

Hi everybody this is my first time on the community.

Hope not to bother anybody with silly questions.

 

I noticed that doing many operations with Double or Float variables returns imprecise results.

I created this simple code as example:

 double x;
 float y;
 int filehandle=FileOpen("File.txt",FILE_WRITE|FILE_TXT);
 FileWrite(filehandle,"D O U B L E   V A R I A B L E");
 FileWrite(filehandle,"");
 for (x=0.05; x<=10; x=x+0.05) FileWrite(filehandle,"   ",x);
 FileWrite(filehandle,"");
 FileWrite(filehandle,"");
 FileWrite(filehandle,"");
 FileWrite(filehandle,"F L O A T   V A R I A B L E");
 FileWrite(filehandle,"");
 for (y=0.05; y<=10 ; y=y+0.05) FileWrite(filehandle,"   ",y);
 FileClose(filehandle);
Opening the resulting “File.txt” and scrolling down you will immediately notice numbers that I wouldn’t really expect.

If math hasn’t become an opinion yet, the code above shouldn’t give numbers like:

2.100000000000001

6.199999999999986

for the Double variable and:

6.10001

8.75002

for the Float variable.

 

Notice that both “for” loops are terminated one iteration earlier because
10.00000000000001 is greater than 10

and also

10.00002 is greater than 10.

 

Other two things to notice from this code (if I’m not asking too much) is the two annoying warnings from MetaEditor:

y=0.05   ---- Truncation of constant variable.

and

 y=y+0.05    ---->    Possible loss of data due to type conversion.

It seems that metaeditor is confusing Float type with Integer type.

 

Thanks a lot in advance for your helpfulness and patience.

Documentation on MQL5: Language Basics / Data Types / Typecasting
  • www.mql5.com
Language Basics / Data Types / Typecasting - Documentation on MQL5
 
claudio:

Hi everybody this is my first time on the community.

Hope not to bother anybody with silly questions.

 

I noticed that doing many operations with Double or Float variables returns imprecise results.

I created this simple code as example:

Opening the resulting “File.txt” and scrolling down you will immediately notice numbers that I wouldn’t really expect.

If math hasn’t become an opinion yet, the code above shouldn’t give numbers like:

2.100000000000001

6.199999999999986

for the Double variable and:

6.10001

8.75002

for the Float variable.

 

Notice that both “for” loops are terminated one iteration earlier because
10.00000000000001 is greater than 10

and also

10.00002 is greater than 10.

 

Other two things to notice from this code (if I’m not asking too much) is the two annoying warnings from MetaEditor:

y=0.05   ---- Truncation of constant variable.

and

 y=y+0.05    ---->    Possible loss of data due to type conversion.

It seems that metaeditor is confusing Float type with Integer type.

 

Thanks a lot in advance for your helpfulness and patience.

hi

you must use NormalizeDouble() function.

there is a good article that covers that topic on mql4 site:

 

https://www.mql5.com/en/articles/1561

 

Working with Doubles in MQL4 - MQL4 Articles
  • www.mql5.com
Working with Doubles in MQL4 - MQL4 Articles: features of automated forex trading and strategy tester
 

Thank you for the hint Investeo

Could you tell me what's going on here?

   double b=8.050000000000001;
   Alert(NormalizeDouble(b,2));

why the output is still 8.050000000000001 ??


Also does anybody knows why I'm having the following warnings from MetaEditor:

y=0.05   ---- Truncation of constant variable.

and

 y=y+0.05    ---->    Possible loss of data due to type conversion.

It seems that metaeditor is confusing Float type with Integer type.

Documentation on MQL5: Language Basics / Data Types / Typecasting
  • www.mql5.com
Language Basics / Data Types / Typecasting - Documentation on MQL5