LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 10-27-2010, 02:52 PM   #1
frieza
Senior Member
 
Registered: Feb 2002
Location: harvard, il
Distribution: Ubuntu 11.4,DD-WRT micro plus ssh,lfs-6.6,Fedora 15,Fedora 16
Posts: 3,111

Rep: Reputation: 371Reputation: 371Reputation: 371Reputation: 371
infinite loop that isnt infinite?


i was playing around with c++ recently and for laughs and giggles i came up with this little program
Code:
#include <iostream>
using namespace std;
int main ()
{
  for (long double n=100; n>0; n = n/2) {
    cout << n << "\n ";
  }
  cout << "FIRE!\n";
  return 0;
}
theoretically that should be an infinite loop but it in fact does terminate eventually echoing out 'FIRE!'

just wondering if even though in theory this should be an infinite loop (a number repetedly divided by 2 should never reach 0 thus the conditions of the loop should never be satisfied, hence infinite loop) perhaps in practice limitations of the variable types prevent this?

no this is not homework just a question to help understand the mechanics of variables and loops
 
Old 10-27-2010, 03:11 PM   #2
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

Quote:
this is not homework just a question to help understand the mechanics of variables and loops
Actually, the answer to your question has to do with the pitfalls of "floating point precision" :

http://www.cygnus-software.com/paper...ringfloats.htm

PS:
On my system, I got down to "3.6452e-4951"

Last edited by paulsm4; 10-27-2010 at 03:15 PM.
 
1 members found this post helpful.
Old 10-27-2010, 03:16 PM   #3
frieza
Senior Member
 
Registered: Feb 2002
Location: harvard, il
Distribution: Ubuntu 11.4,DD-WRT micro plus ssh,lfs-6.6,Fedora 15,Fedora 16
Posts: 3,111

Original Poster
Rep: Reputation: 371Reputation: 371Reputation: 371Reputation: 371
thanks, interesting article btw
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash infinite loop problem? stp001 Linux - Newbie 6 03-15-2010 07:56 AM
Bash infinite loop problems? stp001 Programming 2 03-14-2010 03:41 PM
infinite loop on fclose()? joe2748 Programming 10 03-05-2010 09:09 PM
Infinite Loop ewt3y Programming 3 08-16-2005 10:48 AM
infinite loop beginner_84 Programming 5 08-15-2004 03:32 AM


All times are GMT -5. The time now is 01:49 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration