{"record":{"id":"f4fb6a6d188086ac","repo":"TheAlgorithms/Java","slug":"slope-and-intercept-must-be-valid-numbers","errorCode":null,"errorMessage":"Slope and intercept must be valid numbers.","messagePattern":"Slope and intercept must be valid numbers\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/conversions/AffineConverter.java","lineNumber":23,"sourceCode":" * y = slope * x + intercept.\n *\n * This class supports inversion and composition of affine transformations.\n * It is immutable, meaning each instance represents a fixed transformation.\n */\npublic final class AffineConverter {\n    private final double slope;\n    private final double intercept;\n\n    /**\n     * Constructs an AffineConverter with the given slope and intercept.\n     *\n     * @param inSlope The slope of the affine transformation.\n     * @param inIntercept The intercept (constant term) of the affine transformation.\n     * @throws IllegalArgumentException if either parameter is NaN.\n     */\n    public AffineConverter(final double inSlope, final double inIntercept) {\n        if (Double.isNaN(inSlope) || Double.isNaN(inIntercept)) {\n            throw new IllegalArgumentException(\"Slope and intercept must be valid numbers.\");\n        }\n        slope = inSlope;\n        intercept = inIntercept;\n    }\n\n    /**\n     * Converts the given input value using the affine transformation:\n     * result = slope * inValue + intercept.\n     *\n     * @param inValue The input value to convert.\n     * @return The transformed value.\n     */\n    public double convert(final double inValue) {\n        return slope * inValue + intercept;\n    }\n\n    /**\n     * Returns a new AffineConverter representing the inverse of the current transformation.","sourceCodeStart":5,"sourceCodeEnd":41,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/conversions/AffineConverter.java#L5-L41","documentation":"AffineConverter's constructor rejects any NaN slope or NaN intercept. The affine transform result = slope * inValue + intercept produces NaN output if either parameter is NaN, so the constructor guards at construction time. Only NaN is rejected; infinity (positive or negative) is accepted.","triggerScenarios":"Passing the result of a failed arithmetic operation (e.g., 0.0/0.0, Math.sqrt(-1)) as slope or intercept. Reading unparseable numeric config and defaulting to NaN. Chaining a prior computation that can return NaN (Math.log of a negative, division by zero in doubles).","commonSituations":"Parsing user input or config files where a non-numeric value yields NaN. Floating-point computations upstream that silently produce NaN. Defaulting unset numeric fields to Double.NaN as a sentinel.","solutions":["Validate with Double.isFinite() before constructing the converter, and reject or substitute a sensible default.","Trace the upstream computation producing the NaN (look for division by zero, negative sqrt, or invalid parse).","If NaN is a valid 'unset' sentinel in your domain, replace it with a defined default before passing it in."],"exampleFix":"// before\ndouble s = Double.parseDouble(userSlope); // NumberFormatException not caught -> NaN leak\ndouble i = computeIntercept(); // may be NaN\nAffineConverter ac = new AffineConverter(s, i);\n\n// after\nif (!Double.isFinite(s) || !Double.isFinite(i)) {\n    throw new IllegalArgumentException(\"slope/intercept must be finite\");\n}\nAffineConverter ac = new AffineConverter(s, i);","handlingStrategy":"validation","validationCode":"if (!Double.isFinite(inSlope) || !Double.isFinite(inIntercept)) {\n    throw new IllegalArgumentException(\"slope and intercept must be finite\");\n}\nAffineConverter ac = new AffineConverter(inSlope, inIntercept);","typeGuard":"static boolean isFiniteAffineParams(double slope, double intercept) {\n    return Double.isFinite(slope) && Double.isFinite(intercept);\n}","tryCatchPattern":"try {\n    AffineConverter ac = new AffineConverter(slope, intercept);\n} catch (IllegalArgumentException e) {\n    throw new DomainException(\"Invalid affine parameters\", e);\n}","preventionTips":["Use Double.isFinite (not just isNaN) to also catch infinities at your boundary.","Trace upstream NaN sources: division by zero, sqrt of negatives, failed parses.","Avoid Double.NaN as an 'unset' sentinel; use Optional<Double> instead."],"tags":["conversions","affine","nan","validation","java"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}