GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

No menu could be recovered. Follow the blue links in the page.

This view

  • Original page
  • New address

GeezerVison is showing javascript.info — An occasional infinite loop

  • Tutorial
  • The JavaScript language
  • Data types
  • Numbers
back to the lesson

An occasional infinite loop

importance: 4

This loop is infinite. It never ends. Why?

let i = 0;
while (i != 10) {
  i += 0.2;
}

That’s because i would never equal 10.

Run it to see the real values of i:

let i = 0;
while (i < 11) {
  i += 0.2;
  if (i > 9.8 && i < 10.2) alert( i );
}

None of them is exactly 10.

Such things happen because of the precision losses when adding fractions like 0.2.

Conclusion: evade equality checks when working with decimal fractions.

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.