How to deal with INT as DOUBLE?

 

I'm dealing with integers and I want to get the result of my calculation as simple double value,

below my simple code which using int i, xH and xH to get my double Buffer[],

When I test it, suppose that at certain location (i-xL)=26 and (xH-xL)=7,

The result is only 3 not 3.714!

What should I do to get the result in double form although I'm using int parameters?

int i;
int xH;
int xL;

double Buffer[i]=(i-xL)/(xH-xL);
 

It's typecasting problem https://docs.mql4.com/basis/types/casting

Also this may gives you some idea https://www.mql5.com/en/forum/139559

   int a, b;
   double d, e;
   
   a = 1; b = 2;
   d = a; e = b;
   Print (DoubleToStr(d/e, Digits));

:)

 

when dividing and the result is a double, either the dividend or divisor has to be a double.

int start()
  {
   int one = 1;
   double three = 3;
   double third = one / three;
   
   double two = 2;
   int four = 4;
   double half = two / four;

   Comment(third," ",half);
   return(0);
  }
 
OmegaFX:

The result is only 3 not 3.714!

What should I do to get the result in double form although I'm using int parameters?

double Buffer[i]=(i-xL)/(xH-xL);

The red portion is an int/int resulting in an int (26/7=3). THEN the int is assigned to a double (3 -> 3.0.)

If you convert any part of the expression to a double, then all parts would be converted and the expression would be double/double.

  1. double d=i;
    double Buffer[i]=(d-xL)/(xH-xL);               // 26.0/7 -> 26.0/7.0 -> 3.714285714285714
  2. double Buffer[i]=(i-xL); Buffer[i] /= (xH-xL);
  3. double Buffer[i]=Double(i-xL)/(xH-xL);
    : ////////////////////////////////////////
    int        Int(int a){          return(a); }
    double  Double(double a){       return(a); }
    
See also Working with Doubles in MQL4 - MQL4 Articles
 
OmegaFX:

What should I do to get the result in double form although I'm using int parameters?

Read this ...

https://docs.mql4.com/basis/types/casting

and what WHR posted above.

Reason: