A-mazing Also, notice the difference in number of results between C and C++ below: http://www.google.com/codesearch?hl=en&lr=&q=if%5Cs*... vs http://www.google.com/codesearch?hl=en&lr=&q=if%5Cs*...
A common bug in published code
31–40 of 79 posts
Re: A common bug in published code
#32[queue debate about tools that hold your hands vs understanding what you are actually writing]
I wonder if they did the search for if (x = y) bug pre-gcc-4.x-warn what sort of numbers they would get.
Re: A common bug in published code
#33A-mazing Also, notice the difference in number of results between C and C++ below: http://www.google.com/codesearch?hl=en&lr=&q=if%5Cs*... vs http://www.google.com/codesearch?hl=en&lr=&q=if%5Cs*...
Re: A common bug in published code
#34Apparently, python only has 5 instances of the corresponding error: http://www.google.com/codesearch?hl=en&lr=&q=\s%2Bin... Python-Java flame-war, anyone?
Slight difference here. There are actually two types of errors in the Java code: 1. int foo = (int) Math.random() * some_max_value; The error here is assuming that the multiplication takes place before the truncation. This isn't happening in the Python code because int(expression to truncate) is unambiguous. (+1 to Python here for making it hard to shoot yourself in the foot). 2. int foo = (int) Math.random(); The er…
I can't speak for Python, but in Java it's quite simple to do it right; Random#nextInt(int) "Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive)" (also consider using SecureRandom).
Re: A common bug in published code
#35A-mazing Also, notice the difference in number of results between C and C++ below: http://www.google.com/codesearch?hl=en&lr=&q=if%5Cs*... vs http://www.google.com/codesearch?hl=en&lr=&q=if%5Cs*...
Maybe I am feeling dense, but I do not understand what you are trying to point out. None except one are errors as they relate to embedded sql statements (where Equal To is the comparison operator).
Re: A common bug in published code
#36One would hope at some point a tool would warn you about this since clearly it could optimize (int) Math.random() to just 0. [queue debate about tools that hold your hands vs understanding what you are actually writing] I wonder if they did the search for if (x = y) bug pre-gcc-4.x-warn what sort of numbers they would get.
Speaking of common bugs...
(The word you're looking for is 'cue'.)
Re: A common bug in published code
#37A-mazing Also, notice the difference in number of results between C and C++ below: http://www.google.com/codesearch?hl=en&lr=&q=if%5Cs*... vs http://www.google.com/codesearch?hl=en&lr=&q=if%5Cs*...
That's why I write comparisons like if (CONSTANT == variable)
http://stackoverflow.com/questions/2349378/new-programming-j...
Re: A common bug in published code
#38A-mazing Also, notice the difference in number of results between C and C++ below: http://www.google.com/codesearch?hl=en&lr=&q=if%5Cs*... vs http://www.google.com/codesearch?hl=en&lr=&q=if%5Cs*...
That's why I write comparisons like if (CONSTANT == variable)
http://united-coders.com/christian-harms/what-are-yoda-condi...
Re: A common bug in published code
#39im floored
Re: A common bug in published code
#40Earlier quoted context omitted.
Slight difference here. There are actually two types of errors in the Java code: 1. int foo = (int) Math.random() * some_max_value; The error here is assuming that the multiplication takes place before the truncation. This isn't happening in the Python code because int(expression to truncate) is unambiguous. (+1 to Python here for making it hard to shoot yourself in the foot). 2. int foo = (int) Math.random(); The er…
Third error: This is not the correct way to randomly pick a number in a set range. The proper way is actually quite complicated. Imagine you do (int) (Math.random() * 10), this could give you numbers from 0 to 10. However, you only get 0 if Math.random() * 10 is less than 0.5, but you get 1 if the value is between 0.5 and 1.5. You are half as likely to see a zero! I can't speak for Python, but in Java it's quite simp…