MathFloor(), MathCeil() return wrong value - page 2

 

"/2" is not necessary. You can delete those "/2" from that code.

...
   d_l_2 = (double)lImmediate;
   l_l_2 = (long)((double)lImmediate);
...
   d_l_2 = (double)lChartGetI;
   l_l_2 = (long)((double)lChartGetI);
...

 It will print:

lChartGetI >>(double)l_l_2=717.00000000, 717.00000000, 717.00000000, 717.00000000, 717, 717, 717
lChartGetI >>        l_l_2=717         ,   0.00000000,   1.00000000, 717.00000000,   0,   1, 717
lChartGetI >>        d_l_2=717.00000000, 717.00000000, 717.00000000, 717.00000000, 717, 717, 717
lImmediate >>        l_l_2=717         , 717.00000000, 717.00000000, 717.00000000, 717, 717, 717
lImmediate >>        d_l_2=717.00000000, 717.00000000, 717.00000000, 717.00000000, 717, 717, 717
                       d_l_2 or l_l_2    dmf           dmc           dmr           lmf  lmc  lmr
lImmediate=717, lChartGetI=717, lChartGetI-lImmediate=0.00000000

 

 
And I try to pass these value through parameter
void OnTimer()
  {
//---
   long l1, l2;
   l1 = 717;
   l2 = (long)ChartGetInteger(0, CHART_WIDTH_IN_PIXELS,0); // It get the value right!
   TestMath5(l1, l2);
  }
void TestMath5(long lImmediate, long lChartGetI)
{
   long   l_l_2;
   double d_l_2;
...// other same as TestMath4
}

Oh, the lImmediate occur error too.


lChartGetI >>(double)l_l_2=717.00000000, 717.00000000, 717.00000000, 717.00000000, 717, 717, 717
lChartGetI >>        l_l_2=717         ,   0.00000000,   1.00000000, 717.00000000,   0,   1, 717
lChartGetI >>        d_l_2=717.00000000, 717.00000000, 717.00000000, 717.00000000, 717, 717, 717
lImmediate >>        l_l_2=717         , 0.00000000, 1.00000000, 717.00000000, 0, 1, 717
lImmediate >>        d_l_2=717.00000000, 717.00000000, 717.00000000, 717.00000000, 717, 717, 717
                       d_l_2 or l_l_2    dmf           dmc           dmr           lmf  lmc  lmr
lImmediate=717, lChartGetI=717, lChartGetI-lImmediate=0.00000000
 

So the problem is unrelated with ChartGetInteger().
It just is related with passing parameters to functions and return value.

 

Oh My God!

I debug this bug, and I find a bigger bug!

https://www.mql5.com/en/forum/474
 

 

Now, let's test the final code

int OnInit()
  {
   TestMath6(717);
   return(0);
  }
void TestMath6(long lParam)
{
   long   lLocal=lParam;
   //if directly assign value long lLocal=717, will cause
   //tree optimize error                1       1
   long   l_l_2;
   double d_l_2;
   double dmf, dmc, dmr;
   long   lmf, lmc, lmr;

   Print("lLocal="+lLocal+", lParam="+lParam+", lParam-lLocal="+((double)lParam-(double)lLocal));
   d_l_2 = (double)lLocal;
   l_l_2 = (long)lLocal;
   Print("                   d_l_2 or l_l_2    dmf           dmc           dmr           lmf  lmc  lmr");
   dmf = MathFloor(d_l_2);
   dmc = MathCeil (d_l_2);
   dmr = MathRound(d_l_2);
   lmf = (long)MathFloor(d_l_2);
   lmc = (long)MathCeil (d_l_2);
   lmr = (long)MathRound(d_l_2);
   Print("lLocal >>        d_l_2="+d_l_2+", "+dmf+", "+dmc+", "+dmr+", "+lmf+", "+lmc+", "+lmr);
   dmf = MathFloor(l_l_2);
   dmc = MathCeil (l_l_2);
   dmr = MathRound(l_l_2);
   lmf = (long)MathFloor(l_l_2);
   lmc = (long)MathCeil (l_l_2);
   lmr = (long)MathRound(l_l_2);
   Print("lLocal >>        l_l_2="+l_l_2+"         ,   "+dmf+",   "+dmc+", "+dmr+",   "+lmf+",   "+lmc+", "+lmr);

   d_l_2 = (double)lParam;
   l_l_2 = (long)lParam;
   dmf = MathFloor(d_l_2);
   dmc = MathCeil (d_l_2);
   dmr = MathRound(d_l_2);
   lmf = (long)MathFloor(d_l_2);
   lmc = (long)MathCeil (d_l_2);
   lmr = (long)MathRound(d_l_2);
   Print("lParam >>        d_l_2="+d_l_2+", "+dmf+", "+dmc+", "+dmr+", "+lmf+", "+lmc+", "+lmr);
   dmf = MathFloor(l_l_2);
   dmc = MathCeil (l_l_2);
   dmr = MathRound(l_l_2);
   lmf = (long)MathFloor(l_l_2);
   lmc = (long)MathCeil (l_l_2);
   lmr = (long)MathRound(l_l_2);
   Print("lParam >>        l_l_2="+l_l_2+"         ,   "+dmf+",   "+dmc+", "+dmr+",   "+lmf+",   "+lmc+", "+lmr);

   dmf = MathFloor((double)l_l_2);
   dmc = MathCeil ((double)l_l_2);
   dmr = MathRound((double)l_l_2);
   lmf = (long)MathFloor((double)l_l_2);
   lmc = (long)MathCeil ((double)l_l_2);
   lmr = (long)MathRound((double)l_l_2);
   Print("lParam >>(double)l_l_2="+(double)l_l_2+", "+dmf+", "+dmc+", "+dmr+", "+lmf+", "+lmc+", "+lmr);
}
Files:
testmath6.mq5  13 kb
 

I understand.

double MathCeil (double value);
double MathFloor(double value);
double MathRound(double value);

These function need a double parameter. If you give them a int value, they will look upon the int value as a double value.
And in The IEEE Standard for Floating-Point Arithmetic (IEEE 754), the memory context of a int value will be looked upon as a tiny double value.
So MathCeil() return 1, and MathFloor() return 0. They are right.
But I hope that mql5 compiler can automatic type casting.
This problem has finished.

Reason: