Redundant Math.ceil/floor/round on Integer Argument
ID |
java.math_ceil_with_int_cast |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Reliability |
Language |
Java |
Tags |
reliability, suspicious-comparison |
Description
Reports calls to Math.ceil(), Math.floor(), or Math.round() where the argument is effectively an integer value (possibly cast to double or float).
Rationale
Passing an integer argument to Math.ceil(), Math.floor(), or Math.round() is redundant because an integer value has no fractional part. The rounding operation has no effect and simply wastes CPU cycles. This often indicates a logic error where the developer intended to pass a floating-point expression (e.g., a division of two doubles) but accidentally used integer arithmetic.
// Bad - ceil on integer cast to double is a no-op
double d = Math.ceil((double) intVar);
// Bad - floor on integer is redundant
double d = Math.floor(count);
Remediation
Remove the redundant rounding call and use a simple cast, or ensure the argument is a genuine floating-point expression.
// Good - simple cast
double d = (double) intVar;
// Good - actual floating-point argument
double d = Math.ceil(total / (double) count);