Storing 0.07 as a double - impossible???

 

Is it just me, or can 0.07 not be stored accurately?

 

 Example

double number=0.07;
Comment(number);

 

This displays as 0.07000000000000001, therefore I can for instance send an order using a lot size of 0.07.


 

Well, you can try ...

Comment(NormalizeDouble(number,2));
 

First thing I tried. Doesn't help at all.

 

I've even wrote my own normalise function that didn't help either.

 

This makes the following command impossible, even though it's perfectly valid

OrderSend(Symbol(),OP_BUY,0.07,Ask,3,0,0,NULL,0,0,CLR_NONE);
 
double number=0.07;

OrderSend(Symbol(),OP_BUY,number,Ask,3,0,0,NULL,0,0,CLR_NONE);
It works for me.
 
Paul_B:

First thing I tried. Doesn't help at all.

 

I've even wrote my own normalise function that didn't help either.

 

This makes the following command impossible, even though it's perfectly valid

 

What is minlot and lotstep for your broker?

https://docs.mql4.com/constants/environment_state/marketinfoconstants 

 
Paul_B:

First thing I tried. Doesn't help at all.

 

I've even wrote my own normalise function that didn't help either.

 

This makes the following command impossible, even though it's perfectly valid

 

OrderSend(Symbol(),OP_BUY,0.07,Ask,3,0,0,NULL,0,0,CLR_NONE);

The OrderSend will work perfectly fine with the constant 0.07, it is only when it is calculated that there might be a problem

ie

double a=1.4;
double b=2.0;
double lotsize =a/b;
OrderSend(Symbol(),OP_BUY,lotsize,Ask,3,0,0,NULL,0,0,CLR_NONE);

 May or may not work as intended

 
Paul_B: Is it just me, or can 0.07 not be stored accurately?
  1. Correct, it can not be stored accurately - because 1/10 is not a multiple of powers of two. 1/8 can be. See https://en.wikipedia.org/wiki/Single-precision_floating-point_format (Ex 2: Consider a value 0.25)
  2. Do NOT use NormalizeDouble, EVER. For ANY Reason. It's a kludge, don't use it. It's use is always wrong. Wrong as given here:
    deysmacro: Well, you can try ...
    Comment(NormalizeDouble(number,2));
    Taking a double, normalizing it to 2 digits, give you a double - same problem - does nothing.
  3. Comment(DoubleToString(number,2));

 
WHRoeder:
  1. Do NOT use NormalizeDouble, EVER. For ANY Reason. It's a kludge, don't use it. It's use is always wrong. Wrong as given here:
    deysmacro: Well, you can try ...
    Taking a double, normalizing it to 2 digits, give you a double - same problem - does nothing


It's not like I don't see that coming long before I post it.



 
deysmacro:


It's not like I don't see that coming long before I post it.



Don't worry, NormalizeDouble works perfectly for what is intended.
 

So how exactly should we do it if its calculated rather than declared as a constant for the "0.07":

int a =7;

int b = 100;

double c = a/b;


OrderSend(Symbol(),OP_BUY,c,Ask,3,0,0,NULL,0,0,CLR_NONE);

I also understand that true numbers such as 0.07 can't really be stored.

 I've read several forums suggesting to use the DoubleToString(), but the OrderSend Function needs "double" not string. So I just can't find the solution if my EA calculated these type of numbers what is the solution to that?

 
Daily Inov #:

So how exactly should we do it if its calculated rather than declared as a constant for the "0.07":

int a =7;

int b = 100;

double c = a/b;


I also understand that true numbers such as 0.07 can't really be stored.

 I've read several forums suggesting to use the DoubleToString(), but the OrderSend Function needs "double" not string. So I just can't find the solution if my EA calculated these type of numbers what is the solution to that?

Still wrong : int divide by int is int that is put in double which is then 0.   double c = (double)a/b;  // correct

Reason: