Type Casting Bug??

 

Hello,

Does anyone understand this weird behaviour bellow:

int init()
  {

   double LotSize =1.11611;
   int pres = -1*Logb(10,0.01);
 
   Alert("LotSize = " + LotSize + ", -Logb(10,0.01) = " + (-1*Logb(10,0.01)) + ", pres = " + pres +  ", norm = " + NormalizeDouble(LotSize,pres) );

   return(0);
  }

Output:

2013.08.24 08:44:48 me EURJPY.,H1: Alert: LotSize = 1.11611000, -Logb(10,0.01) = 2.00000000, pres = 1, norm = 1.10000000


But if I do this:

int init()
  {

        double LotSize =1.11611;
        int pres = 2.00000000;
 
   Alert("LotSize = " + LotSize + ", -Logb(10,0.01) = " + (-1*Logb(10,0.01)) + ", pres = " + pres +  ", norm = " + NormalizeDouble(LotSize,pres) );

   return(0);
  }


Output:

2013.08.24 08:46:37 me EURJPY.,H1: Alert: LotSize = 1.11611000, -Logb(10,0.01) = 2.00000000, pres = 2, norm = 1.12000000

In the first case, why didn't pres get assigned the value of -1*Logb(10,0.01) correctly?


 
BenLinus:

Hello,

In the first case, why didn't pres get assigned the value of -1*Logb(10,0.01) correctly?

Isn't it because -1*Logb(10,0.01) is cast to an int and Logb(10,0.01) is 1.9999999xxxxx ? try ( -1 * Logb(10,0.01)) + 0.5 or use MathCeil()
 
int = (...) + 0.5 or int = MathRound(...)
 

MathRound() does the trick. So, it is probably as RaptorUK said: Logb(10,0.01) = 1.9999999....

But it doesn't make sense to me why the output of Alert() says: -Logb(10,0.01) = 2.00000000. Maybe it rounds it on its own.


Thanks.


 
BenLinus:

...

But it doesn't make sense to me why the output of Alert() says: -Logb(10,0.01) = 2.00000000. Maybe it rounds it on its own.

Looks like the number gets rounded in the 9th decimal place.

Alert("Alert test #1: " + 1.99999999);
Alert("Alert test #2: " + 1.999999999);
Alert("Alert test #3: " + 1.999999991);
Alert("Alert test #4: " + 1.999999995);

Alert Test #1

 
Thirteen:

Looks like the number gets rounded in the 9th decimal place.


Yes. So, this was the culprit. Thanks