Question regarding execution.

 

Hi,

Can someone please help me understand why the following code executes in the way it does.

I know there must be a simple reason, but I cannot find it. Thank you in advance.

double num=25.1234-0.123; if(num==25.0004) {Print("It is TRUE");} else {Print("It is not TRUE???");} //Prints...It is TRUE
num=25.1234-25.123; if(num==0.0004) {Print("It is TRUE");} else {Print("It is not TRUE???");} //Prints...It is not TRUE???

 
Parisio:

Can someone please help me understand why the following code executes in the way it does.

I know there must be a simple reason, but I cannot find it. Thank you in advance.

double num=25.1234-0.123; [...]

In floating point arithmetic, 25.1234 - 0.123 = 25.000399999999999.


See topics such as https://www.mql5.com/en/forum/116326 and https://www.mql5.com/en/forum/116228 for the need to use NormalizeDouble(), and/or the CompareDoubles() function from stdlib.mq4.

 
jjc wrote >>

In floating point arithmetic, 25.1234 - 0.123 = 25.000399999999999.

See topics such as https://www.mql5.com/en/forum/116326 and https://www.mql5.com/en/forum/116228 for the need to use NormalizeDouble(), and/or the CompareDoubles() function from stdlib.mq4.

Hi jjc,

I thought the problem was related to the result of num being 0.0004 as opposed to 25.0004. The result of num as 25.0004 executes in the way I expect. The precision I use in my EA code is comparing the 9th decimal place...so unfortunately NormalizeDouble() didn't help me. Your answer however DOES help a great deal, as it allows me to resolve the problem by using >= in place of the ==. Thanks again, you have saved me unknown hours of debugging.

Reason: