TheAlgorithms/Java · error · IllegalArgumentException
x and y must be finite numbers.
Error message
x and y must be finite numbers.
What it means
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.
Source
Thrown at src/main/java/com/thealgorithms/conversions/CoordinateConverter.java:30
* <p>The class is final and cannot be instantiated.
*/
public final class CoordinateConverter {
private CoordinateConverter() {
// Prevent instantiation
}
/**
* Converts Cartesian coordinates to Polar coordinates.
*
* @param x the x-coordinate in the Cartesian system; must be a finite number
* @param y the y-coordinate in the Cartesian system; must be a finite number
* @return an array where the first element is the radius (r) and the second element is the angle (theta) in degrees
* @throws IllegalArgumentException if x or y is not a finite number
*/
public static double[] cartesianToPolar(double x, double y) {
if (!Double.isFinite(x) || !Double.isFinite(y)) {
throw new IllegalArgumentException("x and y must be finite numbers.");
}
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.");
}View on GitHub (pinned to fdfb9a395b)
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.
Example fix
// before
double[] polar = CoordinateConverter.cartesianToPolar(Double.NaN, 5.0);
// after
if (!Double.isFinite(x) || !Double.isFinite(y)) {
throw new IllegalArgumentException("coordinates must be finite");
}
double[] polar = CoordinateConverter.cartesianToPolar(x, y); Defensive patterns
Strategy: validation
Validate before calling
if (!Double.isFinite(x) || !Double.isFinite(y)) {
throw new IllegalArgumentException("coordinates must be finite");
}
double[] polar = CoordinateConverter.cartesianToPolar(x, y); Type guard
static boolean areFiniteCoordinates(double x, double y) {
return Double.isFinite(x) && Double.isFinite(y);
} Try / catch
try {
double[] polar = CoordinateConverter.cartesianToPolar(x, y);
} catch (IllegalArgumentException e) {
throw new DomainException("Non-finite coordinates", e);
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: Sensor data with dropout values represented as NaN/Inf. Config or input parsing failures yielding NaN. Mathematical operations that can overflow (large exponents, divisions).
Related errors
- Slope and intercept must be valid numbers.
- Radius (r) must be non-negative.
- For input string: {}
- Input cannot be null
- Invalid Base64 input length; must be multiple of 4
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/d55be0e77030dd45.
Report an issue: GitHub.