{"record":{"id":"d55be0e77030dd45","repo":"TheAlgorithms/Java","slug":"x-and-y-must-be-finite-numbers","errorCode":null,"errorMessage":"x and y must be finite numbers.","messagePattern":"x and y must be finite numbers\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/conversions/CoordinateConverter.java","lineNumber":30,"sourceCode":" * <p>The class is final and cannot be instantiated.\n */\npublic final class CoordinateConverter {\n\n    private CoordinateConverter() {\n        // Prevent instantiation\n    }\n\n    /**\n     * Converts Cartesian coordinates to Polar coordinates.\n     *\n     * @param x the x-coordinate in the Cartesian system; must be a finite number\n     * @param y the y-coordinate in the Cartesian system; must be a finite number\n     * @return an array where the first element is the radius (r) and the second element is the angle (theta) in degrees\n     * @throws IllegalArgumentException if x or y is not a finite number\n     */\n    public static double[] cartesianToPolar(double x, double y) {\n        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        }","sourceCodeStart":12,"sourceCodeEnd":48,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/conversions/CoordinateConverter.java#L12-L48","documentation":"Thrown by CoordinateConverter.cartesianToPolar(double, double) when either x or y is not a finite number (i.e., is NaN, positive infinity, or negative infinity). Finite coordinates are required because Math.sqrt and Math.atan2 produce undefined results for infinite/NaN inputs, and the returned radius/angle would be meaningless.","triggerScenarios":"Passing Double.NaN, Double.POSITIVE_INFINITY, or Double.NEGATIVE_INFINITY as x or y. Passing the result of an upstream computation that overflowed or divided by zero in floating point. Reading unparseable numeric input that defaulted to NaN.","commonSituations":"Sensor data with dropout values represented as NaN/Inf. Config or input parsing failures yielding NaN. Mathematical operations that can overflow (large exponents, divisions).","solutions":["Filter or reject NaN/Inf at the data ingestion boundary before conversion.","Validate with Double.isFinite(x) && Double.isFinite(y) before calling.","Substitute a sentinel (e.g., 0.0) or skip the conversion if the input is non-finite."],"exampleFix":"// before\ndouble[] polar = CoordinateConverter.cartesianToPolar(Double.NaN, 5.0);\n\n// after\nif (!Double.isFinite(x) || !Double.isFinite(y)) {\n    throw new IllegalArgumentException(\"coordinates must be finite\");\n}\ndouble[] polar = CoordinateConverter.cartesianToPolar(x, y);","handlingStrategy":"validation","validationCode":"if (!Double.isFinite(x) || !Double.isFinite(y)) {\n    throw new IllegalArgumentException(\"coordinates must be finite\");\n}\ndouble[] polar = CoordinateConverter.cartesianToPolar(x, y);","typeGuard":"static boolean areFiniteCoordinates(double x, double y) {\n    return Double.isFinite(x) && Double.isFinite(y);\n}","tryCatchPattern":"try {\n    double[] polar = CoordinateConverter.cartesianToPolar(x, y);\n} catch (IllegalArgumentException e) {\n    throw new DomainException(\"Non-finite coordinates\", e);\n}","preventionTips":["Filter NaN/Inf at the data ingestion boundary.","Use Double.isFinite (covers NaN and both infinities).","Avoid Double.NaN as a sentinel; use Optional or null-check patterns."],"tags":["conversions","coordinate","nan","infinity","validation","java"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}