{"record":{"id":"4dc6a0bc6695bf66","repo":"TheAlgorithms/Java","slug":"radius-r-must-be-non-negative","errorCode":null,"errorMessage":"Radius (r) must be non-negative.","messagePattern":"Radius \\(r\\) must be non-negative\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/conversions/CoordinateConverter.java","lineNumber":47,"sourceCode":"        if (!Double.isFinite(x) || !Double.isFinite(y)) {\n            throw new IllegalArgumentException(\"x and y must be finite numbers.\");\n        }\n        double r = Math.sqrt(x * x + y * y);\n        double theta = Math.toDegrees(Math.atan2(y, x));\n        return new double[] {r, theta};\n    }\n\n    /**\n     * Converts Polar coordinates to Cartesian coordinates.\n     *\n     * @param r the radius in the Polar system; must be non-negative\n     * @param thetaDegrees the angle (theta) in degrees in the Polar system; must be a finite number\n     * @return an array where the first element is the x-coordinate and the second element is the y-coordinate in the Cartesian system\n     * @throws IllegalArgumentException if r is negative or thetaDegrees is not a finite number\n     */\n    public static double[] polarToCartesian(double r, double thetaDegrees) {\n        if (r < 0) {\n            throw new IllegalArgumentException(\"Radius (r) must be non-negative.\");\n        }\n        if (!Double.isFinite(thetaDegrees)) {\n            throw new IllegalArgumentException(\"Theta (angle) must be a finite number.\");\n        }\n        double theta = Math.toRadians(thetaDegrees);\n        double x = r * Math.cos(theta);\n        double y = r * Math.sin(theta);\n        return new double[] {x, y};\n    }\n}\n","sourceCodeStart":29,"sourceCodeEnd":58,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/conversions/CoordinateConverter.java#L29-L58","documentation":"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).","triggerScenarios":"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.","commonSituations":"Distance computations that underflow to negative. User-supplied coordinates without range validation. Defaulting unset fields to -1.","solutions":["Validate r >= 0 before calling, and reject or take the absolute value if appropriate.","Fix the upstream computation producing a negative distance (use Math.abs or clamp to 0).","Also check Double.isFinite(r) since this guard does not catch NaN/Inf for the radius."],"exampleFix":"// before\ndouble[] cart = CoordinateConverter.polarToCartesian(-5.0, 45.0);\n\n// after\nif (r < 0 || !Double.isFinite(r)) {\n    throw new IllegalArgumentException(\"radius must be non-negative and finite\");\n}\ndouble[] cart = CoordinateConverter.polarToCartesian(r, theta);","handlingStrategy":"validation","validationCode":"if (r < 0 || !Double.isFinite(r)) {\n    throw new IllegalArgumentException(\"radius must be non-negative and finite\");\n}\ndouble[] cart = CoordinateConverter.polarToCartesian(r, thetaDegrees);","typeGuard":"static boolean isValidRadius(double r) {\n    return r >= 0 && Double.isFinite(r);\n}","tryCatchPattern":"try {\n    double[] cart = CoordinateConverter.polarToCartesian(r, theta);\n} catch (IllegalArgumentException e) {\n    throw new DomainException(\"Invalid polar radius: \" + r, e);\n}","preventionTips":["Validate r >= 0 and isFinite before calling (the guard only checks < 0).","Use Math.abs or clamp to 0 for upstream distance underflows.","Avoid -1 as an 'unset' sentinel for radius."],"tags":["conversions","coordinate","polar","validation","java"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}