Avoid Comparisons Against Double.MAX_VALUE / Double.MIN_VALUE

ID

java.double_compare_against_max_min

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

Java

Tags

reliability, suspicious-comparison

Description

Reports relational comparisons (<, >, <=, >=) where one operand is Double.MAX_VALUE or Double.MIN_VALUE. Such comparisons are always true or always false for any finite double value, which indicates a logic error.

Rationale

Double.MAX_VALUE is the largest finite double value (~1.8 x 10308). Any finite double is less than or equal to it, so x < Double.MAX_VALUE is almost always true (except when x equals Double.MAX_VALUE itself), and x > Double.MAX_VALUE is almost always false. Similarly, Double.MIN_VALUE is the smallest positive non-zero double (~4.9 x 10-324), so comparisons against it are rarely meaningful.

These comparisons typically reveal a misunderstanding of the constant’s purpose or a copy-paste error.

// Bad -- comparison is always true for any normal double
if (x < Double.MAX_VALUE) {
    process(x);
}

// Bad -- comparison is almost always true
if (x > Double.MIN_VALUE) {
    process(x);
}

Remediation

Replace the boundary constant with the actual domain limit the code intends to enforce:

// Good -- use a meaningful domain bound
if (x < MAX_ALLOWED_PRICE) {
    process(x);
}

// Good -- explicit zero check when that is the intent
if (x > 0.0) {
    process(x);
}