TheAlgorithms/Java · error · IllegalArgumentException
Theta (angle) must be a finite number.
Error message
Theta (angle) must be a finite number.
What it means
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.
Source
Thrown at src/main/java/com/thealgorithms/conversions/CoordinateConverter.java:50
double r = Math.sqrt(x * x + y * y);
double theta = Math.toDegrees(Math.atan2(y, x));
return new double[] {r, theta};
}
/**
* Converts Polar coordinates to Cartesian coordinates.
*
* @param r the radius in the Polar system; must be non-negative
* @param thetaDegrees the angle (theta) in degrees in the Polar system; must be a finite number
* @return an array where the first element is the x-coordinate and the second element is the y-coordinate in the Cartesian system
* @throws IllegalArgumentException if r is negative or thetaDegrees is not a finite number
*/
public static double[] polarToCartesian(double r, double thetaDegrees) {
if (r < 0) {
throw new IllegalArgumentException("Radius (r) must be non-negative.");
}
if (!Double.isFinite(thetaDegrees)) {
throw new IllegalArgumentException("Theta (angle) must be a finite number.");
}
double theta = Math.toRadians(thetaDegrees);
double x = r * Math.cos(theta);
double y = r * Math.sin(theta);
return new double[] {x, y};
}
}
View on GitHub (pinned to fdfb9a395b)
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.
Example fix
// before
double[] result = CoordinateConverter.polarToCartesian(r, theta); // theta may be NaN
// after
if (!Double.isFinite(theta)) {
throw new IllegalArgumentException("theta must be finite, got: " + theta);
}
double[] result = CoordinateConverter.polarToCartesian(r, theta); Defensive patterns
Strategy: validation
Validate before calling
if (!Double.isFinite(thetaDegrees)) {
throw new IllegalArgumentException("thetaDegrees must be finite, got: " + thetaDegrees);
}
double[] result = CoordinateConverter.polarToCartesian(r, thetaDegrees); Type guard
static boolean isValidTheta(double theta) {
return Double.isFinite(theta);
} Try / catch
try {
double[] result = CoordinateConverter.polarToCartesian(r, theta);
} catch (IllegalArgumentException e) {
// theta was NaN or infinite; log and use a default
logger.warn("Invalid theta: {}, using 0", theta);
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Input 'n' is too big to give accurate result.
- The number must be in the range [%d, %d]
- Input array must have length of at least two
- Input must be non-negative
- numOfTerms nonnegative.
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/ed0e651cd52d533a.
Report an issue: GitHub.