Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: arithmetic operator precedence
- X-seq: zsh-workers 25213
- From: Stephane Chazelas <Stephane_Chazelas@xxxxxxxx>
- To: "Jun T." <takimoto-j@xxxxxxxxxxxxxxxxx>
- Subject: Re: arithmetic operator precedence
- Date: Thu, 19 Jun 2008 10:54:54 +0100
- Cc: zsh-workers@xxxxxxxxxx
- In-reply-to: <a0600102dc47fd2c6184e@xxxxxxxxxxxxxxxxx>
- Mail-followup-to: "Jun T." <takimoto-j@xxxxxxxxxxxxxxxxx>, zsh-workers@xxxxxxxxxx
- Mailing-list: contact zsh-workers-help@xxxxxxxxxx; run by ezmlm
- References: <20080616080726.GP10734@xxxxxxxxxxxxxxxxxxx> <20080616144211.276fb0e3@pws-pc> <2d460de70806170219k12ff4cadn441b52c48bf8076f@xxxxxxxxxxxxxx> <20080617094509.GC5016@xxxxxxxxxxxxxxx> <2d460de70806170324o5a44609x9383cc2445d67dd6@xxxxxxxxxxxxxx> <20080617103829.GD5016@xxxxxxxxxxxxxxx> <20080617114340.398c731f@news01> <20080617112815.GF10734@xxxxxxxxxxxxxxxxxxx> <200806171146.m5HBkhfR013230@xxxxxxxxxxxxxx> <a0600102dc47fd2c6184e@xxxxxxxxxxxxxxxxx>
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