{"record":{"id":"fb812f92a6def121","repo":"stanfordnlp/CoreNLP","slug":"can-t-normalize-an-array-with-sum-0-0-or-nan-fb812f","errorCode":null,"errorMessage":"Can't normalize an array with sum 0.0 or NaN: \" + Arrays.toString(Arrays.copyOf(a, 100)) + \" ... ","messagePattern":"Can't normalize an array with sum 0\\.0 or NaN: \" \\+ Arrays\\.toString\\(Arrays\\.copyOf\\(a, 100\\)\\) \\+ \" \\.\\.\\. ","errorType":"exception","errorClass":"ArithmeticException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/math/ArrayMath.java","lineNumber":1339,"sourceCode":"  /**\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) {\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    }\n    multiplyInPlace(a, 1.0/total); // divide each value by total\n  }\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.","sourceCodeStart":1321,"sourceCodeEnd":1357,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/math/ArrayMath.java#L1321-L1357","documentation":"The long-array branch of ArrayMath.L1normalize: when a.length >= 100 and the L1 norm is 0.0 or NaN, the exception message truncates the array to its first 100 elements plus \" ... \" to keep the error readable. The cause is identical to the short-array case — a zero or NaN L1 norm.","triggerScenarios":"Calling ArrayMath.L1normalize on a large (>=100 element) array that is entirely zeros or contains NaN, so L1Norm returns 0.0 or NaN.","commonSituations":"Normalizing big feature/gradient vectors where a silent upstream failure zeroed everything; NaN contamination in long numeric pipelines; mistakenly calling L1normalize on an already-normalized all-zero buffer.","solutions":["Check ArrayMath.L1Norm(a) before the call and skip or substitute a default distribution when it is 0/NaN","Scrub NaN values (e.g. replace with 0, then re-check the norm)","Log a hash or summary of the vector yourself before normalizing so a failure is diagnosable","Verify the producer of the array — an all-zero 100+ vector usually indicates a missing computation step"],"exampleFix":"// before\nArrayMath.L1normalize(largeVec); // throws with truncated dump\n// after\ndouble l1 = ArrayMath.L1Norm(largeVec);\nif (Double.isNaN(l1) || l1 == 0.0) {\n  log.warn(\"L1 norm is \" + l1 + \"; using uniform distribution\");\n  Arrays.fill(largeVec, 1.0 / largeVec.length);\n} else {\n  ArrayMath.L1normalize(largeVec);\n}","handlingStrategy":"validation","validationCode":"double l1 = ArrayMath.L1Norm(largeVec);\nif (l1 == 0.0 || Double.isNaN(l1)) {\n  log.warn(\"L1 norm \" + l1 + \", first NaN at index \" + indexOfNaN(largeVec));\n  return;\n}","typeGuard":"static boolean l1Normalizable(double[] a) { double t = ArrayMath.L1Norm(a); return t > 0.0 && !Double.isNaN(t); }","tryCatchPattern":"try {\n  ArrayMath.L1normalize(largeVec);\n} catch (ArithmeticException e) {\n  log.warn(\"zero/NaN L1 norm in large vector; skipping normalization\");\n}","preventionTips":["Validate the producer of large vectors immediately after construction","Track which pipeline stage zeroed or NaN-poisoned the vector","Use your own targeted error message instead of relying on the 100-element dump","Sanitize NaNs early, once, at pipeline entry"],"tags":["math","normalization","l1-norm","nan","large-arrays"],"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"}