Download MetaTrader 5

Normalize double error

To add comments, please log in or register
No time to learn MQL5? The source code library is at your service!
Luis Alejandro Diaz Vidal
2222
Luis Alejandro Diaz Vidal 2016.11.30 12:45 

Why this is happening?

http://screencast.com/t/IVUWE3n2

I use the next codeline but some values are correct and others not, why it could be?

NormalizeDouble(value,1);
2016-11-30_1241
2016-11-30_1241
  • www.screencast.com
Shared from Screencast.com
honest_knave
Moderator
2208
honest_knave 2016.11.30 13:48  
Luis Alejandro Diaz Vidal:

Why this is happening?

http://screencast.com/t/IVUWE3n2

I use the next codeline but some values are correct and others not, why it could be?

NormalizeDouble(value,1);

It's to do with how the values get stored in binary. There has been a rather protracted discussion about this on another thread yesterday.

For example, you might think you can store 0.1 exactly. Unfortunately, you can't.

double value=0.1;
printf("%.20f",value);

Result: 0.10000000000000000555

This is a good explanation of why that happens.

 

The solution is to accept that many numbers don't have a finite representation in binary and just control the output when you want to display the number e.g.

DoubleToStr(value,1);


 

whroeder1
14504
whroeder1 2016.11.30 13:59  
Do NOT use NormalizeDouble, EVER. For ANY Reason. It's a kludge, don't use it. It's use is always wrong
Luis Alejandro Diaz Vidal
2222
Luis Alejandro Diaz Vidal 2016.11.30 15:38  

LoL guys you have been very helpfull!

Thanks a lot.

To add comments, please log in or register