大道至简,知易行难
广阔天地,大有作为

Bad attempt to compute absolute value of signed random integer

近日FindBugs发现了一个没见过的BUG:

这段代码具体是:

Bad attempt to compute absolute value of signed random integer

Bad attempt to compute absolute value of signed random integer

原因是:This code generates a random signed integer and then computes the absolute value of that random integer. If the number returned by the random number generator is Integer.MIN_VALUE, then the result will be negative as well (since Math.abs(Integer.MIN_VALUE) == Integer.MIN_VALUE). (Same problem arises for long values as well).

解决方案应该是将:

换为:

因为:

转载时请保留出处,违法转载追究到底:进城务工人员小梅 » Bad attempt to compute absolute value of signed random integer

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址