{"record":{"id":"52739ac9db1544bc","repo":"stanfordnlp/CoreNLP","slug":"cannot-handle-weird-double-d","errorCode":null,"errorMessage":"Cannot handle weird double: \" + d","messagePattern":"Cannot handle weird double: \" \\+ d","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/math/SloppyMath.java","lineNumber":709,"sourceCode":"\n\n  /**\n   * Taken from http://nerds-central.blogspot.com/2011/05/high-speed-parse-double-for-jvm.html\n   */\n  public static double parseDouble(boolean negative, long mantissa, int  exponent) {\n     // Do this with no locals other than the arguments to make it stupid easy\n     // for the JIT compiler to inline the code.\n    int e = -16;\n    return (negative ? -1. : 1.) * (((double)mantissa) * exps[(e + 308)]) * exps[(exponent + 308)];\n  }\n\n\n  /**\n   * Segment a double into a mantissa and exponent.\n   */\n  public static Triple<Boolean, Long, Integer> segmentDouble(double d) {\n    if (Double.isInfinite(d) || Double.isNaN(d)) {\n      throw new IllegalArgumentException(\"Cannot handle weird double: \" + d);\n    }\n    boolean negative = d < 0;\n    d = Math.abs(d);\n    int exponent = 0;\n    while (d >= 10.0) {\n      exponent += 1;\n      d = d / 10.;\n    }\n    while (d < 1.0) {\n      exponent -= 1;\n      d = d * 10.;\n    }\n    return Triple.makeTriple(negative, (long) (d * 10000000000000000.), exponent);\n  }\n\n\n  /**\n   * From http://nadeausoftware.com/articles/2009/08/java_tip_how_parse_integers_quickly","sourceCodeStart":691,"sourceCodeEnd":727,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/math/SloppyMath.java#L691-L727","documentation":"SloppyMath.segmentDouble() decomposes a double into a mantissa/exponent pair, which is only meaningful for finite numbers. The library throws IllegalArgumentException when the input is infinite or NaN because such values have no finite mantissa/exponent representation.","triggerScenarios":"Calling SloppyMath.segmentDouble(Double.NaN), segmentDouble(Double.POSITIVE_INFINITY), or segmentDouble(Double.NEGATIVE_INFINITY). Also triggered indirectly when an upstream computation overflows to Infinity or produces NaN (e.g. 0.0/0.0) and the result is passed in.","commonSituations":"Numerical overflow in probability/log computations in NLP pipelines; dividing by zero-size counts; uninitialized fields that default to NaN being passed to math helpers.","solutions":["Check the value with Double.isFinite(d) before calling segmentDouble and handle non-finite input separately","Trace where the NaN/Infinity originated (usually a division by zero or overflow earlier in the pipeline) and fix the upstream computation","If non-finite values are expected, clamp or sanitize them before segmenting"],"exampleFix":"// before\nTriple<Boolean, Long, Integer> t = SloppyMath.segmentDouble(score);\n// after\nif (!Double.isFinite(score)) {\n  score = 0.0; // or handle/log the bad value\n}\nTriple<Boolean, Long, Integer> t = SloppyMath.segmentDouble(score);","handlingStrategy":"validation","validationCode":"if (!Double.isFinite(d)) { throw new IllegalArgumentException(\"Expected finite double, got: \" + d); }","typeGuard":"boolean isUsableDouble(Object v) { return v instanceof Double && Double.isFinite((Double) v); }","tryCatchPattern":"try { segmentDouble(d); } catch (IllegalArgumentException e) { /* sanitize d and retry or skip */ }","preventionTips":["Guard all externally-sourced doubles with Double.isFinite before math helpers","Enable NaN checks in upstream numeric pipelines","Unit-test edge inputs (NaN, +/-Infinity, 0.0)"],"tags":["java","math","nan","infinity","invalid-argument"],"backgroundTag":"invalid-argument-value","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}