Software Engineering for Smart Data Analytics & Smart Data Analytics for Software Engineering
“This code converts an int value to a float precision floating point number and then passes the result to the Math.round() function, which returns the int/long closest to the argument. This operation should always be a no-op, since the converting an integer to a float should give a number with no fractional part. It is likely that the operation that generated the value to be passed to Math.round was intended to be performed using floating point arithmetic.” – 2013-10-14
the cast from an int to a float yields a float with no fractional part. Thus passing the resulting float to the Math.round() function makes no sense – rounding such a value always yields the same number.
So the obvious treatments of this bug are
<code Java> public void wrong1() {
int i = 1; Math.round((float) i); }
</Code>
<code Java> public void wrong2(int i) {
Math.round((float) i); }
</Code>
<code Java> public class IntValueMathRound{
private int x = 0; public void wrong3() { int i = 10; x = Math.round((float) i); }
} </Code>
<code Java> public void undetected1() {
double i = 1; Math.round((float) i); }
</Code>
<code Java> public void undetected2() {
double i = 10.02; Math.round((double) i); }
</Code>
Replace invocation of Math.round(X) by direct access to X:
<code Java> public void wrong1() {
int i = 1; }
</Code>
<code Java> public class IntValueMathRound{
private int x = 0; public void wrong3() { int i = 10; x = i; }
} </Code>
Benchmark project | Precision | Recall | ||||
---|---|---|---|---|---|---|
FB | JT | Delta | FB | JT | Delta | |
Project … | …% | …% | …% | …% | …% | …% |
Project … | …% | …% | …% | …% | …% | …% |
FB = FindBugs, JT = JTransformer, Delta = JTransformer - FindBugs