BUG? Wrong 'if' condition is triggered - completely illogical!! o_O

 

I have a very simple code sample:

    if (Range>avgRange) Comment("Deviation by ",Range-avgRange," pts. ",Range," = ",avgRange);
    if (Range<=avgRange) Comment("No Deviation! 100% Ranging!");

In my specific example, both variables 'Range' & 'avgRange' are of 'double' type and are IDENTICAL in value, equating to 163 respectively!

With that in mind, the first 'if' conditional should NEVER trigger, and yet it does!!!

I even added the part of:

Comment("Deviation by ",Range-avgRange," pts. ",Range," = ",avgRange);

Just to be sure, and here is the result:

I'm STUNNED O_O... WTF?

Is this some sort of BUG, or am I missing something here???

Thanks!!

 

Okay, I managed to resolve this BUG by applying the 'NormalizeDouble()' function to both variables prior to 'if' conditionals.

Range = NormalizeDouble(Range,2);
avgRange = NormalizeDouble(avgRange,2);

However, I am still confused here! I assumed that there might have been a minor deviation between the values for some reason, maybe because each was calculated differently.

To test this out I used the 'DoubleToStr()' function to display both 'double' type values with maximum precision of 8 digits after decimal:

Alert(DoubleToStr(Range,8)," = ",DoubleToStr(avgRange,8));

The result was: 163.00000000 = 163.00000000

Again, EXACTLY IDENTICAL down to the 8th digit after decimal.

And yet still the condition:

if (Range>avgRange)

Was triggered!

This makes no sense at all!!!

Of course from now on I will always use the 'NormalizeDouble()' function to avoid this problem in the future, but again, am I missing something here, or is this in fact a BUG?

Thanks!

 
nanquan:


Of course from now on I will always use the 'NormalizeDouble()' function to avoid this problem in the future, but again, am I missing something here, or is this in fact a BUG?

Thanks!

You need to educate yourself about double variables and the issues they can cause you . . . have read of this thread: https://www.mql5.com/en/forum/136997
 
nanquan:

Okay, I managed to resolve this BUG by applying the 'NormalizeDouble()' function to both variables prior to 'if' conditionals.

However, I am still confused here! I assumed that there might have been a minor deviation between the values for some reason, maybe because each was calculated differently.

To test this out I used the 'DoubleToStr()' function to display both 'double' type values with maximum precision of 8 digits after decimal:

The result was: 163.00000000 = 163.00000000

Again, EXACTLY IDENTICAL down to the 8th digit after decimal.

And yet still the condition:

Was triggered!

This makes no sense at all!!!

Of course from now on I will always use the 'NormalizeDouble()' function to avoid this problem in the future, but again, am I missing something here, or is this in fact a BUG?

Thanks!

No, it's not a bug. The doubles have 15 digits of resolution so displaying to a mere 8 doesn't show the difference.
 

Okay, thanks guys! Didn't know it went all the way up to 15 digits after decimal.

I'll have a read of that thread as well!

Thanks again!

Reason: