NormalizeDouble() not always rounding

 

The NormalizeDouble function almost always rounds to 4 places, but every once in a while I get (ROC is: 0.0005999999999999999) some non-rounded number.

Can someone enlighten me why this is and how to fix?

Thanks.

void OnTick()
  {
    
      double Price1 = iClose("USDCHF",1,1);
      double Price2 = iClose("USDCHF",1,2);
     
      double ROC;
    
      
      if(Price1 > Price2) {
        ROC = Price1 - Price2;
      }
            
      if(Price1 < Price2) {
         ROC = Price2 - Price1;
      }
      
      
      double  ROC_Rounded = NormalizeDouble(ROC, 4);
         
     
      Comment("ROC is: ", ROC_Rounded);
  }
 
afiverestatcastle:

The NormalizeDouble function almost always rounds to 4 places, but every once in a while I get (ROC is: 0.0005999999999999999) some non-rounded number.

Can someone enlighten me why this is and how to fix?

Thanks.

Try this

Comment("ROC is: ", NormalizeDouble(ROC_Rounded,4));

some fractional numbers cannot be accurately stored on a computer.

 
#include <stdlib.mqh>




void OnTick()
  {
    
      double Price1 = iClose("USDCHF",1,1);
      double Price2 = iClose("USDCHF",1,2);
     
      double ROC;
    
      
      if(Price1 > Price2) {
        ROC = Price1 - Price2;
      }
            
      if(Price1 < Price2) {
         ROC = Price2 - Price1;
      }
      
      
      double  ROC_Rounded = NormalizeDouble(ROC, 4);
         
     
      Comment("ROC is: ", DoubleToStrMorePrecision(ROC_Rounded,4));
  }
this will solve your issue. :)
 
afiverestatcastle: The NormalizeDouble function almost always rounds to 4 places, 
  1. No it does not. It rounds to the digits specifies and returns a double. Floating point has infinite number of decimals, it's your not understanding floating point and that some numbers can't be represented exactly. (like 1/10.)

              Double-precision floating-point format - Wikipedia, the free encyclopedia

    See also The == operand. - MQL4 programming forum

    Print out your values to the precision you want with DoubleToString - Conversion Functions - MQL4 Reference.

  2. You used NormalizeDouble, It's use is usually wrong, as it is in your case.

 
afiverestatcastle:

The NormalizeDouble function almost always rounds to 4 places, but every once in a while I get (ROC is: 0.0005999999999999999) some non-rounded number.

Can someone enlighten me why this is and how to fix?

Thanks.

This is the way how 0.0006 is represented internally in memory. Try this code:

Print(0.0006);  // 0.0005999999999999

The solution for your code is:

Print(DoubleToString(0.0006,4)) //0.0006

The above problem is not related to NormalizeDouble() or MathRound() in any way.

 
amrali:

This is the way how 0.0006 is represented internally in memory. Try this code:

The solution for your code is:

The above problem is not related to NormalizeDouble() or MathRound() in any way.

DoubleToString good just for Print. 
 

Thanks you all for your help.


I will need a few days to study all of your comments.

 
afiverestatcastle:

Thanks you all for your help.


I will need a few days to study all of your comments.

Just use this to save your time
Comment("ROC is: ", DoubleToStrMorePrecision(ROC_Rounded,4));
  
 

what about when i am normalising lotsizes, take profit and stop losses? In this case i cant use strings (DoubleToString)..!

 
Julius Mwangi:

what about when i am normalising lotsizes, take profit and stop losses? In this case i cant use strings (DoubleToString)..!

You only use DoubleToString to print, not in calculations.

 
Keith Watford:

You only use DoubleToString to print, not in calculations.

Thanks a lot. Well understood
Reason: