You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When I evaluate (-1)^0 I get -1, which is wrong. Yet when I evaluate pow(-1, 0) I get 1, which is correct. It seems to be an expression compilation error since te_eval only runs case TE_CONSTANT: return n->value;. (-x)^0 also always yields -1 regardless of the value of x, while x^0 and (0-x)^0 correctly yield 1. I'm using the logic branch in case that matters, with TE_POW_FROM_RIGHT defined.
Btw for the ease of debugging I suggest that you name your tokens enum, something like enum token and use enum token type; instead of int type; in the state struct, this way debuggers will show the name of the enum.
Interesting. It seems to only happen with TE_POW_FROM_RIGHT defined. It's definitely a parser error. With TE_POW_FROM_RIGHT defined, it parses the expression as negation(power(1, 0)) instead of power(negation(1),0).
When TE_POW_FROM_RIGHT is defined, there is special logic to ensure that -a^b = -(a^b). The problem is that it will optimize (-1)^0 to -1^0 to -(1^0), which is very wrong.
Without TE_POW_FROM_RIGHT, tinyexpr sees -a^b as (-a)^b, so there is no problem in that case.
There are probably other cases such as -a^-b^-c that need special consideration. I guess with TE_POW_FROM_RIGHT that should parse as -(a^(-(b^(-c)))), whereas without TE_POW_FROM_RIGHT it should parse as ((-a)^(-b))^(-c).
I think the problem can only be handled correctly by introducing another precedence level for the unary minus (and plus) sign, as it is be done by regular C expression parsers in the compiler, too.
Activity
codeplea commentedon Jul 3, 2019
Interesting. It seems to only happen with
TE_POW_FROM_RIGHTdefined. It's definitely a parser error. WithTE_POW_FROM_RIGHTdefined, it parses the expression asnegation(power(1, 0))instead ofpower(negation(1),0).codeplea commentedon Jul 4, 2019
When
TE_POW_FROM_RIGHTis defined, there is special logic to ensure that-a^b=-(a^b). The problem is that it will optimize(-1)^0to-1^0to-(1^0), which is very wrong.Without
TE_POW_FROM_RIGHT, tinyexpr sees-a^bas(-a)^b, so there is no problem in that case.There are probably other cases such as
-a^-b^-cthat need special consideration. I guess withTE_POW_FROM_RIGHTthat should parse as-(a^(-(b^(-c)))), whereas withoutTE_POW_FROM_RIGHTit should parse as((-a)^(-b))^(-c).I will try to work up a solution soon.
codeplea commentedon Oct 2, 2020
I've added some extra test cases to
smoke.c. A fix is still coming...ArashPartow commentedon Jun 7, 2022
@Photosounder Same issue raised here: #10 (comment)
Photosounder commentedon Jun 7, 2022
Doesn't matter to me anymore, I've made a more sophisticated replacement for TinyExpr https://github.com/Photosounder/rouziclib#expression-parsing 😁
ArashPartow commentedon Jun 8, 2022
Very nice 👍
RokerHRO commentedon Apr 8, 2024
I think the problem can only be handled correctly by introducing another precedence level for the unary minus (and plus) sign, as it is be done by regular C expression parsers in the compiler, too.
Fix unary operator handling with TE_POW_FROM_RIGHT, fixes codeplea#52