Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. Clever Code
  4. Obvious and stupid :-)

Obvious and stupid :-)

Scheduled Pinned Locked Moved Clever Code
learning
6 Posts 4 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Suzetta
    wrote on last edited by
    #1

    Some time back, I was perplexed as to why my program went into an endless loop. This was in VB, but of course is language independant when you look at it. Obvious, dumb, but still not something many programmers (even experienced ones) think of for some reason: for (Single i = 1000001; i < 20000000; i++) { // do stuff... }

    W M 2 Replies Last reply
    0
    • S Suzetta

      Some time back, I was perplexed as to why my program went into an endless loop. This was in VB, but of course is language independant when you look at it. Obvious, dumb, but still not something many programmers (even experienced ones) think of for some reason: for (Single i = 1000001; i < 20000000; i++) { // do stuff... }

      W Offline
      W Offline
      wtwhite
      wrote on last edited by
      #2

      Is it that the Single variable runs out of precision and can't increment all the way up to 20 million? I.e. there is some value i < 20000000 such that i + 1 == i? If so I would say that isn't too obvious at all! I have run into a similar problem when using a test for equality on floating point numbers instead of using less-than as you have here.

      S 1 Reply Last reply
      0
      • W wtwhite

        Is it that the Single variable runs out of precision and can't increment all the way up to 20 million? I.e. there is some value i < 20000000 such that i + 1 == i? If so I would say that isn't too obvious at all! I have run into a similar problem when using a test for equality on floating point numbers instead of using less-than as you have here.

        S Offline
        S Offline
        Suzetta
        wrote on last edited by
        #3

        It is indeed - but I think it should be obvious if you know your basic number representations. Testing for equality is similar, in that you run out of precision, but I guess my version is less intuitive as you 'expect' to be able to add one and get a result that is bigger than the original value :-)

        1 Reply Last reply
        0
        • S Suzetta

          Some time back, I was perplexed as to why my program went into an endless loop. This was in VB, but of course is language independant when you look at it. Obvious, dumb, but still not something many programmers (even experienced ones) think of for some reason: for (Single i = 1000001; i < 20000000; i++) { // do stuff... }

          M Offline
          M Offline
          Mindflow
          wrote on last edited by
          #4

          heheh, not obvious, but yes, call anyone "stupid" if they don't learn what variables can and can't hold. Many people out there start reading a book and program immediately, and not read cover to cover, skipping the "What the hell is a Single?" chapter ;) I like to use limits in my code... // PHP $looplimit = 55; while($i<20) {   // some code   // ...   // In the past, I've made the simple mistake of using $i again in here, ruining the loop.   $i++;   if(--$looplimit < 0) {     die("Error: loop limit reached in {description of code location}.");   } // end if } // end while This is very helpful while developing as sometimes loops will run longer than you think they should but you don't realize it. An example: I have an application that loops dates (days), and realized that Javascript wasn't running properly on my PC or there was a problem with my code. The error wasn't too big so if I didn't set a limit, it would have gone un noticed for a while. Also it helps not crash your PC, or even worse, a Development Server.

          E 1 Reply Last reply
          0
          • M Mindflow

            heheh, not obvious, but yes, call anyone "stupid" if they don't learn what variables can and can't hold. Many people out there start reading a book and program immediately, and not read cover to cover, skipping the "What the hell is a Single?" chapter ;) I like to use limits in my code... // PHP $looplimit = 55; while($i<20) {   // some code   // ...   // In the past, I've made the simple mistake of using $i again in here, ruining the loop.   $i++;   if(--$looplimit < 0) {     die("Error: loop limit reached in {description of code location}.");   } // end if } // end while This is very helpful while developing as sometimes loops will run longer than you think they should but you don't realize it. An example: I have an application that loops dates (days), and realized that Javascript wasn't running properly on my PC or there was a problem with my code. The error wasn't too big so if I didn't set a limit, it would have gone un noticed for a while. Also it helps not crash your PC, or even worse, a Development Server.

            E Offline
            E Offline
            Egon_Freeman
            wrote on last edited by
            #5

            Setting a limit might be hard to do in PHP, since You really don't define the variable TYPE You'd expect. What with setting a limit at 2000 when a value could legitimately reach 3000? It can be done in C/C++ or any language with strict type definitons, however. That way You'd know that You're expecting ie. an INT, so it'd be a good idea to set the loopCounter somewhere around 65535 or seomthing... :) [in general, somewhere around the margin value the expected variable type can hold, be it positive or negative] -------------------- Just ignore me. ;)

            M 1 Reply Last reply
            0
            • E Egon_Freeman

              Setting a limit might be hard to do in PHP, since You really don't define the variable TYPE You'd expect. What with setting a limit at 2000 when a value could legitimately reach 3000? It can be done in C/C++ or any language with strict type definitons, however. That way You'd know that You're expecting ie. an INT, so it'd be a good idea to set the loopCounter somewhere around 65535 or seomthing... :) [in general, somewhere around the margin value the expected variable type can hold, be it positive or negative] -------------------- Just ignore me. ;)

              M Offline
              M Offline
              Mindflow
              wrote on last edited by
              #6

              yeah I get what you mean, but what I do is limit the amount of loops. Example: if I know a loop should never reach more than One Year of days, then I limit it to ~400 If the code is ever updated, a test run will show the error message anyways so that the Developer can update that part too. It's all good ;)

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups