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 fa.javascript.info — یک حلقه‌ی بی‌نهایت

  • آموزش
  • زبان جاوااسکریپت
  • انواع داده
  • اعداد
بازگشت به درس

یک حلقه‌ی بی‌نهایت

اهمیت: 4

این حلقه بی‌نهایت است. هیچوقت تمام نمی‌شود. چرا؟

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

به این علت است که i هیچوقت برابر ده نمیشود.

این تکه کد را اجرا کنید تا مقدار حقیقی i را ببینید:

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

هیچکدام از آنها دقیقا ده نیست.

چنین اتفاقاتی به علت از دست رفتن دقت در حین جمع کردن کسرهایی مثل 0.2 رخ می‌دهد.

نتیجه: وقتی با کسرهای اعشاری کار میکنید، از تساوی استفاده نکنید.

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.