Double, 6 = 6.00000 & 6 = 5.999999

 

Good evening

I had a little problem recently with the function floor();

I had a value wich I increased with 0.1 everytime I pressed a button. I also used floor() so if the value was like 0.38 it increases just to 0.4 and not to 0.48 the next time I press the button.

But I just couldnt get pass the value 0.5. The original Code: 

double RoundDown(double value, double size)
{   
   return size * floor(value / size);
}

I adjusted it a little with some printf so I could find the problem: (Outputs in the Comments)

double RoundDown(double value, double size)
{   
                                                                                       //input value was 0.60000 
   double x = value/size;  printf("x (%f) = value (%f) / size (%f)", x, value, size);  //6.000000 = 0.600000 / 0.100000    
   x = floor(x);           printf("%f = floor(x)", x);                                 //5.000000 = floor(6.00000)       
   double y = size * x;    printf("y (%f) = size (%f) * x (%f)", y, size, x);          //0.500000 = 0.100000 * 5.000000
   return y;                                                                           //Output is 0.50000
}

After some reserch i found that the problem was with the double... So that my 6 can be 6.0000 but also 5.99999

both a 6 normally, but for the floor() function it's a huge difference. so floor(6.0000) = 6; but floor(5.99999) = 5.


My Solution was to just ad 0.00001 before use floor().

double RoundDown(double value, double size)
{              
   return size * floor(value / size + 0.00001);                                                                               
}

It's also possible to convert the x into a string and extract everything before the decimals.

I'd like to know how you guys handle this problem. what do you have for solutions?

Because I don't really like mine, but it works for now:D

Have a nice Day

Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
  • www.mql5.com
Predefined Macro Substitutions - Named Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Lukas Roth: Double, 6 = 6.00000 & 6 = 5.999999
  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

    See also The == operand. - MQL4 programming forum

 
William Roeder:
  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

    See also The == operand. - MQL4 programming forum

That habe nothing to do with floating point, that is an mql bug since long time

when i increase a value from 0 to x with a step of 0.1 there is no possibillity to get 5.99999

 
amando: That habe nothing to do with floating point, that is an mql bug since long time when i increase a value from 0 to x with a step of 0.1 there is no possibillity to get 5.99999

Really?

