{"record":{"id":"135063229a0beb89","repo":"stanfordnlp/CoreNLP","slug":"math-exp-lambda-math-pow-lambda-x","errorCode":null,"errorMessage":"Math.exp(-lambda) +\" \"+ Math.pow(lambda, x) + ' ' + factorial(x)","messagePattern":"Math\\.exp\\(-lambda\\) \\+\" \"\\+ Math\\.pow\\(lambda, x\\) \\+ ' ' \\+ factorial\\(x\\)","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/math/SloppyMath.java","lineNumber":663,"sourceCode":"    }\n    int numSamples = 10000;\n    if (acosCache == null) {\n      acosCache = new float[numSamples + 1];\n      for (int i = 0; i <= numSamples; ++i) {\n        double x = 2.0 / ((double) numSamples) * ((double) i) - 1.0;\n        acosCache[i] = (float) Math.acos(x);\n      }\n    }\n\n    int i = ((int) (((cosValue + 1.0) / 2.0) * ((double) numSamples)));\n    return acosCache[i];\n  }\n\n\n  public static double poisson(int x, double lambda) {\n    if (x<0 || lambda<=0.0) throw new RuntimeException(\"Bad arguments: \" + x + \" and \" + lambda);\n    double p = (Math.exp(-lambda) * Math.pow(lambda, x)) / factorial(x);\n    if (Double.isInfinite(p) || p<=0.0) throw new RuntimeException(Math.exp(-lambda) +\" \"+ Math.pow(lambda, x) + ' ' + factorial(x));\n    return p;\n  }\n\n  /**\n   * Uses floating point so that it can represent the really big numbers that come up.\n   * @param x Argument to take factorial of\n   * @return Factorial of argument\n   */\n  public static double factorial(int x) {\n    double result = 1.0;\n    for (int i=x; i>1; i--) {\n      result *= i;\n    }\n    return result;\n  }\n\n\n  /**","sourceCodeStart":645,"sourceCodeEnd":681,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/math/SloppyMath.java#L645-L681","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\ndouble p = SloppyMath.poisson(x, 10000); // exp(-10000) underflows -> p = 0 -> throws\n// after\ndouble logP = -lambda + x * Math.log(lambda) - SloppyMath.logFactorial(x);\ndouble p = Math.exp(logP);","handlingStrategy":"try-catch","validationCode":"// pre-compute log terms to detect unsafe ranges\nboolean safe = lambda < 700 && x < 170; // approx limits before exp/factorial overflow","typeGuard":null,"tryCatchPattern":"try {\n  double p = SloppyMath.poisson(x, lambda);\n} catch (RuntimeException e) {\n  // fall back to log-space computation\n  double logP = -lambda + x * Math.log(lambda) - SloppyMath.logFactorial(x);\n  double p = Math.exp(logP);\n}","preventionTips":["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"],"tags":["java","numerical","underflow","overflow","statistics"],"backgroundTag":"internal-invariant-violation","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}