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
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.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With