Java &引用;“不必要的浮动铸造”;似乎有必要。我错过了什么?
Netbeans*7.4*告诉我这一行的“不必要的浮点转换”:Java &引用;“不必要的浮动铸造”;似乎有必要。我错过了什么?,java,netbeans,casting,Java,Netbeans,Casting,Netbeans*7.4*告诉我这一行的“不必要的浮点转换”: int points; // EDIT ********************* String word; appendOutput(word + (points > 0 ? "\t" + round((float)points/word.length(),2) : "")); 但是如果我删除(floa
int points; // EDIT *********************
String word;
appendOutput(word +
(points > 0
? "\t" + round((float)points/word.length(),2)
: ""));
但是如果我删除(float)
,输出将被截断(在这种情况下应该是这样的)到小于点/word.length
的最大整数
那么Netbeans怎么了?如果我想要正确的输出到百分之一百,我必须忽略它删除(float)
的建议
还是我做错了什么/不认真,或者只是错过了什么
*编辑*
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);
}
嘿。。。忘了我写了自己的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);
}
}
**编辑**刚刚将
(浮动)
添加到MCVE。。。啊…假设点
为int
,那么:
points / word.length()
是一个int
除法,因此结果将是int
。如果结果有小数部分,则将其向下舍入,以适合int
如果希望/需要一个float
或double
结果,则需要为其中一个操作数将类型转换为float
或double
从代码的外观来看,没有bug,也不会发出警告。这似乎是Netbeans中的一个bug。通过使用round
方法的正确签名,可以通过使用javac或其他IDE(如Eclipse或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)
: ""));
}
}
通过
round
方法的实现,我编写了这个示例来在Eclipse和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)
: ""));
}
}
这些IDE都没有发出警告,所以看起来像是Netbeans中的一个bug。根据我们所知,我不认为您遗漏了什么。它看起来像是Netbeans中的一个bug 由于
(float)
绑定到点
,而不绑定到除法的结果,因此强制转换改变了程序的语义,不能合理地描述为“不必要”
a将有助于确认这一点(并且在提交错误报告时会很有用)。
points
是int类型吗?您如何声明word
和points
?如果您发布了一个复制此警告和您的Netbeans版本的文件,那将非常好。这听起来像是Netbeans中的一个错误。我认为一个简单的例子和Netbeans版本是可行的(正如@Radiodef所建议的)。请在您的问题中包含round
方法的签名。如果该方法的第一个参数类型为float
,则该参数将自动升级为float,这将解释Netbeans警告。虽然所有这些都是真的,但它并没有解决问题。我意识到这一切;为什么Netbeans没有抓住我的重点?我应该如何让Netbeans开心并获得百分之一百的输出?@DSlomer64提供了这个轮方法的签名,我可以告诉你这是否是Netbeans的bug。我试图在Eclipse和Intellij中重现这一点,但没有这样愚蠢的警告。我已经多次编辑了原始帖子,以填补其中的空白——例如,变量类型、round
方法的完整性等等,以及一个给出警告的MCVE。我只是在MCVE中编辑,但我不能说我知道如何提交错误报告,如果你是说我应该。我会用谷歌搜索,看看怎么做,但如果你愿意,可以自己做。@NPE--我试图提交一份错误报告,但看起来这可能是在8.0中修复的,所以我无法证明继续下去是正确的,因为我真的不知道我在做什么。我猜分析器误解了代码,认为(float)
绑定到整个表达式。