possible loss of data due to type conversion

 

Hi there;

I have problem with this warning.

I wrote a function(double UnderPrice(char variable)) that takes a "char" parameter and gives a double one.

but when i call it and compile it say a warning (possible loss of data due to type conversion) for "char" variables that add with a positive number

double min=UnderPrice(3)-UnderPrice(-3);
   for(char i=-4;i<=4;i++)
      if(UnderPrice(i+1)-UnderPrice(i)<min)min=UnderPrice(i+1)-UnderPrice(i);
   if(fmax(SymbolInfoInteger(Symbol(),SYMBOL_SPREAD),MarketInfo(Symbol(),MODE_STOPLEVEL))*3*Point>min)
   {
      ExpertRemove();
      Print("This Expert Can't Works on This Symbol Today",SymbolInfoInteger(Symbol(),SYMBOL_SPREAD),MarketInfo(Symbol(),MODE_STOPLEVEL),min);
   }

 Please help me.

 

This warning points you to a type cast and you can prevent this warning if you explicitly write e.g. the designated type in front of the variable: int a = 2; double x = (double)a; (or: double a = double(a).

See here: https://www.mql5.com/en/docs/basis/types/casting

 
Carl Schreiber #:

This warning points you to a type cast and you can prevent this warning if you explicitly write e.g. the designated type in front of the variable: int a = 2; double x = (double)a; (or: double a = double(a).

See here: https://www.mql5.com/en/docs/basis/types/casting

Thanks Carl

I know it, But why i should see this warning when i don't see before. 

for example in below code i didn't see this warning:

{
   for(char i=1;i<ArrayRange(r_s,0)-2;i++)      
      if(Bid>=r_s[i][0] && Bid<r_s[i+1][0])return(i);
   return(-1);   
}
 
Try double to the variable i
So, your for(char i=-4) must be for(double i=-4)
Or
Put (double) before the i as below 
if(UnderPrice((double)i+1)-... 
Plz let me know if the warning gone
 
Mohammed Abdulwadud Soubra #:
Try double to the variable i
So, your for(char i=-4) must be for(double i=-4)
Or
Put (double) before the i as below 
Plz let me know if the warning gone

Hi Mohammed;

It solved with (char) not with (double).

However i need an answer for this question: 

why i should see this warning when i don't see before. 

for example in below code i didn't see this warning:

{
   for(char i=1;i<ArrayRange(r_s,0)-2;i++)      
      if(Bid>=r_s[i][0] && Bid<r_s[i+1][0])return(i);
   return(-1);   
}
 
   char i=2;
   Print(typename(i+1)); // int

i is a char. One (1) is an int, therefor i+1 is also an int. But your function takes a char not an int. Thus the error.

Either cast the one to a char before adding. Cast the sum for the function call. Or, just use an int and stop using char.

 
William Roeder #:

i is a char. One (1) is an int, therefor i+1 is also an int. But your function takes a char not an int. Thus the error.

Either cast the one to a char before adding. Cast the sum for the function call. Or, just use an int and stop using char.

I appreciate you William, NICE answer 
Reason: