javascriptevalmathjs

mathjs eval error: (intermediate value)(intermediate value)(intermediate value) is not a function


If I run a code:

    obj = {};
    obj['number'] = 1;
    obj['expressionS'] = 'Sin(0.5 * c1)';
    obj['compiledExpressionS'] = null;

    let cnr = 'c' + obj.number;
    let params = {};
    params[cnr] = 2;

    var parsedExpressionS = math.parse(obj.expressionS);
    obj.compiledExpressionS = parsedExpressionS.compile();
    let value = obj.compiledExpressionS.eval(params);

I will get an error (thrown in last line): (intermediate value)(intermediate value)(intermediate value) is not a function. Mathjs library is used here (docs: http://mathjs.org/docs/expressions/parsing.html ). I have no idea why the error apear.


Solution

  • The issue is that you have not complied the value ,

    obj.compiledExpressionS = parsedExpressionS.compile(math);
    

    and that Sin should be lower case

    Working fiddle : http://jsfiddle.net/oxb3sp5o/414/

    Working code

    obj = {};
    obj['number'] = 1;
    obj['expressionS'] = 'sin(0.5 * c1)';
    obj['compiledExpressionS'] = null;
    
    let cnr = 'c' + obj.number;
    let params = {};
    params[cnr] = 2;
    
    var parsedExpressionS = math.parse(obj.expressionS);
    obj.compiledExpressionS = parsedExpressionS.compile(math);
    let value = obj.compiledExpressionS.eval(params);
    console.log(value);