Is it possible to get an "exact" value? - page 3

 
abolk:
Checked your function -- leaves 0.02.

Is my cutoff right?

void OnStart()
  {
//---
   int DIGITS=2;
   string stroka=DoubleToString(0.0199999999999,8);
   int P=StringFind(stroka,".");
   Print(stroka);
   stroka=StringSubstr(stroka,0,P+DIGITS+1);
   Print(StringToDouble(stroka));
   Print(NL(DIGITS,0.0199999999999));
   Print(Normalize (0.0199999999999, 2));
  }
//+------------------------------------------------------------------+
double NL (int DIGITS,double value) {
   double step=1/MathPow(10,DIGITS);
   return (MathFloor(value/step)*step);
}
string Normalize (string Normalize_Parse, int Normalize_Number, string Normalize_Separator = ".")
{
   string Normalize_Before,
          Normalize_After;
   
   for (int i = 0; i <= StringLen (Normalize_Parse) - 1; i ++)
   {
      if (StringSubstr (Normalize_Parse, i, StringLen (Normalize_Separator)) == Normalize_Separator)
      {
         for (int ii = i + StringLen (Normalize_Separator); ii < i + StringLen (Normalize_Separator) + Normalize_Number; ii ++)
         {
            Normalize_After += StringSubstr (Normalize_Parse, ii, 1);
         }
         
         break;
      }
      
      Normalize_Before += StringSubstr (Normalize_Parse, i, 1);
   }
   
   return (Normalize_Before + (Normalize_After > 0 ? Normalize_Separator + Normalize_After : ""));
}


2014.10.04 11:56:06.199 normalize EURUSD,H4: 0.01
2014.10.04 11:56:06.199 normalize EURUSD,H4: 0.01
2014.10.04 11:56:06.199 normalize EURUSD,H4: 0.02
2014.10.04 11:56:06.199 normalize EURUSD,H4: 0.02000000
 
WePlexus:
How can your function be optimised to take 0.01 from theabolk example?

which function do you mean?

first through the string or NL?

 
sanyooooook:

and what's my condition cutting it right?

you take my condition and don't cut it:

abolk:
how to take =0.01999999999999999999 -- leave =0.01
 
abolk:

you take my condition and don't cut it:

it's only 8 digits, the compiler seems to normalize the number 0.01999999999999999999 to 0.02

so the input is 0.02.

 
   int DIGITS=2;
   Print(0.01999999999999999999);
   string stroka=DoubleToString(0.01999999999999999999,8);
   int P=StringFind(stroka,".");
   Print(stroka);
   stroka=StringSubstr(stroka,0,P+DIGITS+1);
   Print(StringToDouble(stroka));
   Print(NL(DIGITS,0.01999999999999999999));
   Print(Normalize (0.01999999999999999999, 2));

gives

2014.10.04 12:17:13.637 normalize EURUSD,H4: 0.02
2014.10.04 12:17:13.637 normalize EURUSD,H4: 0.02
2014.10.04 12:17:13.637 normalize EURUSD,H4: 0.02
2014.10.04 12:17:13.637 normalize EURUSD,H4: 0.02000000
2014.10.04 12:17:13.637 normalize EURUSD,H4: 0.02
 

)))), I'm sorry.

doble has 15 significant digits and you have more coming in

 
sanyooooook:

)))), I'm sorry.

doble has 15 significant digits and you have more coming in

Well, finally -- there's some constructive discussion of the problem.
 
sanyooooook:

)))), I'm sorry.

doble has 15 significant digits and you have more coming in.

here's my solution:

double d=0.01999999999999999;
double e=(int)(d*100)/100.0;
Print("e=",e);

gives =0.01

and if you add a 9:

double d=0.019999999999999999;
double e=(int)(d*100)/100.0;
Print("e=",e);
that's =0.02.
 
abolk:
Well, finally -- there's some constructive discussion of the problem.
Why do you need this. Data overflow.
 
abolk:

here's my solution:

d*100

This operation can distort the numerical value

Reason: