The Daily WTF: Curious Perversions in Information Technology
Welcome to TDWTF Forums Sign in | Join | Help
in Search

The ending never-ending loop

Last post 11-21-2008 3:45 PM by Fred Foobar. 29 replies.
Page 1 of 1 (30 items)
Sort Posts: Previous Next
  • 11-19-2008 5:50 PM

    • dtech
    • Top 50 Contributor
    • 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?

    NerdTests.com says I'm a Cool Nerd King.  Click here to take the Nerd Test, get nerdy images and jokes, and write on the nerd forum!
  • 11-19-2008 6:56 PM In reply to

    • pjt33
    • Not Ranked
    • 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.

  • 11-19-2008 7:22 PM In reply to

    Re: The ending never-ending loop

    This has been Photoshopped. 

  • 11-20-2008 2:19 AM In reply to

    • dtech
    • Top 50 Contributor
    • Joined on 11-13-2007
    • Utrecht, Netherlands
    • Posts 623

    Re: The ending never-ending loop

     

    NerdTests.com says I'm a Cool Nerd King.  Click here to take the Nerd Test, get nerdy images and jokes, and write on the nerd forum!
  • 11-20-2008 3:43 AM In reply to

    Re: The ending never-ending loop

    TRWTF is using floats for a loop counter in the first place.
  • 11-20-2008 7:10 AM In reply to

    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!
  • 11-20-2008 7:58 AM In reply to

    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?

  • 11-20-2008 8:12 AM In reply to

    • PJH
    • Top 10 Contributor
    • Joined on 02-14-2007
    • Newcastle, UK
    • Posts 1,256

    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
  • 11-20-2008 8:17 AM In reply to

    Re: The ending never-ending loop

     oh, yeah, hence ending never-ending loop

  • 11-20-2008 8:36 AM In reply to

    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.
  • 11-20-2008 8:40 AM In reply to

    • PJH
    • Top 10 Contributor
    • Joined on 02-14-2007
    • Newcastle, UK
    • Posts 1,256

    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
  • 11-20-2008 12:27 PM In reply to

    • Daid
    • Top 75 Contributor
    • 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.
  • 11-20-2008 12:44 PM In reply to

    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.
  • 11-20-2008 1:27 PM In reply to

    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
  • 11-20-2008 1:40 PM In reply to

    • Zecc
    • Top 50 Contributor
    • Joined on 06-12-2007
    • Location, Location, Location
    • Posts 657

    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'
  • 11-20-2008 1:45 PM In reply to

    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 />
  • 11-20-2008 1:48 PM In reply to

    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.
  • 11-20-2008 1:57 PM In reply to

    • PJH
    • Top 10 Contributor
    • Joined on 02-14-2007
    • Newcastle, UK
    • Posts 1,256

    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
  • 11-20-2008 2:26 PM In reply to

    • Zecc
    • Top 50 Contributor
    • Joined on 06-12-2007
    • Location, Location, Location
    • Posts 657

    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'
  • 11-20-2008 3:44 PM In reply to

    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)

  • 11-20-2008 8:03 PM In reply to

    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.

     

  • 11-20-2008 11:54 PM In reply to

    • tster
    • Top 10 Contributor
    • 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.
  • 11-21-2008 2:26 AM In reply to

    • dtech
    • Top 50 Contributor
    • Joined on 11-13-2007
    • Utrecht, Netherlands
    • Posts 623

    Re: The ending never-ending loop

     you sir, are the true master of BS.

    NerdTests.com says I'm a Cool Nerd King.  Click here to take the Nerd Test, get nerdy images and jokes, and write on the nerd forum!
  • 11-21-2008 8:20 AM In reply to

    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!

  • 11-21-2008 8:31 AM In reply to

    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 />
  • 11-21-2008 8:36 AM In reply to

    • Nelle
    • Top 100 Contributor
    • 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 ... 

  • 11-21-2008 8:51 AM In reply to

    • DOA
    • Top 25 Contributor
    • 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.

  • 11-21-2008 3:21 PM In reply to

    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 Algorithm
    while (numDrinks < 3) {
    haveADrink();
    }
    gotoWork();
    while (numDrinks < 6) {
    haveADrink();
    }
    wasteMyTimeAndOthersOnTDWTF();
  • 11-21-2008 3:38 PM In reply to

    • PJH
    • Top 10 Contributor
    • Joined on 02-14-2007
    • Newcastle, UK
    • Posts 1,256

    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
  • 11-21-2008 3:45 PM In reply to

    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 Algorithm
    while (numDrinks < 3) {
    haveADrink();
    }
    gotoWork();
    while (numDrinks < 6) {
    haveADrink();
    }
    wasteMyTimeAndOthersOnTDWTF();
Page 1 of 1 (30 items)
Powered by Community Server (Non-Commercial Edition), by Telligent Systems