{"record":{"id":"4ff4e9d62fb4c899","repo":"pentaho/pentaho-kettle","slug":"function-cos-only-works-with-a-number","errorCode":null,"errorMessage":"Function COS only works with a number","messagePattern":"Function COS only works with a number","errorType":"exception","errorClass":"KettleValueException","httpStatus":null,"severity":"error","filePath":"core/src/main/java/org/pentaho/di/compatibility/Value.java","lineNumber":2456,"sourceCode":"\n    if ( isNumeric() ) {\n      setValue( Math.ceil( getNumber() ) );\n    } else {\n      throw new KettleValueException( \"Function CEIL only works with a number\" );\n    }\n    return this;\n  }\n\n  // implement the COS function, arguments in args[]\n  public Value cos() throws KettleValueException {\n    if ( isNull() ) {\n      return this;\n    }\n\n    if ( isNumeric() ) {\n      setValue( Math.cos( getNumber() ) );\n    } else {\n      throw new KettleValueException( \"Function COS only works with a number\" );\n    }\n    return this;\n  }\n\n  // implement the EXP function, arguments in args[]\n  public Value exp() throws KettleValueException {\n    if ( isNull() ) {\n      return this;\n    }\n\n    if ( isNumeric() ) {\n      setValue( Math.exp( getNumber() ) );\n    } else {\n      throw new KettleValueException( \"Function EXP only works with a number\" );\n    }\n    return this;\n  }\n","sourceCodeStart":2438,"sourceCodeEnd":2474,"githubUrl":"https://github.com/pentaho/pentaho-kettle/blob/f3058517a153da500bf4551f46d79b91bf8ec552/core/src/main/java/org/pentaho/di/compatibility/Value.java#L2438-L2474","documentation":"The cos() method of Kettle's legacy Value class computes the cosine of the value (in radians) via Math.cos. If the Value is not numeric it throws this KettleValueException, because the underlying Java Math call requires a double. Type enforcement is done at runtime by the legacy compatibility Value class.","triggerScenarios":"Calling Value.cos() when the Value holds a String, Date, Boolean or other non-numeric type - typically an angle value read as text and not converted.","commonSituations":"Trigonometric calculations in legacy JavaScript/Formula steps; angle columns from Excel/CSV inputs typed as String; transformations where a type-conversion step was deleted.","solutions":["Convert the Value to Number before calling cos() (setType(VALUE_TYPE_NUMBER))","Fix the input step so the field is declared numeric","Guard with isNumeric() and convert or skip non-numeric rows"],"exampleFix":"// before\nValue result = angle.cos(); // angle is a String\n// after\nangle.setType(Value.VALUE_TYPE_NUMBER);\nValue result = angle.cos();","handlingStrategy":"type-guard","validationCode":"if (value == null || !value.isNumeric()) {\n  throw new IllegalArgumentException(\"COS requires a numeric value, got type \" + (value == null ? \"null\" : value.getType()));\n}","typeGuard":"boolean isNumericValue(Value v) { return v != null && v.isNumeric(); }","tryCatchPattern":"try {\n  result = value.cos();\n} catch (KettleValueException e) {\n  value.setType(Value.VALUE_TYPE_NUMBER);\n  result = value.cos();\n}","preventionTips":["Ensure angle fields are declared numeric at read time","Convert String-typed angles to Number before trigonometry","Precondition-check with isNumeric() before calling cos()","Re-check field types after modifying or reordering transformation steps"],"tags":["kettle","numeric-type","math-function","runtime-type-check"],"backgroundTag":"type-mismatch","analyzedSha":"f3058517a153da500bf4551f46d79b91bf8ec552","analyzedAt":"2026-09-13T14:04:16.340Z","contentChangedAt":"2026-09-13T14:04:16.340Z","schemaVersion":2},"datasetVersion":"2026-09-20T23:17:15.980Z"}