Asking Stupid question

 

Hi i got case when code on mql4

when i am doing
1000000000 * 3 = -1294967296 (?????????)


but when i do

1000000000 * 3.0 = 3000000000

But this is didnt happend when i multiply with 1 or 2

Can some one explain?

 
Agus Wahyu Pratomo: Hi i got case when code on mql4
when i am doing 1000000000 * 3 = -1294967296 (?????????)

but when i do 1000000000 * 3.0 = 3000000000

But this is didnt happend when i multiply with 1 or 2. Can some one explain?

In the first example, it is doing integer multiplication but the result is greater the limit of an "int" datatype (which is 2 147 483 647), and so the result is "overflowing" or "wrapping" and gives you a negative number. To fix this, typecast it as a "long" (which has a limit of 9 223 372 036 854 775 807).

In the second example, it is doing floating-point multiplication and the results are within the capabilities of that datatype.

 
Fernando Carreiro:

In the first example, it is doing integer multiplication but the result is greater the limit of an "int" datatype (which is 2 147 483 647), and so the result is "overflowing" or "wrapping" and gives you a negative number. To fix this, typecast it as a "long" (which has a limit of 9 223 372 036 854 775 807).

In the second example, it is doing floating-point multiplication and the results are within the capabilities of that datatype.

Thanks for your reply bro. SO i have to use Long rather than int 

Reason: