{"record":{"id":"ed22e185af3c69f0","repo":"stanfordnlp/CoreNLP","slug":"can-t-normalize-an-array-with-sum-0-0-or-nan","errorCode":null,"errorMessage":"Can't normalize an array with sum 0.0 or NaN: \" + Arrays.toString(a)","messagePattern":"Can't normalize an array with sum 0\\.0 or NaN: \" \\+ Arrays\\.toString\\(a\\)","errorType":"exception","errorClass":"ArithmeticException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/math/ArrayMath.java","lineNumber":1328,"sourceCode":"      double[] drow = doubleCounts[i];\n      int[] row = result[i] = new int[drow.length];\n      for (int j=0; j<drow.length; j++) {\n        row[j] = (int) drow[j];\n      }\n    }\n    return result;\n  }\n\n  // PROBABILITY FUNCTIONS\n\n  /**\n   * Makes the values in this array sum to 1.0. Does it in place.\n   * If the total is 0.0 or NaN, throws an RuntimeException.\n   */\n  public static void normalize(double[] a) {\n    double total = sum(a);\n    if (total == 0.0 || Double.isNaN(total)) {\n      throw new ArithmeticException(\"Can't normalize an array with sum 0.0 or NaN: \" + Arrays.toString(a));\n    }\n    multiplyInPlace(a, 1.0/total); // divide each value by total\n  }\n\n  public static void L1normalize(double[] a) {\n    double total = L1Norm(a);\n    if (total == 0.0 || Double.isNaN(total))\n      if (a.length < 100) {\n        throw new ArithmeticException(\"Can't normalize an array with sum 0.0 or NaN: \" + Arrays.toString(a));\n      } else {\n        throw new ArithmeticException(\"Can't normalize an array with sum 0.0 or NaN: \" + Arrays.toString(Arrays.copyOf(a, 100)) + \" ... \");\n      }\n    multiplyInPlace(a, 1.0/total); // divide each value by total\n  }\n  public static void L2normalize(double[] a) {\n    double total = L2Norm(a);\n    if (total == 0.0 || Double.isNaN(total)) {\n      if (a.length < 100) {","sourceCodeStart":1310,"sourceCodeEnd":1346,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/math/ArrayMath.java#L1310-L1346","documentation":"ArrayMath.normalize(double[]) scales the array in place so its elements sum to 1.0. If the sum is 0.0 or NaN, division is impossible, so it throws ArithmeticException including the full array contents to aid debugging. This protects callers from silently producing all-NaN output.","triggerScenarios":"Calling ArrayMath.normalize(a) on an array of all zeros, an array of mixed positive/negative values summing to zero, or an array containing NaN (making the sum NaN).","commonSituations":"Turning unnormalized scores into a probability distribution when a model produced no positive evidence; empty/zero-initialized score buffers; upstream NaN poisoning the sum; subtracting a mean that makes the vector sum exactly zero.","solutions":["Check the sum before normalizing: double s = ArrayMath.sum(a); if (s != 0 && !Double.isNaN(s)) ArrayMath.normalize(a);","Sanitize the array first: replace NaN with 0 (e.g. loop with Double.isNaN check) before calling normalize","Use ArrayMath.logNormalize if you are working in log space and the linear sum under/overflows or cancels","Handle a uniform distribution fallback: if the sum is 0, fill with 1.0/a.length when that is semantically valid"],"exampleFix":"// before\nArrayMath.normalize(scores); // throws if all scores are 0\n// after\ndouble total = ArrayMath.sum(scores);\nif (total == 0.0 || Double.isNaN(total)) {\n  Arrays.fill(scores, 1.0 / scores.length); // uniform fallback\n} else {\n  ArrayMath.normalize(scores);\n}","handlingStrategy":"validation","validationCode":"double total = ArrayMath.sum(a);\nif (total == 0.0 || Double.isNaN(total))\n  throw new IllegalStateException(\"cannot normalize: sum=\" + total);\nfor (double v : a) { if (Double.isNaN(v)) throw new IllegalStateException(\"NaN in input array\"); }","typeGuard":"static boolean normalizable(double[] a) {\n  double t = ArrayMath.sum(a);\n  return t != 0.0 && !Double.isNaN(t);\n}","tryCatchPattern":"try {\n  ArrayMath.normalize(a);\n} catch (ArithmeticException e) {\n  Arrays.fill(a, 1.0 / a.length); // uniform fallback\n}","preventionTips":["Sanitize NaNs before any normalize call","Prefer log-space normalization (logNormalize) for scores that can cancel or underflow","Decide the semantics of a zero vector up front (uniform vs error vs skip)","Check non-negativity if you expect a probability-like input"],"tags":["math","normalization","nan","zero-division","probability"],"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-17T15:17:12.973Z"}