# SDA SE Wiki

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

### Sidebar

• MDSE 2013 • Nice guys  • Methodology • Bug descriptions • Points-to Analysis • Final presentation # Bug Cor 59

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

<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 