Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: arithmetic operator precedence
- X-seq: zsh-workers 25187
- From: Stephane Chazelas <Stephane_Chazelas@xxxxxxxx>
- To: Zsh hackers list <zsh-workers@xxxxxxxxxx>, Richard Hartmann <richih.mailinglist@xxxxxxxxx>, Peter Stephenson <p.w.stephenson@xxxxxxxxxxxx>
- Subject: Re: arithmetic operator precedence
- Date: Tue, 17 Jun 2008 13:46:07 +0100
- In-reply-to: <20080617123551.GJ10734@xxxxxxxxxxxxxxxxxxx>
- Mail-followup-to: Zsh hackers list <zsh-workers@xxxxxxxxxx>, Richard Hartmann <richih.mailinglist@xxxxxxxxx>, Peter Stephenson <p.w.stephenson@xxxxxxxxxxxx>
- Mailing-list: contact zsh-workers-help@xxxxxxxxxx; run by ezmlm
- References: <20080612095723.GF5113@xxxxxxxxxxxxxxx> <20080616080726.GP10734@xxxxxxxxxxxxxxxxxxx> <20080616144211.276fb0e3@pws-pc> <2d460de70806170219k12ff4cadn441b52c48bf8076f@xxxxxxxxxxxxxx> <20080617094509.GC5016@xxxxxxxxxxxxxxx> <20080617111934.GE10734@xxxxxxxxxxxxxxxxxxx> <20080617115742.GE5016@xxxxxxxxxxxxxxx> <20080617123551.GJ10734@xxxxxxxxxxxxxxxxxxx>
On Tue, Jun 17, 2008 at 02:35:51PM +0200, Vincent Lefevre wrote:
[...]
> But POSIX does not say that variable expansion should be performed
> before the arithmetic expression (at least in th way you think).
> If POSIX said so, then with a="1 + 1", $((a * 3)) would give
> $((1 + 1 * 3)) after variable expansion (without being able to make
> the difference with $((1 + 1 * 3)) written directly), then 5 after
> arithmetic expansion. But neither zsh nor bash behaves in that way.
[...]
$((a * 3)) is unspecified as per POSIX if $a doesn't contain an
integer constant. That was my point.
If $a contains an integer constant such as -3, then as per
POSIX, $((a * 3)) should be the same as $(($a * 3)), that is
$((-3 * 3)). If we extend that to the non-POSIX **, that would
be:
a=-3; $((a ** 2)) should be the same as $((-3 ** 2)).
So, if a future version of POSIX adds a **, it is bound to
specify it as $((-3 ** 2)) == 9 if it wants to keep consistant.
That would even be in accordance with all the existing shell
implementations that have **.
So, I think that with SH_PRECEDENCE, $((-3 ** 2)) should give 9.
--
Stéphane
Messages sorted by:
Reverse Date,
Date,
Thread,
Author