Problem with Division in MQL4

 
If you are having a problem with dividing two integer variables in MQL4 . This may be your fix!!
MQL4 will give you a zero instead of a 
Decimal number. The answer is simple . . .
just change one of the integer variables to a double type variable and it will work just fine!!!
 
CHARLESS11:
If you are having a problem with dividing two integer variables in MQL4. This may be your fix!!
MQL4 will not let you divide two integer variables; as it will give you a zero instead of a 
Decimal number. The answer is simple . . .
just change one of the integer variables to a double type variable and it will work just fine!!!

Wrong! It will give you an integer part of the result'

Ie. 5/2 will result in 2

 
Keith Watford:

Wrong! It will give you an integer part of the result'

Ie. 5/2 will result in 2

You misunderstood the issue! The problem is not with integers or whole numbers. The problem happens with "int type defined variables" and not just simple division of two whole numbers!!
 
CHARLESS11:
You misunderstood the issue! The problem is not with integers or whole numbers. The problem happens with "int type defined variables" and not just simple division of two whole numbers!!

I don't think that I misunderstood anything.

You said

CHARLESS11:
MQL4 will not let you divide two integer variables; as it will give you a zero instead of a 
Decimal number. 

and that is not true (unless the result is less than 1).


   int a=5;
   int b=2;
   double res=a/b;
   Print("a/b = ",res);

will print 2.0 which is definitely not zero.

 
Keith Watford:

I don't think that I misunderstood anything.

You said

and that is not true (unless the result is less than 1).


will print 2.0 which is definitely not zero.

No!! Your "a" variable needs to be equal to some expression that calculates a number!! and not just simply equal to a number or equal to a simple mathmatic calculation of numbers. For example if "a" = .02*AccountBalance () and "b" = your calculated stoploss in points. If these two numbers were calculated to be 200/400,  "res" even defined as a double would calculate as zero instead of 0.5. In other words, defining "res" as a double does not override the division rule of dividing two integer numbers. On the surface this is not obvious until you spend an hour like I did trying to figure out what was going on. 
 
CHARLESS11:
 If these two numbers were calculated to be 200/400,  "res" even defined as a double would calculate as zero instead of 0.5.

If they were integer values calculated to 200 and 400, then of course, 200/400 will calculate to 0 because 0.5 is less than 1. The integer part of the result is zero.

Now, if they were calculated to 600 and 400, then 600/400 will calculate to 1, which is the integer part of 1.5

So I repeat....

CHARLESS11:
MQL4 will not let you divide two integer variables; as it will give you a zero instead of a 
Decimal number. 
Keith Watford:

Wrong! It will give you an integer part of the result'

 
CHARLESS11:
I am not in here to argue with you, but rather to help anyone new to programming about this issue. I sure you understand how to work with integers and double variables without any problems. 

My only point is if "someone else" tries to do this:

double variable C = (small integer A variable/
                                    Larger integer B variable)
The answer will be 0.0 instead of the real decimal answer. Just because variable C is a double the result will not be the correct double answer instead it will be zero. If the newbie changes either A or B to a double the answer will be correct. I am new to programming and didn't know this! I thought if you defined C as a double MQL4 would give me the answer as a double, the same way it would if you were doing in a regular math problem on paper! I know this is very elementary to an experienced
programmer, but could be a major stumbling block to someone who knows very little about programming!! This post is obviously not for you, since you understand how MLQ4 language works!
 
CHARLESS11:
I am not in here to argue with you, but rather to help anyone new to programming about this issue.

Yet you keep arguing!

Yes, it is important that newbies understand about this issue, but it is also important that they are given the correct information. In your first post you stated that int/int returns zero and that is wrong. It will only return zero if the result is less than 1.   int/int returns the integer part of the result.

CHARLESS11:
double variable C = (small integer A variable/
                                    Larger integer B variable)
The answer will be 0.0 instead of the real decimal answer.

now, that is correct because the result will be less than 1 so the integer part of the result will be zero.

 
CHARLESS11:
If you are having a problem when dividing a small integer by a larger integer variables in MQL4. This may be your fix!!
MQL4 will give you a zero instead of a 
Decimal number. The answer is simple . . .
just change one of the integer variables to a double type variable and it will work just fine!!!

Thanks, this was helpful

 
David Muriithi:

Thanks, this was helpful

You are welcome!  Happy programming!