stanfordnlp/CoreNLP · error · IllegalArgumentException
Cosine is not between -1 and 1: " + cosValue
Error message
Cosine is not between -1 and 1: " + cosValue
What it means
SloppyMath.acos(double cosValue) uses a precomputed lookup table and requires the input cosine to be within [-1, 1]. Floating-point rounding (or a genuinely invalid input) outside that range throws IllegalArgumentException, as documented in its @throws clause.
Solutions
- Clamp before calling: cosValue = Math.max(-1.0, Math.min(1.0, cosValue))
- Ensure vectors are properly normalized (divide by norm, guarding norm == 0)
- Use Math.acos if you don't need this method's approximation
Example fix
// before double angle = SloppyMath.acos(dot / (normA * normB)); // 1.0000000002 // after double cos = Math.max(-1.0, Math.min(1.0, dot / (normA * normB))); double angle = SloppyMath.acos(cos);
Defensive patterns
Strategy: validation
Validate before calling
if (cosValue < -1.0 || cosValue > 1.0) cosValue = Math.max(-1.0, Math.min(1.0, cosValue));
Type guard
static double clampUnit(double v) { return Math.max(-1.0, Math.min(1.0, v)); } Try / catch
try {
double angle = SloppyMath.acos(cos);
} catch (IllegalArgumentException e) {
double angle = Math.acos(Math.max(-1.0, Math.min(1.0, cos)));
} Prevention
- Always clamp cosine values to [-1, 1] before acos — rounding drift makes 1.0000001 common
- Verify vector normalization; guard the norm against zero before dividing
- Prefer clamping in the similarity helper itself so all callers are protected
When it happens
Trigger: Calling acos with a cosine computed as 1.0000000001 from rounding (e.g. dot product of normalized vectors), or with unnormalized vectors yielding values like 3.2.
Common situations: Vector similarity code where normalization was skipped or divided by zero norm; accumulated floating-point drift slightly exceeding 1.0.
Understand the failure class
Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.
Related errors
- vectorName + " element " + i + " is " + vector[i]
- Math.exp(-lambda) +" "+ Math.pow(lambda, x) + ' ' +…
- neg log lik smaller than 0: " + s
- Counters.dotProduct infinite or NaN value for key:
- Gradient is numerically zero, stopped on machine epsilon.
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/220fb54fe91578b2.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/math/SloppyMath.java:644
}
else {
double den = 1.0 + Math.exp(-x);
return 1.0 / den;
}
}
private static float[] acosCache; // = null;
/**
* Compute acos very quickly by directly looking up the value.
* @param cosValue The cosine of the angle to fine.
* @return The angle corresponding to the cosine value.
* @throws IllegalArgumentException if cosValue is not between -1 and 1
*/
public static double acos(double cosValue) {
if (cosValue < -1.0 || cosValue > 1.0) {
throw new IllegalArgumentException("Cosine is not between -1 and 1: " + cosValue);
}
int numSamples = 10000;
if (acosCache == null) {
acosCache = new float[numSamples + 1];
for (int i = 0; i <= numSamples; ++i) {
double x = 2.0 / ((double) numSamples) * ((double) i) - 1.0;
acosCache[i] = (float) Math.acos(x);
}
}
int i = ((int) (((cosValue + 1.0) / 2.0) * ((double) numSamples)));
return acosCache[i];
}
public static double poisson(int x, double lambda) {
if (x<0 || lambda<=0.0) throw new RuntimeException("Bad arguments: " + x + " and " + lambda);
double p = (Math.exp(-lambda) * Math.pow(lambda, x)) / factorial(x);View on GitHub (pinned to 1b7edd19c4)