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
- Check ArrayMath.L1Norm(a) before the call and skip or substitute a default distribution when it is 0/NaN
- Scrub NaN values (e.g. replace with 0, then re-check the norm)
- Log a hash or summary of the vector yourself before normalizing so a failure is diagnosable
- 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
- Validate the producer of large vectors immediately after construction
- Track which pipeline stage zeroed or NaN-poisoned the vector
- Use your own targeted error message instead of relying on the 100-element dump
- Sanitize NaNs early, once, at pipeline entry
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
- 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/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)