TheAlgorithms/Java · error · IllegalArgumentException

Radius (r) must be non-negative.

Error message

Radius (r) must be non-negative.

What it means

Thrown by CoordinateConverter.polarToCartesian(double, double) when the radius r is negative. A polar radius represents distance from the origin and is physically non-negative; negative values produce mathematically valid but semantically wrong coordinates. Note this is the only check on r; NaN and infinity for r are not rejected by this guard (they slip through unless caught by the thetaDegrees finite check).

Source

Thrown at src/main/java/com/thealgorithms/conversions/CoordinateConverter.java:47

        if (!Double.isFinite(x) || !Double.isFinite(y)) {
            throw new IllegalArgumentException("x and y must be finite numbers.");
        }
        double r = Math.sqrt(x * x + y * y);
        double theta = Math.toDegrees(Math.atan2(y, x));
        return new double[] {r, theta};
    }

    /**
     * Converts Polar coordinates to Cartesian coordinates.
     *
     * @param r the radius in the Polar system; must be non-negative
     * @param thetaDegrees the angle (theta) in degrees in the Polar system; must be a finite number
     * @return an array where the first element is the x-coordinate and the second element is the y-coordinate in the Cartesian system
     * @throws IllegalArgumentException if r is negative or thetaDegrees is not a finite number
     */
    public static double[] polarToCartesian(double r, double thetaDegrees) {
        if (r < 0) {
            throw new IllegalArgumentException("Radius (r) must be non-negative.");
        }
        if (!Double.isFinite(thetaDegrees)) {
            throw new IllegalArgumentException("Theta (angle) must be a finite number.");
        }
        double theta = Math.toRadians(thetaDegrees);
        double x = r * Math.cos(theta);
        double y = r * Math.sin(theta);
        return new double[] {x, y};
    }
}

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Validate r >= 0 before calling, and reject or take the absolute value if appropriate.
  2. Fix the upstream computation producing a negative distance (use Math.abs or clamp to 0).
  3. Also check Double.isFinite(r) since this guard does not catch NaN/Inf for the radius.

Example fix

// before
double[] cart = CoordinateConverter.polarToCartesian(-5.0, 45.0);

// after
if (r < 0 || !Double.isFinite(r)) {
    throw new IllegalArgumentException("radius must be non-negative and finite");
}
double[] cart = CoordinateConverter.polarToCartesian(r, theta);
Defensive patterns

Strategy: validation

Validate before calling

if (r < 0 || !Double.isFinite(r)) {
    throw new IllegalArgumentException("radius must be non-negative and finite");
}
double[] cart = CoordinateConverter.polarToCartesian(r, thetaDegrees);

Type guard

static boolean isValidRadius(double r) {
    return r >= 0 && Double.isFinite(r);
}

Try / catch

try {
    double[] cart = CoordinateConverter.polarToCartesian(r, theta);
} catch (IllegalArgumentException e) {
    throw new DomainException("Invalid polar radius: " + r, e);
}

Prevention

When it happens

Trigger: Passing a negative radius from sensor data, distance computation, or user input. Passing -1 as a default/error sentinel. A subtraction or signed operation producing a negative magnitude.

Common situations: Distance computations that underflow to negative. User-supplied coordinates without range validation. Defaulting unset fields to -1.

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/4dc6a0bc6695bf66. Report an issue: GitHub.