LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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, 01: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,233

Rep: Reputation: 406Reputation: 406Reputation: 406Reputation: 406Reputation: 406
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, 02:11 PM   #2
paulsm4
LQ 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 02:15 PM.
 
1 members found this post helpful.
Old 10-27-2010, 02: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,233

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


Reply



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 06:56 AM
Bash infinite loop problems? stp001 Programming 2 03-14-2010 02:41 PM
infinite loop on fclose()? joe2748 Programming 10 03-05-2010 08:09 PM
Infinite Loop ewt3y Programming 3 08-16-2005 09:48 AM
infinite loop beginner_84 Programming 5 08-15-2004 02:32 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:26 AM.

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
Open Source Consulting | Domain Registration