Why is this an issue?
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.
Casting a non-floating-point value to floating-point and then passing it to Math.round
, Math.ceil
, or
Math.floor
is silly because the result will always be the original value.
These operations are silly with any constant value: Math.abs
, Math.ceil
, Math.floor
, Math.rint
,
Math.round
.
And these oprations are silly with certain constant values:
Operation |
Value |
acos |
0.0 or 1.0 |
asin |
0.0 or 1.0 |
atan |
0.0 or 1.0 |
atan2 |
0.0 |
cbrt |
0.0 or 1.0 |
cos |
0.0 |
cosh |
0.0 |
exp |
0.0 or 1.0 |
expm1 |
0.0 |
log |
0.0 or 1.0 |
log10 |
0.0 or 1.0 |
sin |
0.0 |
sinh |
0.0 |
sqrt |
0.0 or 1.0 |
tan |
0.0 |
tanh |
0.0 |
toDegrees |
0.0 or 1.0 |
toRadians |
0.0 |
Noncompliant code example
public void doMath(int a) {
double floor = Math.floor((double)a); // Noncompliant
double ceiling = Math.ceil(4.2); // Noncompliant
double arcTan = Math.atan(0.0); // Noncompliant
}