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

What it means

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.

Solutions

  1. Check the sum first: float s = ArrayMath.sum(a); if (s != 0.0f && !Float.isNaN(s)) ArrayMath.normalize(a);
  2. Replace NaN entries with 0 before summing; if the values are tiny, normalize in log space with ArrayMath.logNormalize instead
  3. Do the normalization in double (convert to double[], normalize, convert back) to avoid float-precision zero sums
  4. Fall back to a uniform distribution 1.0f/a.length when a zero sum is expected/acceptable

Example fix

// before
ArrayMath.normalize(floatScores); // float sum underflowed to 0
// after
float total = ArrayMath.sum(floatScores);
if (total == 0.0f || Float.isNaN(total)) {
  Arrays.fill(floatScores, 1.0f / floatScores.length);
} else {
  ArrayMath.normalize(floatScores);
}
Defensive patterns

Strategy: validation

Validate before calling

float total = ArrayMath.sum(a);
if (total == 0.0f || Float.isNaN(total))
  throw new IllegalStateException("float array not normalizable: sum=" + total);

Type guard

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

Try / catch

try {
  ArrayMath.normalize(floatArray);
} catch (ArithmeticException e) {
  Arrays.fill(floatArray, 1.0f / floatArray.length);
}

Prevention

When it happens

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

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

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

Appendix: source

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

    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.
   */
  public static void normalize(float[] a) {
    float total = sum(a);
    if (total == 0.0f || Double.isNaN(total)) {
      throw new ArithmeticException("Can't normalize an array with sum 0.0 or NaN");
    }
    multiplyInPlace(a, 1.0f/total); // divide each value by total
  }
  public static void L2normalize(float[] a) {
    float total = L2Norm(a);
    if (total == 0.0 || Float.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
  }

  /**
   * Standardize values in this array, i.e., subtract the mean and divide by the standard deviation.

View on GitHub (pinned to 1b7edd19c4)