I Find a huge bug in MetaTrader/MetaEditor

 
Today I use MetaEditor to build a litter tool, I find follow code will output a wrong result.
int start()
{
double v1;
int k;
v1 = (1.0006-1.0005); //v1 = 0.0001
Print(v1); //OK: output 0.0001
v1 /= 0.0001; //v1 = 1.0000
Print(v1); //OK: output 1
k = v1 ; // ERROR: Here k should equal to 1, but it is equal to 0. !!!!!!!!
Print(v1); //OK: output 1
Print(k); //?????: output 0.
return(0);
}
 

 

Why did you think you opened America? Read MQL4 Book about type casting:

The rule of integer calculations consists in that the fractional part is always discarded.

 
Rosh :

Why did you think you opened America? Read MQL4 Book about type casting:

The rule of integer calculations consists in that the fractional part is always discarded.

But the fractional part is zero!,the integral part is 1.

notice! v1 = 1.0000;

 

I've modified your code so:

//+------------------------------------------------------------------+
//|                                             doubleBeCarefull.mq4 |
//|                      Copyright © 2009, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

#include <stdlib.mqh>
//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int start()
  {
//----
   double v1;
   int k;
   v1 = (1.0006-1.0005); //v1 = 0.0001
   Print(v1); //OK: output 0.0001
   v1 /= 0.0001; //v1 = 1.0000
   Print(v1); //OK: output 1
   k = v1 ; // ERROR: Here k should equal to 1, but it is equal to 0. !!!!!!!!
   Print("Print v1=",v1); //OK: output 1
   Print("DoubleToStrMorePrecision(v1,16)=",DoubleToStrMorePrecision(v1,16));
   Print(k); //?????: output 0.
   return(0);   
//----
   return(0);
  }
//+------------------------------------------------------------------+

and here is result:

19:05:36 Compiling 'doubleBeCarefull'
19:11:01 doubleBeCarefull GBPUSD,H4: loaded successfully
19:11:01 doubleBeCarefull GBPUSD,H4: 0.0001
19:11:01 doubleBeCarefull GBPUSD,H4: 1
19:11:01 doubleBeCarefull GBPUSD,H4: Print v1=1
19:11:01 stdlib GBPUSD,H4: loaded successfully
19:11:01 doubleBeCarefull GBPUSD,H4: DoubleToStrMorePrecision(v1,16)=0.9999999999998898
19:11:01 doubleBeCarefull GBPUSD,H4: 0
19:11:01 doubleBeCarefull GBPUSD,H4: uninit reason 0
19:11:01 stdlib GBPUSD,H4: uninit reason 0
19:11:01 stdlib GBPUSD,H4: removed
19:11:01 doubleBeCarefull GBPUSD,H4: removed


Check it yourself, pleasae.

 

Oh, I see! Thank you Rosh.

So It was Print() function trick me.