TheAlgorithms/Java · warning · IllegalArgumentException
Cannot convert NaN to long!
Error message
Cannot convert NaN to long!
What it means
Thrown inside MathBuilder.toLong() when the internal result is NaN. IMPORTANT: this exception is immediately caught by the method's own catch (Exception ex) block at line 44, which silently returns 0. The caller never sees the exception; the observable symptom is that toLong() returns an unexpected 0. NaN typically results from operations like sqrt of a negative number, log of a negative, or 0.0/0.0.
Source
Thrown at src/main/java/com/thealgorithms/maths/MathBuilder.java:29
* Date: Oct 20, 2024
*/
public final class MathBuilder {
private final double result;
private MathBuilder(Builder builder) {
this.result = builder.number;
}
// Returns final result
public double get() {
return result;
}
// Return result in long
public long toLong() {
try {
if (Double.isNaN(result)) {
throw new IllegalArgumentException("Cannot convert NaN to long!");
}
if (result == Double.POSITIVE_INFINITY) {
return Long.MAX_VALUE;
}
if (result == Double.NEGATIVE_INFINITY) {
return Long.MIN_VALUE;
}
if (result > Long.MAX_VALUE) {
return Long.MAX_VALUE;
}
if (result < Long.MIN_VALUE) {
return Long.MIN_VALUE;
}
return Math.round(result);
} catch (Exception ex) {
return 0;
}
}View on GitHub (pinned to fdfb9a395b)
Solutions
- Check Double.isNaN(builder.get()) before calling toLong() to detect NaN results
- Avoid operations that produce NaN: validate inputs to sqrt, log, and division before applying them
- Be aware that toLong() silently returns 0 for NaN — do not rely on it to signal errors
Example fix
// before
MathBuilder mb = new MathBuilder.Builder(-1.0).sqrt().build();
long result = mb.toLong(); // silently returns 0
// after
MathBuilder mb = new MathBuilder.Builder(-1.0).sqrt().build();
if (Double.isNaN(mb.get())) {
throw new ArithmeticException("Result is NaN, cannot convert to long");
}
long result = mb.toLong(); Defensive patterns
Strategy: validation
Validate before calling
MathBuilder mb = builder.build();
double value = mb.get();
if (Double.isNaN(value)) {
throw new ArithmeticException("MathBuilder result is NaN, cannot convert to long");
}
long result = mb.toLong(); Type guard
static boolean isConvertibleToLong(double value) {
return !Double.isNaN(value);
} Prevention
- Always check mb.get() for NaN before calling toLong() — the exception is swallowed internally and toLong() silently returns 0
- Avoid operations that produce NaN: sqrt of negatives, log of negatives, 0.0/0.0
- Treat toLong() returning 0 as suspicious — it may indicate a swallowed NaN exception
When it happens
Trigger: Building a MathBuilder whose result is NaN (e.g., via sqrt() on a negative, log() on a negative) and then calling toLong(). The caller gets 0, not the exception. The exception is only visible in a debugger or if the catch block is removed.
Common situations: Chaining builder operations that produce NaN without checking intermediate state. Debugging why toLong() returns 0 unexpectedly — the swallowed exception hides the real cause.
Related errors
- Number must be zero for random assignment!
- Number must be zero for PI assignment!
- Number must be zero for E assignment!
- Number must be zero to set!
- Slope and intercept must be valid numbers.
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/505d402ca5eafa02.
Report an issue: GitHub.