stanfordnlp/CoreNLP · error · ArithmeticException

Can't normalize an array with sum 0.0 or NaN: " +…

Error message

Can't normalize an array with sum 0.0 or NaN: " + Arrays.toString(a)

What it means

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.

Solutions

  1. Check the sum before normalizing: double s = ArrayMath.sum(a); if (s != 0 && !Double.isNaN(s)) ArrayMath.normalize(a);
  2. Sanitize the array first: replace NaN with 0 (e.g. loop with Double.isNaN check) before calling normalize
  3. Use ArrayMath.logNormalize if you are working in log space and the linear sum under/overflows or cancels
  4. Handle a uniform distribution fallback: if the sum is 0, fill with 1.0/a.length when that is semantically valid

Example fix

// before
ArrayMath.normalize(scores); // throws if all scores are 0
// after
double total = ArrayMath.sum(scores);
if (total == 0.0 || Double.isNaN(total)) {
  Arrays.fill(scores, 1.0 / scores.length); // uniform fallback
} else {
  ArrayMath.normalize(scores);
}
Defensive patterns

Strategy: validation

Validate before calling

double total = ArrayMath.sum(a);
if (total == 0.0 || Double.isNaN(total))
  throw new IllegalStateException("cannot normalize: sum=" + total);
for (double v : a) { if (Double.isNaN(v)) throw new IllegalStateException("NaN in input array"); }

Type guard

static boolean normalizable(double[] a) {
  double t = ArrayMath.sum(a);
  return t != 0.0 && !Double.isNaN(t);
}

Try / catch

try {
  ArrayMath.normalize(a);
} catch (ArithmeticException e) {
  Arrays.fill(a, 1.0 / a.length); // uniform fallback
}

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10). Data as JSON: /api/errors/ed22e185af3c69f0. Report an issue: GitHub.

Appendix: source

Thrown at src/edu/stanford/nlp/math/ArrayMath.java:1328

      double[] drow = doubleCounts[i];
      int[] row = result[i] = new int[drow.length];
      for (int j=0; j<drow.length; j++) {
        row[j] = (int) drow[j];
      }
    }
    return result;
  }

  // PROBABILITY FUNCTIONS

  /**
   * Makes the values in this array sum to 1.0. Does it in place.
   * If the total is 0.0 or NaN, throws an RuntimeException.
   */
  public static void normalize(double[] a) {
    double total = sum(a);
    if (total == 0.0 || Double.isNaN(total)) {
      throw new ArithmeticException("Can't normalize an array with sum 0.0 or NaN: " + Arrays.toString(a));
    }
    multiplyInPlace(a, 1.0/total); // divide each value by total
  }

  public static void L1normalize(double[] a) {
    double total = L1Norm(a);
    if (total == 0.0 || Double.isNaN(total))
      if (a.length < 100) {
        throw new ArithmeticException("Can't normalize an array with sum 0.0 or NaN: " + Arrays.toString(a));
      } else {
        throw new ArithmeticException("Can't normalize an array with sum 0.0 or NaN: " + Arrays.toString(Arrays.copyOf(a, 100)) + " ... ");
      }
    multiplyInPlace(a, 1.0/total); // divide each value by total
  }
  public static void L2normalize(double[] a) {
    double total = L2Norm(a);
    if (total == 0.0 || Double.isNaN(total)) {
      if (a.length < 100) {

View on GitHub (pinned to 1b7edd19c4)