Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple Math Expression in ExprTk

Tags:

c++

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

like image 497
MistyD Avatar asked Sep 18 '13 14:09

MistyD


People also ask

What is an example of a mathematical expression?

An example of a mathematical expression with a variable is 2x + 3. All variables must have a coefficient, a number that is multiplied by the variable. In the expression 2x + 3, the coefficient of x is the number 2, and it means 2 times x plus 3.

What is this mathematical expression?

When we combine numbers and variables in a valid way, using operations such as addition, subtraction, multiplication, division, exponentiation, and other operations and functions as yet unlearned, the resulting combination of mathematical symbols is called a mathematical expression.


1 Answers

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)

like image 168
Taylor Brandstetter Avatar answered Sep 28 '22 04:09

Taylor Brandstetter