{"record":{"id":"0bbcea3d3cb97cef","repo":"xai-org/x-algorithm","slug":"expecting-a-non-negative-number-for-sqrt-functio","errorCode":null,"errorMessage":"expecting a non-negative number for Sqrt() function but received %f","messagePattern":"expecting a non-negative number for Sqrt\\(\\) function but received (.+?)","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"botmaker/src/java/com/twitter/botmaker/function/math/Sqrt.java","lineNumber":50,"sourceCode":"\n  public Sqrt(String exprText, ImmutableList<ASTNode> children) throws SemanticCheckFailure {\n    super(exprText, children);\n  }\n\n  @Override\n  public Signature getSignature() {\n    return SIGNATURE;\n  }\n\n  @Override\n  protected CacheLevel getCacheLevel() {\n    return CacheLevel.Global;\n  }\n\n  @Override\n  protected Object apply(Context<Runtime> context, Number num) {\n    if (num.doubleValue() < 0) {\n      throw new IllegalArgumentException(\n          String.format(\"expecting a non-negative number for Sqrt() function but received %f\",\n              num.doubleValue())\n      );\n    }\n    return Math.sqrt(num.doubleValue());\n  }\n}\n","sourceCodeStart":32,"sourceCodeEnd":58,"githubUrl":"https://github.com/xai-org/x-algorithm/blob/24c60942c5c5fdad3a6addffb4c6e6d2f228f04f/botmaker/src/java/com/twitter/botmaker/function/math/Sqrt.java#L32-L58","documentation":"Sqrt.apply throws IllegalArgumentException when its numeric argument is negative, because Math.sqrt of a negative would yield NaN. The function demands a non-negative input at runtime.","triggerScenarios":"Sqrt(-1) or Sqrt(x) where x evaluates to any value < 0.0, including -0.0 boundaries being fine but small negative results of floating computations (e.g. -1e-16 from rounding) failing the check.","commonSituations":"Computing norms/distances where floating rounding produces tiny negatives, user-supplied parameters not validated, subtracting timestamps/values that can go negative.","solutions":["Clamp the argument to >= 0 before calling Sqrt: Sqrt(Max(x, 0)) or Math.max(x, 0.0)","Validate user-supplied inputs for non-negativity earlier in the pipeline","If NaN is acceptable, wrap in a custom try-catch and substitute NaN/0"],"exampleFix":"// before\nSqrt().apply(ctx, delta); // delta = -1e-16 -> IllegalArgumentException\n// after\nSqrt().apply(ctx, Math.max(delta, 0.0));\n","handlingStrategy":"validation","validationCode":"double safe = Math.max(num, 0.0);\nreturn Sqrt().apply(ctx, safe);","typeGuard":"public static boolean sqrtDomainOk(double v) { return v >= 0.0 && !Double.isNaN(v); }","tryCatchPattern":"try {\n  return Sqrt().apply(ctx, v);\n} catch (IllegalArgumentException e) {\n  return Double.NaN; // or 0.0 per business rule\n}","preventionTips":["Clamp deltas from floating arithmetic before Sqrt (Max(x, 0))","Validate user-supplied numeric params for non-negativity at input parsing","Unit-test boundary values (-0.0, tiny negatives) around zero"],"tags":["math","sqrt","domain-error","negative-value"],"backgroundTag":"math-domain-error","analyzedSha":"24c60942c5c5fdad3a6addffb4c6e6d2f228f04f","analyzedAt":"2026-08-28T11:40:14.686Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}