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
- Check the sum before normalizing: double s = ArrayMath.sum(a); if (s != 0 && !Double.isNaN(s)) ArrayMath.normalize(a);
- Sanitize the array first: replace NaN with 0 (e.g. loop with Double.isNaN check) before calling normalize
- Use ArrayMath.logNormalize if you are working in log space and the linear sum under/overflows or cancels
- 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
- Sanitize NaNs before any normalize call
- Prefer log-space normalization (logNormalize) for scores that can cancel or underflow
- Decide the semantics of a zero vector up front (uniform vs error vs skip)
- Check non-negativity if you expect a probability-like input
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
- 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 mean is NaN
- Cannot handle weird double: " + d
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)