Move a decimal to the left three times. - Question.

 

I can multiply three by zero, point, zero, zero, one (3*0.001 = 0.003) to move a decimal to the left three times.

Is there an operation or expression that will move a decimal to the left three times instead of having to execute the equation above?

I was venturing through the MQL4 Reference in a MetaEditor Navigator and the only thing that popped out was-

// Logical shift of y representation to the left by x bit   y <<= x;

I don't know if this would work, but it doesn't hurt to throw it out there. Any ideas as to the use of the above operation?

Thank you.

 
Untested:
double ShiftDecimal(double number, int places){
   return(number * MathPow(10, places)); 
}

or something along these lines. As far as I know there is no operator for that, so you have to write a function like above (or if you need it only in one place then just do the above inline to avoid another function call).

The shift operator you mentioned in your posting would do the very same thing for binary numbers (numbers to the base two), it would move the "decimal" point (not really "decimal", how would one call that? "Binary point" maybe?) of a binary number, it is equivalent to the above would be:

double ShiftBinary(int number, int places){
   return(number * MathPow(2, places)); 
}

Here the base is 2 instead of 10.

Note that the usage of MathPow() is a very expensive operation. While the binary shift would be done in just one clock cycle on the CPU because binary is the native number base that is used on that low level it is much harder for the CPU to deal with floating point numbers and this odd (odd in terms of what the computer is used to) base-10 numbering system. MathPow() will involve calls of MathExp() and MathLog(), maybe you should better precompute its result once and cache it if you need it often. If you need many different shifts then compute an array of multiplicators and keep that around so you can later avoid calling MathPow() every time.

 

7bit,

Thank you. It seems your thoughts are directed to the usage of the "MathPow()" function. Any thoughts as to the use of the operation below?

// Logical shift of y representation to the left by x bit   y <<= x;

Thank you.

 
WhooDoo22:

7bit,

Thank you. It seems your thoughts are directed to the usage of the "MathPow()" function. Any thoughts as to the use of the operation below?

Do you know what a bit is ?
 

Simon,

LOL! trick questions now, huh?

To my knowledge, a bit is an allotted size of memory, I believe the smallest size. The next size up is byte.

Eight bits equals a byte.

Thank you.

 
WhooDoo22:

7bit,

Thank you. It seems your thoughts are directed to the usage of the "MathPow()" function. Any thoughts as to the use of the operation below?

Thank you.


the binary shift does the same in base 2 but you need operations in Base 10:


Example: Base 10 (Decimal, this is what you asked for, there is no special operator for this, you need MathPow() and multiplication)

DecimalShift(543000000, -3)  ->  543000


Example: Base 2 (binary, this is what the binary shift operators >> or << do but they operate only in base 2):

BinaryShift(0b011101000000, -3)  ->  0b011101000

or the very same thing with the binary shift operator (much faster but same result):

0b011101000000 >> 3  ->  0b011101000


But as I have written already there is no decimal version of the binary shift operator, this operator is a binary only thing. You need to do it with MathPow() to determine the correct multiplier for your decimal shift and then do a multiplication to apply it.

 

7bit,

Understood. 

Thank you much!

 
7bit:

the binary shift does the same in base 2: 


RaptorUK:
Do you know what a bit is ?


 ;-)

Reason: