stanfordnlp/CoreNLP · error · RuntimeException
Math.exp(-lambda) +" "+ Math.pow(lambda, x) + ' ' +…
Error message
Math.exp(-lambda) +" "+ Math.pow(lambda, x) + ' ' + factorial(x)
What it means
The second guard in SloppyMath.poisson: after computing p = exp(-lambda)*lambda^x/factorial(x), the result must be a finite positive probability. If p is Infinite (underflow/overflow in the intermediate terms, e.g. factorial overflow) or <= 0 (exp(-lambda) underflowed to 0 for very large lambda), a RuntimeException is thrown showing the three intermediate values.
Solutions
- Compute in log space: p = Math.exp(-lambda + x*Math.log(lambda) - SloppyMath.logFactorial(x)) (or use math libraries like Commons Math PoissonDistribution)
- For large lambda/x, switch to a normal approximation or a dedicated Poisson implementation
- Clamp lambda/x to a numerically safe range before calling
Example fix
// before double p = SloppyMath.poisson(x, 10000); // exp(-10000) underflows -> p = 0 -> throws // after double logP = -lambda + x * Math.log(lambda) - SloppyMath.logFactorial(x); double p = Math.exp(logP);
Defensive patterns
Strategy: try-catch
Validate before calling
// pre-compute log terms to detect unsafe ranges boolean safe = lambda < 700 && x < 170; // approx limits before exp/factorial overflow
Try / catch
try {
double p = SloppyMath.poisson(x, lambda);
} catch (RuntimeException e) {
// fall back to log-space computation
double logP = -lambda + x * Math.log(lambda) - SloppyMath.logFactorial(x);
double p = Math.exp(logP);
} Prevention
- For large lambda or x, always compute in log space instead of the naive formula
- Know the numeric limits: factorial overflows near x > 170, exp underflows for lambda > ~745
- Use a robust stats library (e.g. Commons Math PoissonDistribution) for tail probabilities
When it happens
Trigger: Calling poisson with a very large lambda (exp(-lambda) underflows to 0.0) or a large x (factorial(x) overflows to Infinity), so the computed p is 0 or Infinite.
Common situations: Rare-event modeling with huge rates, computing PMF far in the tail, or using this naive implementation where a log-space computation (log-Poisson) is required.
Understand the failure class
Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.
Related errors
- vectorName + " element " + i + " is " + vector[i]
- Invalid hypergeometric
- Invalid Fisher's exact: " + "k=" + k + " n=" + n + " r=" +…
- Cosine is not between -1 and 1: " + cosValue
- Bad arguments: " + x + " and " + lambda
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/135063229a0beb89.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/math/SloppyMath.java:663
}
int numSamples = 10000;
if (acosCache == null) {
acosCache = new float[numSamples + 1];
for (int i = 0; i <= numSamples; ++i) {
double x = 2.0 / ((double) numSamples) * ((double) i) - 1.0;
acosCache[i] = (float) Math.acos(x);
}
}
int i = ((int) (((cosValue + 1.0) / 2.0) * ((double) numSamples)));
return acosCache[i];
}
public static double poisson(int x, double lambda) {
if (x<0 || lambda<=0.0) throw new RuntimeException("Bad arguments: " + x + " and " + lambda);
double p = (Math.exp(-lambda) * Math.pow(lambda, x)) / factorial(x);
if (Double.isInfinite(p) || p<=0.0) throw new RuntimeException(Math.exp(-lambda) +" "+ Math.pow(lambda, x) + ' ' + factorial(x));
return p;
}
/**
* Uses floating point so that it can represent the really big numbers that come up.
* @param x Argument to take factorial of
* @return Factorial of argument
*/
public static double factorial(int x) {
double result = 1.0;
for (int i=x; i>1; i--) {
result *= i;
}
return result;
}
/**View on GitHub (pinned to 1b7edd19c4)