{"record":{"id":"220fb54fe91578b2","repo":"stanfordnlp/CoreNLP","slug":"cosine-is-not-between-1-and-1-cosvalue","errorCode":null,"errorMessage":"Cosine is not between -1 and 1: \" + cosValue","messagePattern":"Cosine is not between -1 and 1: \" \\+ cosValue","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/math/SloppyMath.java","lineNumber":644,"sourceCode":"    }\n    else {\n      double den = 1.0 + Math.exp(-x);\n      return 1.0 / den;\n    }\n  }\n\n\n  private static float[] acosCache; // = null;\n\n  /**\n   * Compute acos very quickly by directly looking up the value.\n   * @param cosValue The cosine of the angle to fine.\n   * @return The angle corresponding to the cosine value.\n   * @throws IllegalArgumentException if cosValue is not between -1 and 1\n   */\n  public static double acos(double cosValue) {\n    if (cosValue < -1.0 || cosValue > 1.0) {\n      throw new IllegalArgumentException(\"Cosine is not between -1 and 1: \" + cosValue);\n    }\n    int numSamples = 10000;\n    if (acosCache == null) {\n      acosCache = new float[numSamples + 1];\n      for (int i = 0; i <= numSamples; ++i) {\n        double x = 2.0 / ((double) numSamples) * ((double) i) - 1.0;\n        acosCache[i] = (float) Math.acos(x);\n      }\n    }\n\n    int i = ((int) (((cosValue + 1.0) / 2.0) * ((double) numSamples)));\n    return acosCache[i];\n  }\n\n\n  public static double poisson(int x, double lambda) {\n    if (x<0 || lambda<=0.0) throw new RuntimeException(\"Bad arguments: \" + x + \" and \" + lambda);\n    double p = (Math.exp(-lambda) * Math.pow(lambda, x)) / factorial(x);","sourceCodeStart":626,"sourceCodeEnd":662,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/math/SloppyMath.java#L626-L662","documentation":"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.","triggerScenarios":"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.","commonSituations":"Vector similarity code where normalization was skipped or divided by zero norm; accumulated floating-point drift slightly exceeding 1.0.","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"],"exampleFix":"// before\ndouble angle = SloppyMath.acos(dot / (normA * normB)); // 1.0000000002\n// after\ndouble cos = Math.max(-1.0, Math.min(1.0, dot / (normA * normB)));\ndouble angle = SloppyMath.acos(cos);","handlingStrategy":"validation","validationCode":"if (cosValue < -1.0 || cosValue > 1.0) cosValue = Math.max(-1.0, Math.min(1.0, cosValue));","typeGuard":"static double clampUnit(double v) { return Math.max(-1.0, Math.min(1.0, v)); }","tryCatchPattern":"try {\n  double angle = SloppyMath.acos(cos);\n} catch (IllegalArgumentException e) {\n  double angle = Math.acos(Math.max(-1.0, Math.min(1.0, cos)));\n}","preventionTips":["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"],"tags":["java","numerical","floating-point","trigonometry"],"backgroundTag":"value-out-of-range","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}