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(Arrays.copyOf(a, 100)) + " ... 

What it means

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.

Solutions

  1. Check ArrayMath.L1Norm(a) before the call and skip or substitute a default distribution when it is 0/NaN
  2. Scrub NaN values (e.g. replace with 0, then re-check the norm)
  3. Log a hash or summary of the vector yourself before normalizing so a failure is diagnosable
  4. Verify the producer of the array — an all-zero 100+ vector usually indicates a missing computation step

Example fix

// before
ArrayMath.L1normalize(largeVec); // throws with truncated dump
// after
double l1 = ArrayMath.L1Norm(largeVec);
if (Double.isNaN(l1) || l1 == 0.0) {
  log.warn("L1 norm is " + l1 + "; using uniform distribution");
  Arrays.fill(largeVec, 1.0 / largeVec.length);
} else {
  ArrayMath.L1normalize(largeVec);
}
Defensive patterns

Strategy: validation

Validate before calling

double l1 = ArrayMath.L1Norm(largeVec);
if (l1 == 0.0 || Double.isNaN(l1)) {
  log.warn("L1 norm " + l1 + ", first NaN at index " + indexOfNaN(largeVec));
  return;
}

Type guard

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

Try / catch

try {
  ArrayMath.L1normalize(largeVec);
} catch (ArithmeticException e) {
  log.warn("zero/NaN L1 norm in large vector; skipping normalization");
}

Prevention

When it happens

Trigger: Calling ArrayMath.L1normalize on a large (>=100 element) array that is entirely zeros or contains NaN, so L1Norm returns 0.0 or NaN.

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

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/fb812f92a6def121. Report an issue: GitHub.

Appendix: source

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

  /**
   * 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) {
        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
  }

  /**
   * 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.

View on GitHub (pinned to 1b7edd19c4)