MQL4 Relation Operations
Overview
Boolean values in MQL4 are represented as FALSE (0) and TRUE (any non-zero value). Relation operations and logical operations result in either FALSE (0) or TRUE (1).
Operators
==: True if a is equal to b.!=: True if a is not equal to b.<: True if a is less than b.>: True if a is greater than b.<=: True if a is less than or equal to b.>=: True if a is greater than or equal to b.Real Number Comparison
Direct comparison of two real numbers (double) for equality is unreliable due to potential differences in the 15th decimal place. To accurately compare real numbers:
1. Calculate the normalized difference between the two numbers. 2. Compare this normalized difference to zero.
**Example Function:**
bool CompareDoubles(double number1, double number2)
{
if(NormalizeDouble(number1 - number2, 8) == 0) return(true);
else return(false);
}
**Usage Example:**
void OnStart()
{
double first = 0.3;
double second = 3.0;
double third = second - 2.7; if (first != third) {
if (CompareDoubles(first, third)) {
printf("%.16f and %.16f are equal", first, third);
}
}
}
// Result: 0.3000000000000000 and 0.2999999999999998 are equal