Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: arithmetic operator precedence



On Thu, Jun 19, 2008 at 06:37:17PM +0900, Jun T. wrote:
> 
> At 0:46 PM +0100 08.6.17, Peter Stephenson wrote:
> >As it looks like "some sort of"
> >compatibility might as well be with other shells (so the option could
> >really be called "SH_PRECEDENCES", although it's perhaps less obvious that
> >means "something approximating to the precedences you probably expect if
> >you are a regular programmer") this doesn't give us room for manoeuvre
> >without yet another option.
> 
> -3**2 = 9 is quite counter-intuitive and "mathematically wrong",
[...]

I don't find it counter-intuitive nor mathematically wrong as
there's no "**" operator of that shape in "maths".

But to rephrase what I was trying to say rather awkwardly
before.

With all the existing operators, one can do

x=$(( some-expression ))
y=$(( some-other-expression ))
z=$(( $x <op> $y ))

and it's OK whatever some-expression and some-other-expression
and <op> as long as they are POSIX.

The syntax above is the only one you can write in a POSIX
script, and POSIX guarantees it to work at the moment.

If you change ** so that it has  higher precedence than the
unary minus, you break that harmony.

Typically, that means you'll have to write:

z=$(( ($x) <op> ($y) ))

because you now have operators that have higher precedence than
the unary "-" and unary "+".

Again ** is not a POSIX operator, so it's not really a concern
at this time, as you can't use ** in a POSIX script anyway. But
that's to show how it could become one if POSIX was to include
"**" in a future version of the standard and set its precedence
as you suggest.

-- 
Stéphane



Messages sorted by: Reverse Date, Date, Thread, Author