Hello All,
I get different boolean output for this expression: a = false && false || true && true; print(a); output JavaScript: true output Macro: 0 Documentation says: The ImageJ macro language supports almost all of the standard Java operators but with fewer precendence levels. But hopefully the above is not intended! Norbert -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
On Thursday 05 Jun 2014 22:54:24 Norbert Vischer wrote:
> a = false && false || true && true; > print(a); Maybe you need to write the macro: a = (false && false) || (true && true); That returns 1 -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
In reply to this post by vischer
Hi Norbert,
On Thu, 5 Jun 2014, Norbert Vischer wrote: > I get different boolean output for this expression: > > a = false && false || true && true; > print(a); > > > output JavaScript: true > output Macro: 0 > > > Documentation says: > The ImageJ macro language supports almost all of the standard Java > operators but with fewer precendence levels. > But hopefully the above is not intended! You are asking an awful lot of a toy interpreter. Basically you are asking to implement the macro language to implement the same priorities with regards to Boolean operators as Javascript. To repeat: that is an awful lot. It is true that Javascript lets && bind stronger than ||. There is no logical reason to do so, just the tradition of the Unix shell where people wanted to be able to write do_this && do_that && do_yet_another_thing || fail where fail would be executed whenever anything in that && chain before that failed. But from a mathematical point of view, it makes no sense at all to give && preference over || whatsoever, these operators are equivalent (a && b is exactly the same as !(!a || !b)). Therefore, again, you are asking an awful lot of the poor little macro language. Is it really that hard to add explicit parentheses? Or to switch to a real language for that massive program of yours? Ciao, Johannes -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
Hi Johannes,
I always thought that precedences like "MULTIPLY before ADD" in algebra, or "AND before OR" in boolean algebra (which uses the same notation * and +), have some logical reason that is stronger than the laziness of Unix programmers. But what matters is that it is an established convention. I sent this to the List not because I wasn't aware of the work-around beforehand. Instead, I wanted to make aware of an inconsistency when converting code between macro and java(script), which usually works surprisingly well. Secondly, I wanted to ask Wayne to remove something which I considered to be a simple bug. Norbert On 6. Jun 2014, at 0:16, Johannes Schindelin <[hidden email]> wrote: Hi Norbert, On Thu, 5 Jun 2014, Norbert Vischer wrote: > I get different boolean output for this expression: > > a = false && false || true && true; > print(a); > > > output JavaScript: true > output Macro: 0 > > > Documentation says: > The ImageJ macro language supports almost all of the standard Java > operators but with fewer precendence levels. > But hopefully the above is not intended! You are asking an awful lot of a toy interpreter. Basically you are asking to implement the macro language to implement the same priorities with regards to Boolean operators as Javascript. To repeat: that is an awful lot. It is true that Javascript lets && bind stronger than ||. There is no logical reason to do so, just the tradition of the Unix shell where people wanted to be able to write do_this && do_that && do_yet_another_thing || fail where fail would be executed whenever anything in that && chain before that failed. But from a mathematical point of view, it makes no sense at all to give && preference over || whatsoever, these operators are equivalent (a && b is exactly the same as !(!a || !b)). Therefore, again, you are asking an awful lot of the poor little macro language. Is it really that hard to add explicit parentheses? Or to switch to a real language for that massive program of yours? Ciao, Johannes Norbert Vischer Research Engineer Centre for Advanced Microscopy Swammerdam Institute for Life Sciences (SILS) University of Amsterdam Science Park 904 1098 XH Amsterdam, the Netherlands -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
Precedence varies from context to context. “Commonly used” is not the same as “universal truth”.
Everyone believes that the version they currently use, or the one they first learned, is the “correct” one. (Otherwise, why would they use it?) When in doubt…() Now, if we could only convert everyone to RPN, these problems would disappear (and other problems would appear). -- Kenneth Sloan [hidden email] On Jun 5, 2014, at 18:30 , Norbert Vischer <[hidden email]> wrote: > Hi Johannes, > > I always thought that precedences like "MULTIPLY before ADD" in algebra, or "AND before OR" in boolean algebra (which uses the same notation * and +), have some logical reason that is stronger than the laziness of Unix programmers. But what matters is that it is an established convention. > I sent this to the List not because I wasn't aware of the work-around beforehand. Instead, I wanted to make aware of an inconsistency when converting code between macro and java(script), which usually works surprisingly well. Secondly, I wanted to ask Wayne to remove something which I considered to be a simple bug. > > Norbert > > > > > On 6. Jun 2014, at 0:16, Johannes Schindelin <[hidden email]> wrote: > > Hi Norbert, > > On Thu, 5 Jun 2014, Norbert Vischer wrote: > >> I get different boolean output for this expression: >> >> a = false && false || true && true; >> print(a); >> >> >> output JavaScript: true >> output Macro: 0 >> >> >> Documentation says: >> The ImageJ macro language supports almost all of the standard Java >> operators but with fewer precendence levels. >> But hopefully the above is not intended! > > You are asking an awful lot of a toy interpreter. Basically you are asking > to implement the macro language to implement the same priorities with > regards to Boolean operators as Javascript. To repeat: that is an awful > lot. > > It is true that Javascript lets && bind stronger than ||. There is no > logical reason to do so, just the tradition of the Unix shell where people > wanted to be able to write > > do_this && do_that && do_yet_another_thing || fail > > where fail would be executed whenever anything in that && chain before > that failed. > > But from a mathematical point of view, it makes no sense at all to give && > preference over || whatsoever, these operators are equivalent (a && b is > exactly the same as !(!a || !b)). > > Therefore, again, you are asking an awful lot of the poor little macro > language. Is it really that hard to add explicit parentheses? Or to switch > to a real language for that massive program of yours? > > Ciao, > Johannes > > Norbert Vischer > Research Engineer > Centre for Advanced Microscopy > Swammerdam Institute for Life Sciences (SILS) > University of Amsterdam > Science Park 904 > 1098 XH Amsterdam, the Netherlands > > -- > ImageJ mailing list: http://imagej.nih.gov/ij/list.html -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
In reply to this post by vischer
Hi everyone,
the simplified precedence scheme for operators in the ImageJ macro language is well documented: http://rsb.info.nih.gov/ij/developer/macro/macros.html#operators I guess that this simplification helps to keep the macro interpreter simple and also very fast (considering that it uses no kind of compilation or abstract syntax tree). Michael ________________________________________________________________ On Jun 6, 2014, at 01:30, Norbert Vischer wrote: > Hi Johannes, > > I always thought that precedences like "MULTIPLY before ADD" in algebra, or "AND before OR" in boolean algebra (which uses the same notation * and +), have some logical reason that is stronger than the laziness of Unix programmers. But what matters is that it is an established convention. > I sent this to the List not because I wasn't aware of the work-around beforehand. Instead, I wanted to make aware of an inconsistency when converting code between macro and java(script), which usually works surprisingly well. Secondly, I wanted to ask Wayne to remove something which I considered to be a simple bug. > > Norbert > > > > > On 6. Jun 2014, at 0:16, Johannes Schindelin <[hidden email]> wrote: > > Hi Norbert, > > On Thu, 5 Jun 2014, Norbert Vischer wrote: > >> I get different boolean output for this expression: >> >> a = false && false || true && true; >> print(a); >> >> >> output JavaScript: true >> output Macro: 0 >> >> >> Documentation says: >> The ImageJ macro language supports almost all of the standard Java >> operators but with fewer precendence levels. >> But hopefully the above is not intended! > > You are asking an awful lot of a toy interpreter. Basically you are asking > to implement the macro language to implement the same priorities with > regards to Boolean operators as Javascript. To repeat: that is an awful > lot. > > It is true that Javascript lets && bind stronger than ||. There is no > logical reason to do so, just the tradition of the Unix shell where people > wanted to be able to write > > do_this && do_that && do_yet_another_thing || fail > > where fail would be executed whenever anything in that && chain before > that failed. > > But from a mathematical point of view, it makes no sense at all to give && > preference over || whatsoever, these operators are equivalent (a && b is > exactly the same as !(!a || !b)). > > Therefore, again, you are asking an awful lot of the poor little macro > language. Is it really that hard to add explicit parentheses? Or to switch > to a real language for that massive program of yours? > > Ciao, > Johannes > > Norbert Vischer > Research Engineer > Centre for Advanced Microscopy > Swammerdam Institute for Life Sciences (SILS) > University of Amsterdam > Science Park 904 > 1098 XH Amsterdam, the Netherlands > > -- > ImageJ mailing list: http://imagej.nih.gov/ij/list.html -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
Free forum by Nabble | Edit this page |