SDA SE Wiki

Software Engineering for Smart Data Analytics & Smart Data Analytics for Software Engineering

User Tools

Site Tools


Bug Cor 59

Inspired by

Original Description

“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

Detailed Description

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

  • to remove the rounding operation (by an automated fix) or
  • to alert the developer that the original integer value might have better been generated by using floating point arithmetic (manually).

Sample Problem Scenario

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

Sample Fix

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>

Evaluation Results

Benchmark project Precision Recall
FB JT Delta FB JT Delta
Project … …% …% …% …% …% …%
Project … …% …% …% …% …% …%

FB = FindBugs, JT = JTransformer, Delta = JTransformer - FindBugs

teaching/labs/mdse/2013/bug_descriptions/jt-bug-cor59.txt · Last modified: 2018/05/09 01:59 (external edit)

SEWiki, © 2023