Problems with division especially when there's 7

 

Hi all, still new to this mql language and I came a across a very weird error and need a little guidance. For division of double numbers such as:

5.0/100 = 0.05

6.0/100 = 0.06

7.0/100 = 0.07000000000000001

I got no idea why when I divide 7.0/100 I got that result when its suppose to be 0.07. Is it an error from my side or the mql itself because it works on 5.0/100 and 6.0/100. 

 

Hi,

I would recommend you to read the chapter Language Baics -> Data Types -> Real Types in the MQL documentation.
There you will find the answer.

Best regards,

 
Werner Arthur Klehr:

Hi,

I would recommend you to read the chapter Language Baics -> Data Types -> Real Types in the MQL documentation.
There you will find the answer.

Best regards,

Got it it works now when I use Float instead of Double, Thanks for the help man.
 

Floating-point has an infinite number of decimals, it's you, not understanding floating-point and that some numbers can't be represented exactly. (like 1/10.s)
          Double-precision floating-point format - Wikipedia

See also The == operand. - MQL4 programming forum 2013.06.07

If you want to see the correct number of digits, convert it to a string with the correct/wanted accuracy.
          question about decima of marketinfo() - MQL4 programming forum 2016.05.18

 

Yea its kinda difficult because I want to use the double number for my "volume" or "lots" variable when sending an order. So for example that I want to order a buy order with 0.07 lots. I realize that the "volume" or the "lots" size is in a double format.

double a = 7.0/100;

OrderSend(_Symbol, OP_BUY, a, Ask, 10,0,0,"First trade",30012021,0,clrGreen);

But this won't work because a = 0.070000000001, but the function requires a double variable for the volume.

Because for numbers like 0.07 it wont print out 0.07 only but 0.0700000001 therefore sometimes my code crashes due to these formats. 

 

Hi,

another hint from me: have a look at the NormalizeDouble() function - maybe it does
exactly what you want.

Best regards,

 
  1. Daily Inov: But this won't work because a = 0.070000000001, but the function requires a double variable for the volume.

    Yes it will. What part of “some numbers can't be represented exactly” was unclear?

  2. Werner Klehr: another hint from me: have a look at the NormalizeDouble() function 
    NormalizeDouble, It's use is usually wrong.
    1. Floating point has infinite number of decimals, it's your not understanding floating point and that some numbers can't be represented exactly. (like 1/10.)
                Double-precision floating-point format - Wikipedia, the free encyclopedia

      See also The == operand. - MQL4 programming forum 2013.06.07

    2. Print out your values to the precision you want with DoubleToString - Conversion Functions - MQL4 Reference.

    3. SL/TP (stops) need to be normalized to tick size (not Point) — code fails on metals. (On 5Digit Broker Stops are only allowed to be placed on full pip values. How to find out in mql? - MQL4 programming forum) and abide by the limits Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial and that requires understanding floating point equality Can price != price ? - MQL4 programming forum

    4. Open price for pending orders need to be adjusted. On Currencies, Point == TickSize, so you will get the same answer, but it won't work on Metals. So do it right: Trailing Bar Entry EA - MQL4 programming forum or Bid/Ask: (No Need) to use NormalizeDouble in OrderSend - MQL4 programming forum

    5. Lot size must also be adjusted to a multiple of LotStep and check against min and max. If that is not a power of 1/10 then NormalizeDouble is wrong. Do it right.

    6. MathRound() and NormalizeDouble() are rounding in a different way. Make it explicit.
                MT4:NormalizeDouble - MQL5 programming forum
                How to Normalize - Expert Advisors and Automated Trading - MQL5 programming forum

    7. Prices you get from the terminal are already normalized.

    8. PIP, Point, or Tick are all different in general.
                What is a TICK? - MQL4 programming forum 2014.08.03

 

Hi,

i agree 100% to Williams explanations about NormalizeDouble - but for calculating the lot-size
in your short test programm it will work fine ;-)

Best regards

 
The output of Print() and alert() can be confusing for some people.
Better use 
Print( Repr(num) );
Or
printf("%.15g", num);

Reason: