//+------------------------------------------------------------------+ //| debug_demo.mq5 | //| Copyright © 2018, Amr Ali | //| https://www.mql5.com/en/users/amrali | //+------------------------------------------------------------------+ #property copyright "Copyright © 2018, Amr Ali" #property link "https://www.mql5.com/en/users/amrali" #property version "4.0" #include //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ #define PRINT(A) Print(#A + " = ", (A)) //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void OnStart() { double a = 0.1; double b = 0.2; //--- Example of where exact comparison of doubles can fail: if(a + b == 0.3) { Print("This should be the expected result."); } else { Print("Too bad comparison!"); } //--- Debugging floating-point numbers can help. Print( a + b ); // 0.30000000000000004 Print( 0.3 ); // 0.3 PRINT( DoubleToStringExact(a + b) ); // 0.300000000000000044408920985006261616945266723632812500 PRINT( DoubleToStringExact(0.3) ); // 0.299999999999999988897769753748434595763683319091796875 PRINT( DoubleToHexadecimal(a + b) ); // 0x3FD3333333333334 PRINT( DoubleToHexadecimal(0.3) ); // 0x3FD3333333333333 PRINT( UlpDiff(a + b, 0.3) ); // 1 PRINT( NextAfter(0.3) == a + b ); // true } //+------------------------------------------------------------------+ /* output: Too bad comparison! 0.30000000000000004 0.3 DoubleToStringExact(a+b) = 0.300000000000000044408920985006261616945266723632812500 DoubleToStringExact(0.3) = 0.299999999999999988897769753748434595763683319091796875 DoubleToHexadecimal(a+b) = 0x3FD3333333333334 DoubleToHexadecimal(0.3) = 0x3FD3333333333333 UlpDiff(a+b,0.3) = 1 NextAfter(0.3)==a+b = true */