0
for (double i = 0; i < 0.05; i+= 0.001)
{
            //do something
}

In this for loop,

I thought the count would go as follows: 0, 0.001, 0.002, 0.003, etc., but it becomes 0.0090000000000000011 instead of 0.009.

Why is such conversion possible?

Ivan
  • 399
  • 2
  • 5
  • 18
Vintony
  • 9
  • 1

1 Answers1

2

The resolution of the double/float is not infinite.

See here

Because some numbers cannot be represented exactly as fractional binary values, floating-point numbers can only approximate real numbers.

G. B.
  • 528
  • 2
  • 15