{"record":{"id":"2289f12243fed544","repo":"stanfordnlp/CoreNLP","slug":"counters-dotproduct-infinite-or-nan-value-for-key","errorCode":null,"errorMessage":"Counters.dotProduct infinite or NaN value for key: ","messagePattern":"Counters\\.dotProduct infinite or NaN value for key: ","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/stats/Counters.java","lineNumber":1210,"sourceCode":"    return result;\n  }\n\n  /**\n   * Returns the product of c1 and c2.\n   *\n   * @return The product of c1 and c2.\n   */\n  public static <E> double dotProduct(Counter<E> c1, Counter<E> c2) {\n    double dotProd = 0.0;\n    if (c1.size() > c2.size()) {\n      Counter<E> tmpCnt = c1;\n      c1 = c2;\n      c2 = tmpCnt;\n    }\n    for (E key : c1.keySet()) {\n      double count1 = c1.getCount(key);\n      if (Double.isNaN(count1) || Double.isInfinite(count1)) {\n        throw new RuntimeException(\"Counters.dotProduct infinite or NaN value for key: \" + key + '\\t' + c1.getCount(key) + '\\t' + c2.getCount(key));\n      }\n      if (count1 != 0.0) {\n        double count2 = c2.getCount(key);\n        if (Double.isNaN(count2) || Double.isInfinite(count2)) {\n          throw new RuntimeException(\"Counters.dotProduct infinite or NaN value for key: \" + key + '\\t' + c1.getCount(key) + '\\t' + c2.getCount(key));\n        }\n        if (count2 != 0.0) {\n          // this is the inner product\n          dotProd += (count1 * count2);\n        }\n      }\n    }\n    return dotProd;\n  }\n\n  /**\n   * Returns the product of Counter c and double[] a, using Index idx to map\n   * entries in C onto a.","sourceCodeStart":1192,"sourceCodeEnd":1228,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/stats/Counters.java#L1192-L1228","documentation":"Counters.dotProduct validates every nonzero-or-checked count of the smaller counter before multiplying: if c1's count for a key is NaN or infinite, it throws RuntimeException naming the key and both counters' values. This guards the dot product from silently producing NaN/Infinity.","triggerScenarios":"Calling Counters.dotProduct(c1, c2) when either counter contains a count set to Double.NaN or +/-Infinity — usually from dividing by zero, log(0), or downstream arithmetic overflow.","commonSituations":"Building counters from log-probabilities where log(0) produced -Infinity; smoothing/normalization code dividing by a zero total; accidental division by zero in feature extraction.","solutions":["Find how the offending key got a NaN/Infinity count (the message names the key and both values)","Sanitize counts before the dot product: replace non-finite values with 0 or clamp","Fix upstream math: guard divisions (total>0) and use log(0+epsilon) instead of log(0)"],"exampleFix":"// before\ndouble dot = Counters.dotProduct(logCounterA, logCounterB); // logCounterA has -Infinity for \"unk\"\n// after\ncounterA.setCount(\"unk\", 0.0); // or use Math.log(1e-10) upstream\ndouble dot = Counters.dotProduct(counterA, counterB);","handlingStrategy":"validation","validationCode":"boolean isFinite(Counter<?> c) {\n  for (Object k : c.keySet()) {\n    double v = c.getCount(k);\n    if (Double.isNaN(v) || Double.isInfinite(v)) return false;\n  }\n  return true;\n}","typeGuard":null,"tryCatchPattern":"try {\n  dot = Counters.dotProduct(c1, c2);\n} catch (RuntimeException e) {\n  log.warn(\"non-finite counts: {}\", e.getMessage());\n  sanitize(c1); sanitize(c2);\n  dot = Counters.dotProduct(c1, c2);\n}","preventionTips":["Sanitize counters with a removeNonFiniteCounts-style pass before math","Guard divisions by zero totals; use log(x + epsilon)","Unit-test pipelines against NaN/Infinity propagation"],"tags":["java","numerical","nan","validation"],"backgroundTag":"invalid-argument-value","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"}