Obvious and stupid :-)
-
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... }
-
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... }
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.
-
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.
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 :-)
-
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... }
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. -
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.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. ;)
-
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. ;)
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 ;)