TheAlgorithms/Java · error · IllegalArgumentException
Slope and intercept must be valid numbers.
Error message
Slope and intercept must be valid numbers.
What it means
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.
Source
Thrown at src/main/java/com/thealgorithms/conversions/AffineConverter.java:23
* y = slope * x + intercept.
*
* This class supports inversion and composition of affine transformations.
* It is immutable, meaning each instance represents a fixed transformation.
*/
public final class AffineConverter {
private final double slope;
private final double intercept;
/**
* Constructs an AffineConverter with the given slope and intercept.
*
* @param inSlope The slope of the affine transformation.
* @param inIntercept The intercept (constant term) of the affine transformation.
* @throws IllegalArgumentException if either parameter is NaN.
*/
public AffineConverter(final double inSlope, final double inIntercept) {
if (Double.isNaN(inSlope) || Double.isNaN(inIntercept)) {
throw new IllegalArgumentException("Slope and intercept must be valid numbers.");
}
slope = inSlope;
intercept = inIntercept;
}
/**
* Converts the given input value using the affine transformation:
* result = slope * inValue + intercept.
*
* @param inValue The input value to convert.
* @return The transformed value.
*/
public double convert(final double inValue) {
return slope * inValue + intercept;
}
/**
* Returns a new AffineConverter representing the inverse of the current transformation.View on GitHub (pinned to fdfb9a395b)
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.
Example fix
// before
double s = Double.parseDouble(userSlope); // NumberFormatException not caught -> NaN leak
double i = computeIntercept(); // may be NaN
AffineConverter ac = new AffineConverter(s, i);
// after
if (!Double.isFinite(s) || !Double.isFinite(i)) {
throw new IllegalArgumentException("slope/intercept must be finite");
}
AffineConverter ac = new AffineConverter(s, i); Defensive patterns
Strategy: validation
Validate before calling
if (!Double.isFinite(inSlope) || !Double.isFinite(inIntercept)) {
throw new IllegalArgumentException("slope and intercept must be finite");
}
AffineConverter ac = new AffineConverter(inSlope, inIntercept); Type guard
static boolean isFiniteAffineParams(double slope, double intercept) {
return Double.isFinite(slope) && Double.isFinite(intercept);
} Try / catch
try {
AffineConverter ac = new AffineConverter(slope, intercept);
} catch (IllegalArgumentException e) {
throw new DomainException("Invalid affine parameters", e);
} Prevention
- 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.
When it happens
Trigger: 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).
Common situations: 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.
Related errors
- x and y must be finite numbers.
- For input string: {}
- Input cannot be null
- Invalid Base64 input length; must be multiple of 4
- Padding '=' can only appear at the end (last 1 or 2 characte
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/f4fb6a6d188086ac.
Report an issue: GitHub.