c++exprtk

Simple Math Expression in ExprTk


I am attempting to use a simple expression such as the following and the result should be that the value of z becomes 1. However nothing seems to be happening any suggestions on how I could resolve this issue ?

template<typename t>
void MyTestB()
{

    t x = 1.0;
    t z = 0;

    std::string e = "if((x + 2) == 3){z=1;}";
    exprtk::symbol_table<t> symbol_table;
    symbol_table.add_variable("x",x);
    symbol_table.add_variable("z",z);

    exprtk::expression<t> expression;
    expression.register_symbol_table(symbol_table);


    exprtk::parser<t> parser;

    parser.compile(e,expression);
    t y = expression.value();
    std::cout << z;
}

The program does finish however at y = NAN (which is understandable because expression is a conditional statement) However z still remains 0. I was expecting it to become 1


Solution

  • Looking at the examples, it appears that if statements should have the form:

    if (condition, expression if true, expression if false)

    Also, assignment uses := instead of just =. So you should use the string:

    if((x + 2) == 3, z := 1, 0)