"Unnecessary float casting" appears to be necessary. What am I missing?

Netbeans * 7.4 * tells me "Unnecessary Casting for floats" for this line:

int points; // EDIT *********************
String word; 

appendOutput(word + 
                  (points > 0 
                  ? "\t" + round((float)points/word.length(),2)  
                  : "")); 

      

But if I delete (float)

, the output is truncated (as it should in the circumstances) to the largest integer less points/word.length

.

So what about Netbeans? If I want to get the correct result down to the hundredths, I must ignore his delete suggestion (float)

.

Or am I doing something wrong / obscene or just missing something?

* EDIT *

Heh ... forgot I wrote my own method round

:

  public static double round(float x, int n){
    return Math.floor((0 + Math.pow(10, n)*x + 0.5)) / Math.pow(10, n);  
  }

      

* MCVE *

public class JavaApplication66 {

  public static void main(String[] args) {

    int points = 23; // EDIT *********************
    String word = "FOO"; 

    System.out.println(word + 
                (points > 0 
                ? "\t" + round((float)points/word.length(),2)  
                : ""));
}

public static double round(float x, int n){
  return Math.floor((0 + Math.pow(10, n)*x + 0.5)) / Math.pow(10, n);  
}

      

}

** edit ** just added (float)

to MCVE ... augh ...

+3


source to share


2 answers


Based on what we know, I don't think you missed anything. This looks like a bug in Netbeans.

Since it (float)

binds to points

and not to the result of the division, casting changes the semantics of the program and cannot reasonably be described as "unnecessary".



MCVE will help confirm this (and will be helpful when filing a bug report).

+1


source


Assuming there points

is int

, then this:

points / word.length()

      

Is a section int

, so the result will be int

. If the result has a decimal part, rounding will be rounded to int

.

Casting to float

or double

for one of the operands is necessary if you want / want a result float

or double

.

From what the code looks like, there is no error and no warning should be raised. This looks like a bug in Netbeans. With the correct method signature, round

this can be verified by testing similar code with javac or using another IDE such as Eclipse or Intellij.




With method injection, round

I wrote this example to reproduce the problem in Eclipse and Intellij.

public class Test {
    public static double round(float x, int n){
        return Math.floor((0 + Math.pow(10, n)*x + 0.5)) / Math.pow(10, n);  
    }
    public static void main(String[] args) {
        int points = 0;
        String word = "foo";
        StringBuilder appendOutput = new StringBuilder();
        appendOutput.append(word + 
            (points > 0 
                ? "\t" + round((float)points/word.length(),2)  
                : ""));
    }
}

      

None of these IDEs raised the warning, so it looks like a bug in Netbeans.

+2


source







All Articles