Convert to Integer function

 
Is there a function to convert a Double value to an Integer? In another language I've used I think it was called "CInt". At the moment I'm defining an integer and assigning the Double value to it, but it would be simpler if I didn't have to do this. Thanks.
 

Assigning a double to and integer variable will do it.

 
Ickyrus:

Assigning a double to and integer variable will do it.


OK, I'll just write my own function that does that. Thanks.
 

Take your pick: mathfloor, mathceil, mathround. These can all be used to reduce your double value to an integer without resorting to typecasting.

 
1005phillip:

Take your pick: mathfloor, mathceil, mathround. These can all be used to reduce your double value to an integer without resorting to typecasting.

Not strictly true. You cannot do bit manipulation with results of those functions. I store order info in an array of doubles, but must convert stored OrderType() back to true int to check for BUY/SELL via bit mask
 
brewmanz:
Not strictly true. You cannot do bit manipulation with results of those functions. I store order info in an array of doubles, but must convert stored OrderType() back to true int to check for BUY/SELL via bit mask

Is MathFloor(1.1) != 1?

Is MathCeil(0.9) != 1?

Is MathRound(1.1) != 1?

 
1005phillip:

Is MathFloor(1.1) != 1?

Is MathCeil(0.9) != 1?

Is MathRound(1.1) != 1?

It seems to me that you do not understand bit manipulation & processing, which is what I was specifically addressing. Bit processing is not the same as bool.

if((myDouble & myBitMask) == myBitMask)

will not compile but

if((myInt & myBitMask) == myBitMask)

does.

 

uh, bit processing. I have to say that i never used that in real live. I had to study it (time ago).

I can remember that there was a "cool" example, with bit processing it is possible to swap two values without the need of a third helper variable. Might be interesting if you want tune your code to the maximum..

 

As an Assembler programmer for several decades, I use bits a lot. e.g. When I enter a trade, I use a bitmask to decide which SL methods to use i.e. set to 1+2+8 to use SL methods 1, 2 & 4. The actual code I use is

      ThisSLMethodSeq = 1;
      int bitMask = 1;
      for(int bitTest = StopLossMethodsFlags; bitTest != 0; ThisSLMethodSeq++, bitMask = bitMask << 1)
      {
         if((bitTest & bitMask) == bitMask)
         {
            OpenSpecificShortTrade(ThisSLMethodSeq, pOptionalStopLoss); 
         }
         int notMask = 0x7fffffff - bitMask;
         bitTest = bitTest & notMask;
      }

Oh, and that swap method you mentioned (very handy when RAM storage was $1 a byte) ...

   int ia, ib;
   ia = 15;
   ib = 24;
   ia = ia ^ ib;
   ib = ia ^ ib;
   ia = ia ^ ib;
   if(ia != 24 || ib != 15)
      Alert("ia=", ia, "ib=", ib, " wrong");

but you can't use it on doubles ;-)

I have used it for swapping strings in the dim & distant past (okay, over 30 years ago), but not in MQL4.

 
1005phillip:

Take your pick: mathfloor, mathceil, mathround. These can all be used to reduce your double value to an integer without resorting to typecasting.

Look at the return types for these, all doubles. Thus
if((MathCeil(myDouble) & myBitMask) == myBitMask)
will not compile. They return the integer equivalent which is usually, but not always fine.
    double  minLot  = MarketInfo(Symbol(), MODE_MINLOT),
            lotStep = MarketInfo(Symbol(), MODE_LOTSTEP);
        size = MathFloor(size/lotStep)*lotStep;
        if (size < minLot){     EA.status = status;     return(0); }

Either use int v=d; or use
int Int(double d){ return(d); }
 

Fortunately it is easily testable. Run the attached script, investigate the math. Works for me. MQL seems to deftly manage distinguishing between true integers and "equivalent" integers as you put it and mathfloor, mathceil, and mathround all perform as expected.

Files:
integer.mq4  5 kb
Reason: