Live data from Hacker News

A common bug in published code

google.com

31–40 of 79 posts

Re: A common bug in published code

#31

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*...

The code is generating code for a different language -- the = is always in a string.

Re: A common bug in published code

#32
One 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.

Re: A common bug in published code

#33

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*...

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

#34
post #20

Apparently, 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…

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 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

#35

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*...

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).

maybe we re seeing different results. mine have 8 incorrect assignments in the first page

Re: A common bug in published code

#36

One 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.

> queue

Speaking of common bugs...

(The word you're looking for is 'cue'.)

Re: A common bug in published code

#37

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*...

That's why I write comparisons like if (CONSTANT == variable)

"yoda conditions":

http://stackoverflow.com/questions/2349378/new-programming-j...

Re: A common bug in published code

#38

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*...

That's why I write comparisons like if (CONSTANT == variable)

Yoda conditions, those are called.

http://united-coders.com/christian-harms/what-are-yoda-condi...

Re: A common bug in published code

#40
post #34
post #20

Earlier 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…

I actually struggle to understand why people even use Math.random() when the Random class exists.
Post reply on HN