Normalize double error

 

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
 
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);


 

 
Do NOT use NormalizeDouble, EVER. For ANY Reason. It's a kludge, don't use it. It's use is always wrong
 

LoL guys you have been very helpfull!

Thanks a lot.

Reason: