IF comparison not working

 

Hi,


I am developing a function that is driving me crazy, a simple:

if ( temp1 == temp2 ) 

seems not to be working.

The Original function is:

int encontraIOposto2(int direcao, double valorFecho)
{
   for(i=0;i<150;i++)
   {
      if ( direcao == 1 && valorFecho == longGridPositions[i] ) return ( i-1 );
      
      if ( direcao == 2 && valorFecho == shortGridPositions[i] ) return ( i+1 );
   }
   return (0);
}


After decomposing it for debugging and testing proposes, this is the function I am now testing:

int encontraIOposto(int direcao, double valorFecho)
{
   double temp1, temp2;
   temp1 = valorFecho;
   int valorI;
   
   
   for(i=0;i<150;i++)
   {
      if ( direcao == 1 )
      {
         temp2 = longGridPositions[i];
         Print("|| [i]: ", i);
         Print("|| longGridPositions[i]: ", longGridPositions[i]);
         Print("|| valorFecho: ", valorFecho);
         if ( temp1 == temp2 ) 
         {
            Print("***************************************************************");
            valorI = i-1;
            return (valorI);
         }
      }
      
      if ( direcao == 2 )
      {
         temp2 = shortGridPositions[i];
         Print("|| [i]: ", i);
         Print("|| shortGridPositions[i]: ", shortGridPositions[i]);
         Print("|| valorFecho: ", valorFecho);
         if ( temp1 == temp2 ) 
         {
            Print("***************************************************************");
            valorI = i+1;
            return (valorI);
         }
      }
   }
   return (0);
}


It seems that the:

if ( temp1 == temp2 )

is not working.


Please look at the results in the Tester Journal:

Tester Journal Results


When the [i] is 73, valorFecho is equal to longGridPositions[i], both with value 1.23.

But as you can see from the Journal, the return is not executed, and the for circle for continues, also the

Print("***************************************************************");

does not appear in the Journal!


Does anybody can see what am I doing wrong?


Thanks in advance

 
nunonuk:

Hi,

I am developing a function that is driving me crazy, a simple:

Read this thread carefully, understand what a floating point number is and learn how to effectively compare double values: Can price != price ?
 
RaptorUK:
Read this thread carefully, understand what a floating point number is and learn how to effectively compare double values: Can price != price ?


Hi RaptorUK, I used NormalizeDouble in the past, but never understood really why, just knew that without that it wouldn't work!


Now I understand why!


Thanks for bringing light to my mind :)