Rounding Problem with uint

 
Hi Can someone explayn to my where this comes from:


input double SL = 1.23400;
uint mySL = 0;

void OnTick()
{

mySL  = uint(NormalizeDouble(SL,5)/_Point);
Comment( IntegerToString(mySL));
  
}
This code with a 1.23400 prints a value 123399

>> so i miss 1 point - i can not finger out why. Wath i am  missing ??
 

The number of type double 124000 is represented in memory as 123999.9999999999854481.

The integer part is 123999. Therefore, if you try to change the type of uint or long, you should set it to 123999.

//+------------------------------------------------------------------+
//|                                                      Test_en.mq5 |
//|                                      Copyright 2012, CompanyName |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+ 
void OnStart()
  {
   double   SL          = 1.24000;
   uint     uint_mySL   = 0;
   long     long_mySL   = 0;
   double   double_mySL = 0.0;

   double   temp=SL/Point();

   uint_mySL      = uint(temp);
   long_mySL      = long(temp);
   double_mySL    = temp;
   Comment("uint_mySL: ",IntegerToString(uint_mySL),"\n",
           "long_mySL: ",IntegerToString(long_mySL),"\n",
           "double_mySL: ",DoubleToString(double_mySL,Digits()));
  }

example

Files:
Test_en.mq5  3 kb
 
Vladimir Karputov:

The number of type double 124000 is represented in memory as 123999.9999999999854481.

The integer part is 123999. Therefore, if you try to change the type of uint or long, you should set it to 123999.


That is not exact. All integers are represented exactly with a double.

The problem here comes from Point() or _Point which is not exactly represented by a double, so when you divide by it you got imprecision. You need to multiply by exact value :

   int      toPoint     = int(MathRound(1/Point()));

   double   temp=SL*toPoint;
 
ALAIN whenever i pass in ur corner beers on me !!
 
Zar88:
ALAIN whenever i pass in ur corner beers on me !!
I will not forget
Reason: