|
The ending never-ending loop
Last post 11-21-2008 3:45 PM by Fred Foobar. 29 replies.
-
11-19-2008 5:50 PM
|
|
-
dtech


- Joined on 11-13-2007
- Utrecht, Netherlands
- Posts 623
|
The ending never-ending loop
Probably you've seen this in more than one variation, but I liked this one.
double a = 1; for(double b=0;b < a; b++) { a++; }
What o what goes wrong here?
|
|
-
-
pjt33


- Joined on 12-06-2005
- Posts 35
|
Re: The ending never-ending loop
It's not quite a never-ending loop. Once a gets above about 2^53 the increment won't change it, so b will catch up.
|
|
-
-
morbiuswilters


- Joined on 01-15-2008
- East Coast Represent!
- Posts 4,987
|
Re: The ending never-ending loop
This has been Photoshopped.
|
|
-
-
-
SenTree


- Joined on 04-03-2008
- Posts 116
|
Re: The ending never-ending loop
TRWTF is using floats for a loop counter in the first place.
|
|
-
-
bjolling


- Joined on 06-08-2008
- Belgium
- Posts 431
|
Re: The ending never-ending loop
morbiuswilters:This has been Photoshopped.
Now that you mention it.... the blue color on the sleeve of your avatar doesn't seem to match with the color of the vest...
 It's... Monkey Piston's Frying Circle!
|
|
-
-
NeoMojo


- Joined on 01-22-2008
- Posts 21
|
Re: The ending never-ending loop
Shriley the loop will never be used since b starts off as < a. Or am I missing some in joke?
|
|
-
-
PJH


- Joined on 02-14-2007
- Newcastle, UK
- Posts 1,253
|
Re: The ending never-ending loop
NeoMojo:Shriley the loop will never be used since b starts off as < a.
Programming 101 - the loop continues while the condition is true, not false. If it helps, here it is as a while loop: double a = 1; double b = 0; while(b < a){ a++; b++;
}
Abstinence makes the Church grow fondlers.
- unknown
|
|
-
-
NeoMojo


- Joined on 01-22-2008
- Posts 21
|
Re: The ending never-ending loop
oh, yeah, hence ending never-ending loop
|
|
-
-
bstorer


- Joined on 02-01-2007
- Alexandria, VA
- Posts 3,402
|
Re: The ending never-ending loop
NeoMojo:Shriley the loop will never be used since b starts off as < a. Or am I missing some in joke?
You're missing something far, far worse: an understanding of basic for loops.
|
|
-
-
PJH


- Joined on 02-14-2007
- Newcastle, UK
- Posts 1,253
|
Re: The ending never-ending loop
NeoMojo:oh, yeah, hence ending never-ending loop
In theory. As pointed out previously, in reality, since they're floats, there'll come a time when the limitatation of the floating point format means that the number is large enough that adding one won't actually change the value, so a will equal b. (Alternatively, with integers, a will roll round just before b, making b > a.)
Abstinence makes the Church grow fondlers.
- unknown
|
|
-
-
Daid


- Joined on 01-30-2007
- Posts 348
|
Re: The ending never-ending loop
And as floating point calculations differ from machine to machine it fill the requirements.
It delays execution depending on the running machine.
|
|
-
-
j6cubic


- Joined on 06-07-2007
- Posts 88
|
Re: The ending never-ending loop
bstorer: NeoMojo:Shriley the loop will never be used since b starts off as < a. Or am I missing some in joke?
You're missing something far, far worse: an understanding of basic for loops.
Awesome. After Visual Basic for Applications Micosoft has finally released Visual Basic for Loops. The perfect tool to let highly-paid consultants play without giving them a chance to do any real damage. All-new ability to store your loops in an Access database! Order today.
|
|
-
-
A Wizard A True Star


- Joined on 04-04-2005
- Posts 84
|
Re: The ending never-ending loop
NeoMojo:Shriley the loop will never be used since b starts off as < a. Or am I missing some in joke?
TRWTF is that you don't know how to properly misspell "surely".
just another onionhead
|
|
-
-
Zecc


- Joined on 06-12-2007
- Location, Location, Location
- Posts 654
|
Re: The ending never-ending loop
A Wizard A True Star:TRWTF is that you don't know how to properly misspell "surely".
Are you trolling or are you not getting the reference?
If mixed metaphors were illegal, I'd be having an indigestion. typeof NaN == 'number'
|
|
-
-
amischiefr


- Joined on 06-11-2008
- North Florida
- Posts 515
|
Re: The ending never-ending loop
Please provide a non You-Tube link so that I may view all of the Shriley goodness at work. Thank you, The Management
<somethingwitty />
|
|
-
-
bstorer


- Joined on 02-01-2007
- Alexandria, VA
- Posts 3,402
|
Re: The ending never-ending loop
A Wizard A True Star: NeoMojo:Shriley the loop will never be used since b starts off as < a. Or am I missing some in joke?
TRWTF is that you don't know how to properly misspell "surely".
Maybe he meant "shrilly," as in "the loop will never be used in a shrill manner." Of course, I have absolutely no idea what that means.
|
|
-
-
PJH


- Joined on 02-14-2007
- Newcastle, UK
- Posts 1,253
|
Re: The ending never-ending loop
Zecc: A Wizard A True Star:TRWTF is that you don't know how to properly misspell "surely".
Are you trolling or are you not getting the reference?
Did you not notice the OP incorrectly misspelt surely, Shriley, instead of Shirley?
Abstinence makes the Church grow fondlers.
- unknown
|
|
-
-
Zecc


- Joined on 06-12-2007
- Location, Location, Location
- Posts 654
|
Re: The ending never-ending loop
PJH: Zecc: A Wizard A True Star:TRWTF is that you don't know how to properly misspell "surely".
Are you trolling or are you not getting the reference?
Did you not notice the OP incorrectly misspelt surely, Shriley, instead of Shirley?
I completely misread and subsequently misunderstood A.W.A.T.S.' comment. I apologize and retreat in shame.
If mixed metaphors were illegal, I'd be having an indigestion. typeof NaN == 'number'
|
|
-
-
TwelveBaud


- Joined on 06-03-2008
- UVA/Wise, Wise, VA
- Posts 258
|
Re: The ending never-ending loop
amischiefr: Please provide a non You-Tube link so that I may view all of the Shriley goodness at work. Thank you, The Management Ted Striker:
Surely you can't be serious.
Rumack:
I am serious... and don't call me Shirley. --Airplane! (1980)
|
|
-
-
TopCod3rsBottom


- Joined on 10-01-2008
- Posts 1
|
Re: The ending never-ending loop
PJH: NeoMojo:oh, yeah, hence ending never-ending loop
In theory. As pointed out previously, in reality, since they're floats, there'll come a time when the limitatation of the floating point format means that the number is large enough that adding one won't actually change the value, so a will equal b
Hence, *ending* never-eding loops. A "never-ending" loop which ends. A finite "infinite loop". Or just a really slow busy loop.
|
|
-
-
tster


- Joined on 04-11-2006
- Natick, MA
- Posts 1,765
|
Re: The ending never-ending loop
If the loop survives this iteration, you will still be parted. If a is
defeated, and b made king and the conditions that you checked for become false,
it will still have to taste the bitterness of mortality. Whether by
the incrementing or the slow decay of silicon, the loop will terminate. And there will
be no comfort for you. No comfort to ease the pain of its passing. It
will come to completion, an image of the splendor of loops of Java in
glory undimmed before the surpassing of Double.MAX_VALUE. But you, my intel x86 processor,
you will linger on in solitude, as a GUI in .NET
that comes without a DataGridView.ColumnHeaderMouseClick event. Here you will dwell, bound to your objectDataSource1,
under the fading menus, until all the Form is changed and the long cycles of your life are utterly spent. Pentium, there is nothing for you
here... only termination of loops.
The pig go. Go is to the fountain. The pig put foot. Grunt. Foot in what? ketchup. The dove fly. Fly is in sky. The dove drop something. The something on the pig. The pig disgusting... see bio for the earth shattering ending.
|
|
-
-
dtech


- Joined on 11-13-2007
- Utrecht, Netherlands
- Posts 623
|
Re: The ending never-ending loop
you sir, are the true master of BS.
|
|
-
-
halcyon1234


- Joined on 09-10-2008
- Posts 93
|
Re: The ending never-ending loop
PJH: NeoMojo:oh, yeah, hence ending never-ending loop
In theory. As pointed out previously, in reality, since they're floats, there'll come a time when the limitatation of the floating point format means that the number is large enough that adding one won't actually change the value, so a will equal b. (Alternatively, with integers, a will roll round just before b, making b > a.)
Interesting note: I coded this up and set it up to run overnight on my work machine. 16 hours later, and: 1) The Console version is still going, currently at 3770983985 < 3770983986 2) The logged version (really just CrazyLoop.exe > file1.txt) crashed when file1 took up too much of the hard disk (18,609,824,752 bytes). Fun!
|
|
-
-
amischiefr


- Joined on 06-11-2008
- North Florida
- Posts 515
|
Re: The ending never-ending loop
TwelveBaud:Ted Striker:
Surely you can't be serious.
Rumack:
I am serious... and don't call me Shirley. --Airplane! (1980)
Ahh yes, a classic.
<somethingwitty />
|
|
-
-
Nelle


- Joined on 11-08-2007
- graz.at.earth.milkyway.universe
- Posts 285
|
Re: The ending never-ending loop
halcyon1234:Interesting note: I coded this up and set it up to run overnight on my work machine. 16 hours later, and: 1) The Console version is still going, currently at 3770983985 < 3770983986 Its clearly a matter of processing power... We should write an official tdwtf screensaver which processes the given loop in a distributed environment (simply copying SETI screensaver will do) ... If we could use the GPUs as well, it would even double the performance ...
|
|
-
-
DOA


- Joined on 06-26-2007
- Posts 703
|
Re: The ending never-ending loop
Nelle: halcyon1234:Interesting note: I coded this up and set it up to run overnight on my work machine. 16 hours later, and: 1) The Console version is still going, currently at 3770983985 < 3770983986
Its clearly a matter of processing power... We should write an official tdwtf screensaver which processes the given loop in a distributed environment (simply copying SETI screensaver will do) ... If we could use the GPUs as well, it would even double the performance ...
Sweet, TDWTF@Home. Plz send me teh codez.
|
|
-
-
Fred Foobar


- Joined on 01-29-2006
- Posts 92
|
Re: The ending never-ending loop
halcyon1234: PJH: NeoMojo:oh, yeah, hence ending never-ending loop
In theory. As pointed out previously, in reality, since they're floats, there'll come a time when the limitatation of the floating point format means that the number is large enough that adding one won't actually change the value, so a will equal b. (Alternatively, with integers, a will roll round just before b, making b > a.)
Interesting note: I coded this up and set it up to run overnight on my work machine. 16 hours later, and: 1) The Console version is still going, currently at 3770983985 < 3770983986 2) The logged version (really just CrazyLoop.exe > file1.txt) crashed when file1 took up too much of the hard disk (18,609,824,752 bytes). Fun!
You're spending too much time outputting numbers. The cost of converting the double to a decimal representation in ASCII and outputting it to stdout is running way past the cost of a simple floating-point add. Try this, see if it works faster: #include <stdio.h> int main(int argc, char *argv[) { double a = 1, b = 0; for (; b < a; b++) a++; printf("%f", a); } *Note: There is a 30% chance that I am entirely, woefully wrong here. Should this be the case, feel free to mock me mercilessly.
The TDWTF Drinking Algorithmwhile (numDrinks < 3) { haveADrink(); } gotoWork(); while (numDrinks < 6) { haveADrink(); } wasteMyTimeAndOthersOnTDWTF();
|
|
-
-
PJH


- Joined on 02-14-2007
- Newcastle, UK
- Posts 1,253
|
Re: The ending never-ending loop
TopCod3rsBottom:
PJH:
NeoMojo:oh, yeah, hence ending never-ending loop
In theory.
As pointed out previously, in reality, since they're floats, there'll come a time when the limitatation of the floating point format means that the number is large enough that adding one won't actually change the value, so a will equal b
Hence, *ending* never-eding loops. A "never-ending" loop which ends. A finite "infinite loop". Or just a really slow busy loop.
Well since the post that prompted that/my reply had a distinct lack of understanding of what a for loop in most languages does, I thought it prudent to clarify any other potential misunderstandings about the subject/1st OP to the post I was replying to.
Feel free to boost your ego by pointing out another error in this post. Otherwise STFU.
Abstinence makes the Church grow fondlers.
- unknown
|
|
-
-
Fred Foobar


- Joined on 01-29-2006
- Posts 92
|
Re: The ending never-ending loop
Gaah, edit timeout. Here's what I meant to post:
#include <stdio.h>
int main(int argc, char **argv) {
double a = 1, b = 0;
for (; b < a; b++) {
a++;
}
printf("%f", a);
return 0;
}
Running something similar (with a signal handler added to print the numbers) on my laptop reached the number reached by halcyon in about 3 minutes.
The TDWTF Drinking Algorithmwhile (numDrinks < 3) { haveADrink(); } gotoWork(); while (numDrinks < 6) { haveADrink(); } wasteMyTimeAndOthersOnTDWTF();
|
|
Page 1 of 1 (30 items)
|
|
|