stanfordnlp/CoreNLP · error · ArithmeticException

Can't standardize array whose standard deviation is 0.0 or…

Error message

Can't standardize array whose standard deviation is 0.0 or NaN

What it means

ArrayMath.standardize(double[]) also throws when the array's standard deviation is 0.0 (all elements identical) or NaN, because dividing by such a value is undefined. This check runs after the NaN-mean check, so the array has a valid mean but no spread.

Solutions

  1. Compute ArrayMath.stdev(a) first and skip standardization when it is 0.0 or NaN, leaving the (mean-centered or original) values as-is.
  2. Add an epsilon to the standard deviation before dividing if a standardized scale is required regardless.
  3. Drop constant/degenerate feature columns in preprocessing before the normalization stage.
  4. Catch ArithmeticException and fall back to mean-centering only (addInPlace(a, -ArrayMath.mean(a))).

Example fix

// before
ArrayMath.standardize(feature);
// after
double s = ArrayMath.stdev(feature);
if (s != 0.0 && !Double.isNaN(s)) {
  ArrayMath.standardize(feature);
} else {
  ArrayMath.addInPlace(feature, -ArrayMath.mean(feature)); // center only
}
Defensive patterns

Strategy: validation

Validate before calling

double s = ArrayMath.stdev(a);
if (Double.isNaN(s) || s == 0.0) {
  // skip scaling or center-only
} else {
  ArrayMath.standardize(a);
}

Try / catch

try {
  ArrayMath.standardize(a);
} catch (ArithmeticException e) {
  ArrayMath.addInPlace(a, -ArrayMath.mean(a)); // center-only fallback
}

Prevention

When it happens

Trigger: Calling ArrayMath.standardize(a) where every element of a is the same constant (stdev == 0.0), or stdev computes NaN (e.g. variance overflow or NaN variance from Inf values).

Common situations: Normalizing a feature column that is constant across the dataset (a degenerate feature), or features containing only one repeated default value after sparse data preprocessing.

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

Appendix: source

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

        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.
   * If standard deviation is 0.0, throws a RuntimeException.
   */
  public static void standardize(double[] a) {
    double m = mean(a);
    if (Double.isNaN(m)) {
      throw new ArithmeticException("Can't standardize array whose mean is NaN");
    }
    double s = stdev(a);
    if (s == 0.0 || Double.isNaN(s)) {
      throw new ArithmeticException("Can't standardize array whose standard deviation is 0.0 or NaN");
    }
    addInPlace(a, -m); // subtract mean
    multiplyInPlace(a, 1.0/s); // divide by standard deviation
  }

  public static double L2Norm(double[] a) {
    double result = 0.0;
    for(double d: a) {
      result += d * d;
    }
    return Math.sqrt(result);
  }
  public static float L2Norm(float[] a) {
    double result = 0;
    for(float d: a) {
      result += d * d;
    }
    return (float) Math.sqrt(result);

View on GitHub (pinned to 1b7edd19c4)