SonarQube 提出重大违规 Silly math should not be performed在我的代码中。描述说
Certain math operations are just silly and should not be performed because their results are predictable.
In particular,
anyValue
% 1 is silly because it will always return 0.
但在我的例子中,anyValue
是一个 double 值。这有效 as intended为了我。这是实际的代码:
double v = Double.parseDouble(Utils.formatDouble(Double.valueOf(value.getValue()), accuracy.intValue()));
boolean negative = v < 0;
v = Math.abs(v);
long deg = (long) Math.floor(v);
v = (v % 1) * 60;
分析器是否假设我的变量是 int
(这是他们的错误)?还是我遗漏了什么?
最佳答案
这确实是一个错误,非常感谢您的报告。
其中绝对没有检查 % 运算符的左操作数的类型。
我刚刚提交了以下错误来处理这个问题:https://jira.sonarsource.com/browse/SONARJAVA-1457
关于java - 为什么 anyValue 是 double 值时 Sonar 中的 anyValue % 1 "silly math"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34639319/