Live data from Hacker News

A common bug in published code

google.com

41–50 of 79 posts

Re: A common bug in published code

#41
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…

  public class example
  {
      public static void main(String[] JAVA_LOL) throws Throwable
      {
          // prints "6"
          System.out.println((int)6.99999);
      }
  }

Re: A common bug in published code

#42
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…

We're all aware that casting to an integer does truncation, not rounding. Right?

Re: A common bug in published code

#43
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…

At least in Java and other languages, when casting as int truncates, and doesn't round the number, so: ((int) (0.9999999)) == 0 But then there's Math.floor and Math.ceil for what you're describing which can be used as well.

Re: A common bug in published code

#44
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…

import random # will print an integer between 0 and 10 inclusive print randint(0,10)

Re: A common bug in published code

#45
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 can't speak for Python, but in Java it's quite simple to do it right

http://docs.python.org/library/random.html#random.randint

Re: A common bug in published code

#46
post #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'.)

Were you actually confused, or are you just picking nits? If the latter, then it's not really a bug is it.

Re: A common bug in published code

#47

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?

I can help that flame war...

I bet those 5 were written by folks with more (or stronger) skill with C/C++/Java other strongly typed language where casting is required/common.

Casting is uncommon and "weird" in Python. Usually means you're being unpythonic. As in this case you should be using randint or randrange rather than cast to int.

Re: A common bug in published code

#48
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…

In that case you can always use floor or ceil.

Re: A common bug in published code

#49
post #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'.)

Unless we're talking about adding the debate to the queue of debates we're having, that is. Even then, I suppose, it should be enqueue...

Re: A common bug in published code

#50
post #46
post #36

Earlier quoted context omitted.

> queue Speaking of common bugs... (The word you're looking for is 'cue'.)

Were you actually confused, or are you just picking nits? If the latter, then it's not really a bug is it.

Actually, nits are bugs. They just haven't hatched yet.

[1] http://en.wikipedia.org/wiki/Louse

Post reply on HN