Strange result, is it a bug?

 

Hi guys,

This code:

int a=50;
double c=2.55;

Comment(a*c," / ",MathRound(a*c)," # ",MathRound(127.5));

Gives this: 127,5 / 127.0 # 128.0

Why not: 127,5 / 128.0 # 128.0?

 
Paul Anscombe:

to do with the way floating points work, try adding 0.1 before rounding

Pretty absurd.

So, you mean it is a bug?

 
linux80s:

Pretty absurd.

So, you mean it is a bug?

it's a common issue not limited to MQL 

you just have to deal with it

William has a whole thing he has posted on floating point numbers here, have a little dig
 
Paul Anscombe:

it's a common issue not limited to MQL 

you just have to deal with it

William has a whole thing he has posted on floating point numbers here, have a little dig

What's the difference between 50*2.55 and 127.5 ?

What's the difference between MathRound(50*2.55) and MathRound(127.5) ?

 
linux80s:

What's the difference between 50*2.55 and 127.5 ?

What's the difference between MathRound(50*2.55) and MathRound(127.5) ?

like I said just do a little digging if you want the detail, plenty of coverage on the internet and some here.

But 2.55 is not necessarily 2.55 and 50*2.55 is not necessarily 127.5  because the decimal precision is only for your eyes in real terms we are dealing with floating point numbers....  try google   

so this simple bit of code

  for(int iC=0.0; iC < 20; iC++)
   {
      Print(iC*0.1 + "  " + MathRound(iC*0.1));
   }
   

produces this output....    see the problem

2021.01.29 18:26:24.386 Scratch (UsaInd-sb,D1) 0.0  0.0

2021.01.29 18:26:24.386 Scratch (UsaInd-sb,D1) 0.1  0.0

2021.01.29 18:26:24.386 Scratch (UsaInd-sb,D1) 0.2  0.0

2021.01.29 18:26:24.386 Scratch (UsaInd-sb,D1) 0.3  0.0

2021.01.29 18:26:24.386 Scratch (UsaInd-sb,D1) 0.4  0.0

2021.01.29 18:26:24.386 Scratch (UsaInd-sb,D1) 0.5  1.0

2021.01.29 18:26:24.386 Scratch (UsaInd-sb,D1) 0.6000000000000001  1.0

2021.01.29 18:26:24.386 Scratch (UsaInd-sb,D1) 0.7000000000000001  1.0

2021.01.29 18:26:24.386 Scratch (UsaInd-sb,D1) 0.8  1.0

2021.01.29 18:26:24.386 Scratch (UsaInd-sb,D1) 0.9  1.0



 
Paul Anscombe:

like I said just do a little digging if you want the detail, plenty of coverage on the internet and some here.

But 2.55 is not necessarily 2.55 and 50*2.55 is not necessarily 127.5  because the decimal precision is only for your eyes in real terms we are dealing with floating point numbers....  try google   

so this simple bit of code

produces this output....    see the problem

2021.01.29 18:26:24.386 Scratch (UsaInd-sb,D1) 0.0  0.0

2021.01.29 18:26:24.386 Scratch (UsaInd-sb,D1) 0.1  0.0

2021.01.29 18:26:24.386 Scratch (UsaInd-sb,D1) 0.2  0.0

2021.01.29 18:26:24.386 Scratch (UsaInd-sb,D1) 0.3  0.0

2021.01.29 18:26:24.386 Scratch (UsaInd-sb,D1) 0.4  0.0

2021.01.29 18:26:24.386 Scratch (UsaInd-sb,D1) 0.5  1.0

2021.01.29 18:26:24.386 Scratch (UsaInd-sb,D1) 0.6000000000000001  1.0

2021.01.29 18:26:24.386 Scratch (UsaInd-sb,D1) 0.7000000000000001  1.0

2021.01.29 18:26:24.386 Scratch (UsaInd-sb,D1) 0.8  1.0

2021.01.29 18:26:24.386 Scratch (UsaInd-sb,D1) 0.9  1.0



Strange, thanks. It seems we ALWAYS need to prepare the floating values, no matter how those look like.