-1

How this loop is performing and why it doesn't print 2.399999999999999

i = 1.2

while i < 2.4:
    print(i)
    i += 0.2

Also when I tried :

2.39999999999999999 < 2.4

I got False

But when I tried :

2.399999999999999 < 2.4

I got True

Can anyone explain why it is so ?

Mark Dickinson
  • 29,088
  • 9
  • 83
  • 120
Deepak Tripathi
  • 3,175
  • 1
  • 8
  • 21

1 Answers1

1

This is because computer architecture either rounds the number or truncate the number. In this scenario, if we see architecture level either considering the number equals to 2.4 or greater than just by Machine epsilon.

It is good to have a look at the concept of Machine Epsilon.

bugger
  • 31
  • 4