{"record":{"id":"ed0e651cd52d533a","repo":"TheAlgorithms/Java","slug":"theta-angle-must-be-a-finite-number","errorCode":null,"errorMessage":"Theta (angle) must be a finite number.","messagePattern":"Theta \\(angle\\) must be a finite number\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/conversions/CoordinateConverter.java","lineNumber":50,"sourceCode":"        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":32,"sourceCodeEnd":58,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/conversions/CoordinateConverter.java#L32-L58","documentation":"Thrown by CoordinateConverter.polarToCartesian when thetaDegrees is NaN, positive infinity, or negative infinity. The method uses Double.isFinite() to guard the angle because Math.cos/Math.sin would silently propagate NaN or Infinity into the x/y Cartesian result, producing garbage coordinates.","triggerScenarios":"Passing Double.NaN, Double.POSITIVE_INFINITY, or Double.NEGATIVE_INFINITY as the thetaDegrees argument. Also triggered when thetaDegrees originates from a division-by-zero (0.0/0.0) or an unbounded result from a prior calculation like Math.log of a non-positive value.","commonSituations":"Angle data read from a sensor or external API that returns NaN on failure. Parsing theta from a config file or JSON payload where the numeric field is missing, null, or corrupt. Chaining from upstream math operations that produce infinity without the caller realizing it.","solutions":["Call Double.isFinite(thetaDegrees) before invoking polarToCartesian and handle the invalid case.","Sanitize the upstream data source — replace or reject NaN/Infinity at the boundary before the value reaches the conversion.","If parsing from a string, validate the parsed double immediately after Double.parseDouble and before passing it downstream."],"exampleFix":"// before\ndouble[] result = CoordinateConverter.polarToCartesian(r, theta); // theta may be NaN\n\n// after\nif (!Double.isFinite(theta)) {\n    throw new IllegalArgumentException(\"theta must be finite, got: \" + theta);\n}\ndouble[] result = CoordinateConverter.polarToCartesian(r, theta);","handlingStrategy":"validation","validationCode":"if (!Double.isFinite(thetaDegrees)) {\n    throw new IllegalArgumentException(\"thetaDegrees must be finite, got: \" + thetaDegrees);\n}\ndouble[] result = CoordinateConverter.polarToCartesian(r, thetaDegrees);","typeGuard":"static boolean isValidTheta(double theta) {\n    return Double.isFinite(theta);\n}","tryCatchPattern":"try {\n    double[] result = CoordinateConverter.polarToCartesian(r, theta);\n} catch (IllegalArgumentException e) {\n    // theta was NaN or infinite; log and use a default\n    logger.warn(\"Invalid theta: {}, using 0\", theta);\n}","preventionTips":["Always use Double.isFinite() rather than separate isNaN/isInfinite checks for a single guard.","Validate doubles at the data boundary (parse, deserialize) rather than deep in call chains.","Treat NaN as a sentinel for data failure and reject it explicitly."],"tags":["coordinate-conversion","input-validation","floating-point","math"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}