The "if" control sentence fails comparing between integers (rates_total && other)

 

Hi. I don't know what exactly happens. I am comparing two integers that have the same value (with the > operator) and However the control statement "if" returns true ...
Does anyone know why this happens and how to fix it?

I attach a image of the console output...

And this is the code:



NOTE:

start, end and maxPeriods are integers too... in fact,  (end   = rates_total) (start = 0 or start = maxPeriods  or start=rates_total-1)


   //+------------------------------------------------------------------+
   bool CopyData(
                        int pHandler,                      
                        double &pArray[],
                        const int rates_total,                      
                        int pIndicatorID=0
                )
   {
  
         if(!CheckData(pHandler) )return false;
         
         int usedData = end-start;
         if(usedData > rates_total-maxPeriods)
         {
            Print("Bad Settings: Check => (end, start) => rates_total=", rates_total, "    usedData=", usedData );
            return false;         
         }
         
         
         
         if(IsStopped())return false;
         ResetLastError();
         if(CopyBuffer(pHandler, pIndicatorID, start, end, pArray)==-1)
         {            
            Print("CopyBuffer failed! Error => ",GetLastError());
            return false;
         }             
         
         return true;
   }
Custom Graphical Controls. Part 1: Creating a Simple Control
Custom Graphical Controls. Part 1: Creating a Simple Control
  • www.mql5.com
This article covers general principles of development of graphical controls. We are going to prepare tools for a quick and convenient work with graphical objects, analyze an example of creation of a simple control for entering text or numeric data as well as the ways of using it.
Files:
if_fails.png  88 kb
 
karp wak:

Hi. I don't know what exactly happens. I am comparing two integers that have the same value (with the > operator) and However the control statement "if" returns true ...
Does anyone know why this happens and how to fix it?

I attach a image of the console output...

And this is the code:



NOTE:

start, end and maxPeriods are integers too... in fact,  (end   = rates_total) (start = 0 or start = maxPeriods  or start=rates_total-1)


You are subtracting  maxPeriods from rates_total.


if(usedData > rates_total - maxPeriods)

Unless maxPeriods is zero, usedData will always be bigger.

 
Alexandre Borela:
You are subtracting  maxPeriods from rates_total.


Unless maxPeriods is zero, usedData will always be bigger.


Look at the attach image of the console output. It is printing the same value. So the sentence is true. But it can't be if the numbers are equal because I am compared to the (greater than) operator...

This is my other part of the code ... but everything indicates that the problem must be the "if" control statement



   //+------------------------------------------------------------------+
   bool CalculateLimits(const int rates_total, const int prev_calculated)
   {
         start = 0;       
         end   = rates_total;             
                
         if(!isFirstCalculation && prev_calculated==0)
         {
            start = maxPeriods;
            isFirstCalculation = true;
            return true;
         } 
         else if(rates_total==prev_calculated+1) start=rates_total-1;
         else if(rates_total<=prev_calculated) start=rates_total-1;         
         
         return false;
   }
 
Alexandre Borela:
You are subtracting  maxPeriods from rates_total.


Unless maxPeriods is zero, usedData will always be bigger.

I'm sorry ... You were right ... I think I've been programming too many hours ... getting a little sleep would do me good XD ...
Thanks for your help!!

 
karp wak:

I'm sorry ... You were right ... I think I've been programming too many hours ... getting a little sleep would do me good XD ...
Thanks for your help!!

Not a problem, we all get stuck once in a while.
Reason: