-1

I ma using MT4 but it might be the general question of floating number.

I am using NormalizeDouble function which rounds the digit of numbers like this.

double x = 1.33242
y = NormalizeDouble(x,2) // y is 1.33

However in some case. Even after rounded by NormalizeDouble, there happens a number such us 0.800000001

I have no idea why it happens and how to fix it.

It might be a basic mathematical thing.

whitebear
  • 11,200
  • 24
  • 114
  • 237

1 Answers1

1

You are truncating to powers of 10 but fractional part of float/double can express exactly only powers of 2 like

0.5,0.25,0.125,...

and numbers decomposable to them hence your case:

0.8 = 1/2+1/4 +1/32   +1/64    +1/512      +1/1024      +1/8192         +1/16384...
    = 0.5+0.25+0.03125+0.015625+0.001953125+0.0009765625+0.0001220703125+0.00006103515625...
    = 0.11001100110011... [bin]

as 0.3 is like periodic number in binary and will always cause some noise in lower bits of mantissa. The FPU implementation tries to find the closest number to your desired value hence the 0.800000001

Spektre
  • 49,595
  • 11
  • 110
  • 380