problem with normalizedouble?

 

i have a problem with normalizedouble in csv file :
226.82;-3.26;-9.890000000000001
226.64;-3.34;-10.13
226.7;-3.31;-10.05
226.79;-3.28;-9.94
226.72;-3.31;-10.03
226.44;-3.42;-10.15
226.78;-3.28;-9.539999999999999
227.45;-2.99;-9.109999999999999
227.29;-3.06;-9.220000000000001

WTF..... why sometime  normalizedouble  doesn't works????????????????????????????????????


how this is possible?????????????????

here is my code:

thanks for any help


                

 
//--- write a file of equity if strategy active (total>1)
   double oilpricestart=43.539;
   double capitalstart=234.47;
   double capital=NormalizeDouble(AccountEquity(),2);
   double capital_percent=capitalstart/100;
   double performance=(capital-capitalstart)/capital_percent;
   double perfNormalized=NormalizeDouble(performance,2);
   double oilpricepercent=oilpricestart/100;
   double oilperformance=(oilpricestart-Ask)/oilpricepercent;
   double oilperfNormalized=NormalizeDouble(oilperformance,2);

   if(TimeCurrent()>TimesignalA)
      if(OrdersTotal()>0)
        {
         //---- add data to the end of file each 900 second interval
         TimesignalA=TimeCurrent()+900;

         int handle=FileOpen("timetosuccessSHORT.csv",FILE_CSV|FILE_READ|FILE_WRITE,';');
         if(handle>0)
           {
            FileWrite(handle,"05-05-16 INITIAL EQYUITY = 234.47€ WTICOUSD 43.539","strategy return % since start","oil performance % since start");
            FileSeek(handle,0,SEEK_END);
            FileWrite(handle,capital,perfNormalized,oilperfNormalized);
            FileClose(handle);
            handle=0;
           }

        }
 

Doubles and floats are represented as the closest number of a mantissa and an exponent (https://en.wikipedia.org/wiki/Double-precision_floating-point_format).

So even 0 is not 0 but (e.g.) 0.00000000000000000000001 or 3 not 3 but (e.g.) 2.9999999999999999999999999989.

And even after normalizing a double it is still a double!

 
Use DoubleToStr() when writing to a file or Print
 
thank you all
 
  1. 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
  2. Do NOT use NormalizeDouble, EVER. For ANY Reason. It's a kludge, don't use it. It's use is always wrong
 
gooly:

Doubles and floats are represented as the closest number of amantissa and an exponent(https://en.wikipedia.org/wiki/Double-precision_floating-point_format).

So even 0 is not 0 but (e.g.) 0.00000000000000000000001 or 3 not 3 but (e.g.) 2.9999999999999999999999999989.

And even after normalizing a double it is still a double!

0 is just 0 of course. And there is also no problem with 3, you should chose your examples better ;-)
 
angevoyageur:
0 is just 0 of course.

And why did we had some discussions here that the comparison if ( a==0 ) failed?

 
gooly:

And why did we had some discussions here that the comparisons failed:


The code you posted can't "failed", it's always true.

Do you have some link(s) ?

 
angevoyageur:

The code you posted can't "failed", it's always true.

Do you have some link(s) ?

double a = 0.0;
if (a == 0.0 ) {

will work as a and 0.0 do have the same representation of 0.0 - wait a minute..

 

Here and here: "except with zero never compare doubles for equality" (WHRoeder).

But what if you have two doubles a,b you shouldn't do if (a==b) but if (a-b==0.0) should work?

But what if this works 99 times and then it fails?

Reason: