# Why two double vars with equal values result in &lt; comparison at if statement..??

15

I have a script with two iMa (previous and actual (for example) into double var

double ima1 = iMA(Symbol(),useTimeFrame,MovingPeriod,MovingShift,MODE_SMA,PRICE_CLOSE,1);

double ima2 = iMA(Symbol(),useTimeFrame,MovingPeriod,MovingShift,MODE_SMA,PRICE_CLOSE,0);

both are equal values:  1.08668

but at if statement this result as ima1 < ima2

if(ima1 < ima2) ....

My problem is not the source of vars values (in example above iMA) but the result of if statement with this double vars...

Anyone have a response..???

7663

Do you know the troubles with the doubles?

Look here.

So instead of (ima1 < ima2) (use the SRC-button for code !!) you should (must) use:

`if (ima2 - ima1 > Point*0.1) ...`
16498

 jdbr69:  both are equal values:  1.08668 Because they are not equal The == operand. - MQL4 forum
15

OK.

i have more and more years of experience in many progrmatical language as C, C++, C#, Java, etc. and use (normally) a library of functions to solve this problem when I trafer a program between diferent version of compiler / interprete... In this case (mql4) I have only a one year of experiecne and I write many scripts, Ea, classes and lib. First I read all entire online manual an many and many articles... BUT I ignore an important note present in the on line documentations :

https://www.mql5.com/en/articles/1561 said

Note that in trading operations it is impossible to use the unnormalized prices, whose accuracy exceeds at least by one digit demanded by a trading server.

The StopLoss, TakeProfit and Price values for the pending orders should be normalized with the accuracy, which value is stored in predetermined variable Digits.

In section:

### 3. Function NormalizeDouble

The function NormalizeDouble (double value, int digits) rounds the floating point value to the given precision. Returns normalized value of the double type.

For example:

```int start()
{
double a=3.141592653589;
return(0);
}```

the result is:

a=3.14159000

Note that in trading operations it is impossible to use the unnormalized prices, whose accuracy exceeds at least by one digit demanded by a trading server.
The StopLoss, TakeProfit and Price values for the pending orders should be normalized with the accuracy, which value is stored in predetermined variable Digits.

### 4. Check for equality of two double numbers

It is recommended to compare two double numbers using the function CompareDoubles(double number1,double number2) of the stdlib.mq4 library, which looks as:

```//+------------------------------------------------------------------+
//| correct comparison of 2 doubles                                  |
//+------------------------------------------------------------------+
bool CompareDoubles(double number1,double number2)
{
if(NormalizeDouble(number1-number2,8)==0) return(true);
else return(false); ```

}

Some confusion was originated for the result o f if statement... in many case it result as aspected by me.

Thanks to Gooly and WhRoeder for cooperation and very fast response.

7663

NormalizeDouble() does NOT what you think it does!

As it returns a double and an double - by its definition and construction - is only the closest approximation to a 'human' number!

What would work would be:

```bool CompareDoubles(double number1,double number2)
{
if(  (int)MathRound((number1-number2)/100000000.0) == 0 ) return(true);
else return(false);
}
```

What if you use:

```#define equal(a,b) (fabs((a)-(b))<0.000000001)
...
double x = M_PI;
if ( equal(x,0.0) ) { .. } else { .. }
```

Hint. Watch your doubles in the debugger!

16498

 jdbr69: https://www.mql5.com/en/articles/1561 saidNote that in trading operations it is impossible to use the unnormalized prices, whose accuracy exceeds at least by one digit demanded by a trading server.                The StopLoss, TakeProfit and Price values for the pending orders should be normalized with the accuracy, which value is stored in predetermined variable Digits. I state the exact opposite: Do NOT use NormalizeDouble, EVER. For ANY Reason. It's a kludge, don't use it. It's use is always wrong SL/TP are market orders when triggered, they don't need to be normalized, only abide by the limits Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial and that requires understanding floating point equality Can price != price ? - MQL4 forum Only the 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 forum or Bid/Ask: (No Need) to use NormalizeDouble in OrderSend - MQL4 forum Lot size must also be adjusted to a multiple of LotStep. If that is not a power of 1/10 then NormalizeDouble is wrong. Do it right.
Moderator
10544

WHRoeder:
 Lot size must also be adjusted to a multiple of LotStep. If that is not a power of 1/10 then NormalizeDouble is wrong. Do it right.

I was actually quite surprised to discover that a calculated lot size does not need to adjusted to a multiple of LotStep.  MT4 will round it to the nearest correct value . You do need to check the max and min lot size though.
Moderator
33768

GumRai:
I was actually quite surprised to discover that a calculated lot size does not need to adjusted to a multiple of LotStep.  MT4 will round it to the nearest correct value . You do need to check the max and min lot size though.
How do you discover that ? If you saw that somewhere, it's certainly not always true. You have to "normalize" your lot from LotStep.
Moderator
10544

angevoyageur:
How do you discover that ? If you saw that somewhere, it's certainly not always true. You have to "normalize" your lot from LotStep.
Yes, you are right, it is only if the lot step is 0.01. If the calculated lot size is 0.0123, 0.01 will be used