{"record":{"id":"e617f8b4259f29ab","repo":"stanfordnlp/CoreNLP","slug":"can-t-normalize-an-array-with-sum-0-0-or-nan-e617f8","errorCode":null,"errorMessage":"Can't normalize an array with sum 0.0 or NaN","messagePattern":"Can't normalize an array with sum 0\\.0 or NaN","errorType":"exception","errorClass":"ArithmeticException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/math/ArrayMath.java","lineNumber":1362,"sourceCode":"    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.\n   */\n  public static void normalize(float[] a) {\n    float total = sum(a);\n    if (total == 0.0f || Double.isNaN(total)) {\n      throw new ArithmeticException(\"Can't normalize an array with sum 0.0 or NaN\");\n    }\n    multiplyInPlace(a, 1.0f/total); // divide each value by total\n  }\n  public static void L2normalize(float[] a) {\n    float total = L2Norm(a);\n    if (total == 0.0 || Float.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    }\n    multiplyInPlace(a, 1.0/total); // divide each value by total\n  }\n\n  /**\n   * Standardize values in this array, i.e., subtract the mean and divide by the standard deviation.","sourceCodeStart":1344,"sourceCodeEnd":1380,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/math/ArrayMath.java#L1344-L1380","documentation":"ArrayMath.normalize(float[]) scales a float array in place to sum to 1.0. If the sum is 0.0f or NaN it throws ArithmeticException with a fixed message (no array contents). Note the guard tests Double.isNaN(total) even though total is float — both 0.0 and NaN sums are rejected because dividing would yield NaN/Infinity everywhere.","triggerScenarios":"Calling ArrayMath.normalize on a float[] whose float sum is exactly 0.0 (including float rounding of tiny values to 0) or NaN — e.g. an all-zero score buffer or one containing Float.NaN.","commonSituations":"Float score arrays from a model that predicted nothing positive; float underflow where many tiny positives sum to 0.0f; NaN entering via 0/0 upstream; converting double pipelines to float and hitting precision loss.","solutions":["Check the sum first: float s = ArrayMath.sum(a); if (s != 0.0f && !Float.isNaN(s)) ArrayMath.normalize(a);","Replace NaN entries with 0 before summing; if the values are tiny, normalize in log space with ArrayMath.logNormalize instead","Do the normalization in double (convert to double[], normalize, convert back) to avoid float-precision zero sums","Fall back to a uniform distribution 1.0f/a.length when a zero sum is expected/acceptable"],"exampleFix":"// before\nArrayMath.normalize(floatScores); // float sum underflowed to 0\n// after\nfloat total = ArrayMath.sum(floatScores);\nif (total == 0.0f || Float.isNaN(total)) {\n  Arrays.fill(floatScores, 1.0f / floatScores.length);\n} else {\n  ArrayMath.normalize(floatScores);\n}","handlingStrategy":"validation","validationCode":"float total = ArrayMath.sum(a);\nif (total == 0.0f || Float.isNaN(total))\n  throw new IllegalStateException(\"float array not normalizable: sum=\" + total);","typeGuard":"static boolean normalizable(float[] a) { float t = ArrayMath.sum(a); return t != 0.0f && !Float.isNaN(t); }","tryCatchPattern":"try {\n  ArrayMath.normalize(floatArray);\n} catch (ArithmeticException e) {\n  Arrays.fill(floatArray, 1.0f / floatArray.length);\n}","preventionTips":["Watch for float underflow: tiny positives can sum to exactly 0.0f — use logNormalize or double","Sanitize Float.NaN before summing","Prefer double[] for normalization math, converting only at the edges","Test float score pipelines with all-zero and NaN inputs"],"tags":["math","normalization","float","nan","precision"],"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"}