#property strict
void OnStart() {
   for(double i=0; i < 9; i += 0.1) Print(i);
}
0 13:41:18.518 Script testscr USDSGD,Daily: loaded successfully
0 13:41:18.557 testscr USDSGD,Daily: initialized
0 13:41:18.580 testscr USDSGD,Daily: 0.0
0 13:41:18.581 testscr USDSGD,Daily: 0.1
0 13:41:18.581 testscr USDSGD,Daily: 0.2
0 13:41:18.581 testscr USDSGD,Daily: 0.3
0 13:41:18.581 testscr USDSGD,Daily: 0.4
0 13:41:18.581 testscr USDSGD,Daily: 0.5
0 13:41:18.581 testscr USDSGD,Daily: 0.6
0 13:41:18.581 testscr USDSGD,Daily: 0.7
0 13:41:18.581 testscr USDSGD,Daily: 0.7999999999999999
0 13:41:18.581 testscr USDSGD,Daily: 0.8999999999999999
0 13:41:18.581 testscr USDSGD,Daily: 0.9999999999999999
0 13:41:18.581 testscr USDSGD,Daily: 1.1
0 13:41:18.581 testscr USDSGD,Daily: 1.2
0 13:41:18.581 testscr USDSGD,Daily: 1.3
0 13:41:18.581 testscr USDSGD,Daily: 1.4
0 13:41:18.581 testscr USDSGD,Daily: 1.5
0 13:41:18.581 testscr USDSGD,Daily: 1.6
0 13:41:18.581 testscr USDSGD,Daily: 1.7
0 13:41:18.581 testscr USDSGD,Daily: 1.8
0 13:41:18.581 testscr USDSGD,Daily: 1.900000000000001
0 13:41:18.581 testscr USDSGD,Daily: 2.0
0 13:41:18.581 testscr USDSGD,Daily: 2.100000000000001
0 13:41:18.581 testscr USDSGD,Daily: 2.200000000000001
0 13:41:18.581 testscr USDSGD,Daily: 2.300000000000001
0 13:41:18.581 testscr USDSGD,Daily: 2.400000000000001
0 13:41:18.581 testscr USDSGD,Daily: 2.500000000000001
0 13:41:18.581 testscr USDSGD,Daily: 2.600000000000001
0 13:41:18.581 testscr USDSGD,Daily: 2.700000000000001
0 13:41:18.581 testscr USDSGD,Daily: 2.800000000000001
0 13:41:18.581 testscr USDSGD,Daily: 2.900000000000001
0 13:41:18.581 testscr USDSGD,Daily: 3.000000000000001
0 13:41:18.581 testscr USDSGD,Daily: 3.100000000000001
0 13:41:18.581 testscr USDSGD,Daily: 3.200000000000002
0 13:41:18.581 testscr USDSGD,Daily: 3.300000000000002
0 13:41:18.581 testscr USDSGD,Daily: 3.400000000000002
0 13:41:18.581 testscr USDSGD,Daily: 3.500000000000002
0 13:41:18.581 testscr USDSGD,Daily: 3.600000000000002
0 13:41:18.581 testscr USDSGD,Daily: 3.700000000000002
0 13:41:18.581 testscr USDSGD,Daily: 3.800000000000002
0 13:41:18.581 testscr USDSGD,Daily: 3.900000000000002
0 13:41:18.581 testscr USDSGD,Daily: 4.000000000000002
0 13:41:18.581 testscr USDSGD,Daily: 4.100000000000001
0 13:41:18.581 testscr USDSGD,Daily: 4.200000000000001
0 13:41:18.581 testscr USDSGD,Daily: 4.300000000000001
0 13:41:18.581 testscr USDSGD,Daily: 4.4
0 13:41:18.581 testscr USDSGD,Daily: 4.5
0 13:41:18.581 testscr USDSGD,Daily: 4.6
0 13:41:18.581 testscr USDSGD,Daily: 4.699999999999999
0 13:41:18.581 testscr USDSGD,Daily: 4.799999999999999
0 13:41:18.581 testscr USDSGD,Daily: 4.899999999999999
0 13:41:18.581 testscr USDSGD,Daily: 4.999999999999998
0 13:41:18.581 testscr USDSGD,Daily: 5.099999999999998
0 13:41:18.581 testscr USDSGD,Daily: 5.199999999999998
0 13:41:18.581 testscr USDSGD,Daily: 5.299999999999997
0 13:41:18.581 testscr USDSGD,Daily: 5.399999999999997
0 13:41:18.581 testscr USDSGD,Daily: 5.499999999999996
0 13:41:18.581 testscr USDSGD,Daily: 5.599999999999996
0 13:41:18.581 testscr USDSGD,Daily: 5.699999999999996
0 13:41:18.581 testscr USDSGD,Daily: 5.799999999999995
0 13:41:18.581 testscr USDSGD,Daily: 5.899999999999995
0 13:41:18.581 testscr USDSGD,Daily: 5.999999999999995
0 13:41:18.581 testscr USDSGD,Daily: 6.099999999999994
0 13:41:18.581 testscr USDSGD,Daily: 6.199999999999994
0 13:41:18.581 testscr USDSGD,Daily: 6.299999999999994
0 13:41:18.581 testscr USDSGD,Daily: 6.399999999999993
0 13:41:18.581 testscr USDSGD,Daily: 6.499999999999993
0 13:41:18.581 testscr USDSGD,Daily: 6.599999999999993
0 13:41:18.581 testscr USDSGD,Daily: 6.699999999999992
0 13:41:18.581 testscr USDSGD,Daily: 6.799999999999992
0 13:41:18.581 testscr USDSGD,Daily: 6.899999999999991
0 13:41:18.581 testscr USDSGD,Daily: 6.999999999999991
0 13:41:18.581 testscr USDSGD,Daily: 7.099999999999991
0 13:41:18.581 testscr USDSGD,Daily: 7.19999999999999
0 13:41:18.581 testscr USDSGD,Daily: 7.29999999999999
0 13:41:18.581 testscr USDSGD,Daily: 7.39999999999999
0 13:41:18.581 testscr USDSGD,Daily: 7.499999999999989
0 13:41:18.581 testscr USDSGD,Daily: 7.599999999999989
0 13:41:18.581 testscr USDSGD,Daily: 7.699999999999989
0 13:41:18.581 testscr USDSGD,Daily: 7.799999999999988
0 13:41:18.581 testscr USDSGD,Daily: 7.899999999999988
0 13:41:18.581 testscr USDSGD,Daily: 7.999999999999988
0 13:41:18.581 testscr USDSGD,Daily: 8.099999999999987
0 13:41:18.581 testscr USDSGD,Daily: 8.199999999999987
0 13:41:18.581 testscr USDSGD,Daily: 8.299999999999986
0 13:41:18.581 testscr USDSGD,Daily: 8.399999999999986
0 13:41:18.581 testscr USDSGD,Daily: 8.499999999999986
0 13:41:18.581 testscr USDSGD,Daily: 8.599999999999985
0 13:41:18.581 testscr USDSGD,Daily: 8.699999999999985
0 13:41:18.581 testscr USDSGD,Daily: 8.799999999999985
0 13:41:18.581 testscr USDSGD,Daily: 8.899999999999984
0 13:41:18.581 testscr USDSGD,Daily: 8.999999999999984
0 13:41:18.581 testscr USDSGD,Daily: uninit reason 0
0 13:41:18.622 Script testscr USDSGD,Daily: removed
 
