cyberdstar / muparserx

Automatically exported from code.google.com/p/muparserx
Other
0 stars 0 forks source link

?: operator must be last in expression #42

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?

It doesn't seem to be possible to have ?: in an expression except if it's the 
last one.
This:
abs(y) < 0.25 ? (-1) : (1) + 1
gives either -1 or 2 as an answer not 0 and 2 as might be expected from the 
parentheses and this doesn't work:
(abs(y) < 0.25 ? -1 : 1)
Unexpected parenthesis "(" found at position 1....
(Which also precludes e.g. (x+y)*(x+y), etc.)

So seems it's impossible to have e.g. two independent ?:s in an expression:

(abs(x) < 1 ? -1 : 1) + (abs(y) < 1 ? 1 : -1)
abs(x) < 1 ? (-1) : (1) + abs(y) < 1 ? 1 : -1

What version of the product are you using? On what operating system?
3.0.3, macosx

Original issue reported on code.google.com by ilja.j.h...@nasa.gov on 2 Dec 2014 at 4:02

GoogleCodeExporter commented 9 years ago
Ok the problem seems to be the opening parenthesis. I guess prepending all 
expressions with 0+ in that case would work.

Original comment by ilja.j.h...@nasa.gov on 2 Dec 2014 at 4:12

GoogleCodeExporter commented 9 years ago
Here is the result of my investigation:

1.) abs(y) < 0.25 ? (-1) : (1) + 1
I wouldn't expect 0 or two in the first expression. The behavior is consistent 
with the behavior of C/C++ source code in the same situation. You need a way to 
terminate the ?: Operator this is either the end of an expression or a closing 
bracket.

2.) (abs(y) < 0.25 ? -1 : 1)
This would be a bug but I can't reproduce the issue. I checked gcc, llvm and 
Visual Studio. 

I added both cases to the unit test and it is running fine. If i remember 
correctly there are some issues in the LLVM version shipped with OSX. My best 
guess so far is that this may be the cause of the issue.

Original comment by ib...@gmx.info on 10 Dec 2014 at 8:38

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Does your test also work with vector expressions? Starting an expression with ( 
seems to work for me with scalar variables but not with vector ones.

Original comment by ilja.j.h...@nasa.gov on 10 Dec 2014 at 9:39

GoogleCodeExporter commented 9 years ago
Seems to work for vectors. I'd be suprised if it didn't because this error is 
thrown in the parsing stage. At that stage the type is still unknown. 

My test sample was "((size(va)[0]==3 ? 1:2)" can you try to give me a simple 
expression showing the behavior? 

Original comment by ib...@gmx.info on 10 Dec 2014 at 10:29

GoogleCodeExporter commented 9 years ago
I meant an expression that produces a vector, e.g. this works: {1, 0, 0} but 
this fails: {(1), 0, 0}. Only the first component seems to be affected.

Original comment by ilja.j.h...@nasa.gov on 10 Dec 2014 at 10:43

GoogleCodeExporter commented 9 years ago
Thanks for the clarification. I fixed this issue in the SVN repository (V3.0.5).

Original comment by ib...@gmx.info on 14 Dec 2014 at 5:57

GoogleCodeExporter commented 9 years ago
Thanks!

Original comment by ilja.j.h...@nasa.gov on 14 Dec 2014 at 6:33