stanfordnlp/CoreNLP · error · ArithmeticException
Can't standardize array whose mean is NaN
Error message
Can't standardize array whose mean is NaN
What it means
ArrayMath.standardize(double[]) subtracts the mean and divides by the standard deviation. If the array's mean is NaN, standardization is meaningless, so it throws an ArithmeticException. A NaN mean almost always means the array contains at least one NaN (or +Inf/-Inf) element.
Solutions
- Scan the array before calling standardize and remove or impute NaN/Infinite elements.
- Check ArrayMath.isEmpty / a.length > 0 before standardizing; reject empty arrays at the caller.
- Fix the upstream data parsing/generation step that introduced NaN into the array.
- Catch ArithmeticException if NaN inputs are expected and use a fallback (e.g. return the array unchanged or use precomputed statistics).
Example fix
// before
ArrayMath.standardize(values);
// after
boolean hasBad = false;
for (double v : values) { if (Double.isNaN(v) || Double.isInfinite(v)) { hasBad = true; break; } }
if (values.length > 0 && !hasBad) {
ArrayMath.standardize(values);
} Defensive patterns
Strategy: validation
Validate before calling
if (a.length == 0) throw new IllegalArgumentException("empty array");
for (double v : a) {
if (Double.isNaN(v) || Double.isInfinite(v)) throw new IllegalArgumentException("bad value: " + v);
} Try / catch
try {
ArrayMath.standardize(a);
} catch (ArithmeticException e) {
// fall back: leave data unscaled or impute
} Prevention
- Validate arrays for NaN/Inf/emptiness before standardization
- Fix parsing code that lets 'NaN' or missing values into numeric arrays
- Impute or drop bad elements during preprocessing
- Compute mean/stdev yourself first to log which condition fails
When it happens
Trigger: Calling ArrayMath.standardize(a) where mean(a) returns NaN — i.e. the array contains NaN, or Inf and -Inf together, or is empty (mean of empty array yields NaN).
Common situations: Feature normalization pipelines where one feature value was parsed from bad data ('NaN' string, missing value), or empty arrays passed accidentally from empty collections.
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
- Can't normalize an array with sum 0.0 or NaN: " +…
- Can't normalize an array with sum 0.0 or NaN
- Can't normalize an array with sum 0.0 or NaN: " +…
- Can't sample from NaN
- Can't standardize array whose standard deviation is 0.0 or…
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/0c9ca167b08b1bdd.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/math/ArrayMath.java:1386
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.
* 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;View on GitHub (pinned to 1b7edd19c4)