William Roeder:

Really?

0 13:41:18.518 Script testscr USDSGD,Daily: loaded successfully
0 13:41:18.557 testscr USDSGD,Daily: initialized
0 13:41:18.580 testscr USDSGD,Daily: 0.0
0 13:41:18.581 testscr USDSGD,Daily: 0.1
0 13:41:18.581 testscr USDSGD,Daily: 0.2
0 13:41:18.581 testscr USDSGD,Daily: 0.3
0 13:41:18.581 testscr USDSGD,Daily: 0.4
0 13:41:18.581 testscr USDSGD,Daily: 0.5
0 13:41:18.581 testscr USDSGD,Daily: 0.6
0 13:41:18.581 testscr USDSGD,Daily: 0.7
0 13:41:18.581 testscr USDSGD,Daily: 0.7999999999999999
0 13:41:18.581 testscr USDSGD,Daily: 0.8999999999999999
0 13:41:18.581 testscr USDSGD,Daily: 0.9999999999999999
0 13:41:18.581 testscr USDSGD,Daily: 1.1
0 13:41:18.581 testscr USDSGD,Daily: 1.2
0 13:41:18.581 testscr USDSGD,Daily: 1.3
0 13:41:18.581 testscr USDSGD,Daily: 1.4
0 13:41:18.581 testscr USDSGD,Daily: 1.5
0 13:41:18.581 testscr USDSGD,Daily: 1.6
0 13:41:18.581 testscr USDSGD,Daily: 1.7
0 13:41:18.581 testscr USDSGD,Daily: 1.8
0 13:41:18.581 testscr USDSGD,Daily: 1.900000000000001
0 13:41:18.581 testscr USDSGD,Daily: 2.0
0 13:41:18.581 testscr USDSGD,Daily: 2.100000000000001
0 13:41:18.581 testscr USDSGD,Daily: 2.200000000000001
0 13:41:18.581 testscr USDSGD,Daily: 2.300000000000001
0 13:41:18.581 testscr USDSGD,Daily: 2.400000000000001
0 13:41:18.581 testscr USDSGD,Daily: 2.500000000000001
0 13:41:18.581 testscr USDSGD,Daily: 2.600000000000001
0 13:41:18.581 testscr USDSGD,Daily: 2.700000000000001
0 13:41:18.581 testscr USDSGD,Daily: 2.800000000000001
0 13:41:18.581 testscr USDSGD,Daily: 2.900000000000001
0 13:41:18.581 testscr USDSGD,Daily: 3.000000000000001
0 13:41:18.581 testscr USDSGD,Daily: 3.100000000000001
0 13:41:18.581 testscr USDSGD,Daily: 3.200000000000002
0 13:41:18.581 testscr USDSGD,Daily: 3.300000000000002
0 13:41:18.581 testscr USDSGD,Daily: 3.400000000000002
0 13:41:18.581 testscr USDSGD,Daily: 3.500000000000002
0 13:41:18.581 testscr USDSGD,Daily: 3.600000000000002
0 13:41:18.581 testscr USDSGD,Daily: 3.700000000000002
0 13:41:18.581 testscr USDSGD,Daily: 3.800000000000002
0 13:41:18.581 testscr USDSGD,Daily: 3.900000000000002
0 13:41:18.581 testscr USDSGD,Daily: 4.000000000000002
0 13:41:18.581 testscr USDSGD,Daily: 4.100000000000001
0 13:41:18.581 testscr USDSGD,Daily: 4.200000000000001
0 13:41:18.581 testscr USDSGD,Daily: 4.300000000000001
0 13:41:18.581 testscr USDSGD,Daily: 4.4
0 13:41:18.581 testscr USDSGD,Daily: 4.5
0 13:41:18.581 testscr USDSGD,Daily: 4.6
0 13:41:18.581 testscr USDSGD,Daily: 4.699999999999999
0 13:41:18.581 testscr USDSGD,Daily: 4.799999999999999
0 13:41:18.581 testscr USDSGD,Daily: 4.899999999999999
0 13:41:18.581 testscr USDSGD,Daily: 4.999999999999998
0 13:41:18.581 testscr USDSGD,Daily: 5.099999999999998
0 13:41:18.581 testscr USDSGD,Daily: 5.199999999999998
0 13:41:18.581 testscr USDSGD,Daily: 5.299999999999997
0 13:41:18.581 testscr USDSGD,Daily: 5.399999999999997
0 13:41:18.581 testscr USDSGD,Daily: 5.499999999999996
0 13:41:18.581 testscr USDSGD,Daily: 5.599999999999996
0 13:41:18.581 testscr USDSGD,Daily: 5.699999999999996
0 13:41:18.581 testscr USDSGD,Daily: 5.799999999999995
0 13:41:18.581 testscr USDSGD,Daily: 5.899999999999995
0 13:41:18.581 testscr USDSGD,Daily: 5.999999999999995
0 13:41:18.581 testscr USDSGD,Daily: 6.099999999999994
0 13:41:18.581 testscr USDSGD,Daily: 6.199999999999994
0 13:41:18.581 testscr USDSGD,Daily: 6.299999999999994
0 13:41:18.581 testscr USDSGD,Daily: 6.399999999999993
0 13:41:18.581 testscr USDSGD,Daily: 6.499999999999993
0 13:41:18.581 testscr USDSGD,Daily: 6.599999999999993
0 13:41:18.581 testscr USDSGD,Daily: 6.699999999999992
0 13:41:18.581 testscr USDSGD,Daily: 6.799999999999992
0 13:41:18.581 testscr USDSGD,Daily: 6.899999999999991
0 13:41:18.581 testscr USDSGD,Daily: 6.999999999999991
0 13:41:18.581 testscr USDSGD,Daily: 7.099999999999991
0 13:41:18.581 testscr USDSGD,Daily: 7.19999999999999
0 13:41:18.581 testscr USDSGD,Daily: 7.29999999999999
0 13:41:18.581 testscr USDSGD,Daily: 7.39999999999999
0 13:41:18.581 testscr USDSGD,Daily: 7.499999999999989
0 13:41:18.581 testscr USDSGD,Daily: 7.599999999999989
0 13:41:18.581 testscr USDSGD,Daily: 7.699999999999989
0 13:41:18.581 testscr USDSGD,Daily: 7.799999999999988
0 13:41:18.581 testscr USDSGD,Daily: 7.899999999999988
0 13:41:18.581 testscr USDSGD,Daily: 7.999999999999988
0 13:41:18.581 testscr USDSGD,Daily: 8.099999999999987
0 13:41:18.581 testscr USDSGD,Daily: 8.199999999999987
0 13:41:18.581 testscr USDSGD,Daily: 8.299999999999986
0 13:41:18.581 testscr USDSGD,Daily: 8.399999999999986
0 13:41:18.581 testscr USDSGD,Daily: 8.499999999999986
0 13:41:18.581 testscr USDSGD,Daily: 8.599999999999985
0 13:41:18.581 testscr USDSGD,Daily: 8.699999999999985
0 13:41:18.581 testscr USDSGD,Daily: 8.799999999999985
0 13:41:18.581 testscr USDSGD,Daily: 8.899999999999984
0 13:41:18.581 testscr USDSGD,Daily: 8.999999999999984
0 13:41:18.581 testscr USDSGD,Daily: uninit reason 0
0 13:41:18.622 Script testscr USDSGD,Daily: removed

And you think this is correct? Sorry, that is a bug, if you dont see this feel free. But this is wrong

 
amando: And you think this is correct? Sorry, that is a bug, if you dont see this feel free. But this is wrong

It is reality. 1/10 can not be represented as a double.

 
William Roeder:

It is reality. 1/10 can not be represented as a double.

 An you explain why? Its easy, your wrong

 
amando #:

 An you explain why? Its easy, your wrong

No he's not wrong, William Roeders answear is correct.

I didn't understand it till i had it in my study this week.

This is not a bug of MQL. It's because of how doubles/floats are stored:


have a look here:

https://www.trimble.com/OEM_ReceiverHelp/V4.44/en/FloatingPointDataTypes.html#:~:text=6%20decimal%20digits.-,DOUBLE,of%20the%20exponent%20is%201023.


PS: thanks William for the answear :)

Reason: