Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: arithmetic operator precedence
- X-seq: zsh-workers 25191
- 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 14:20:39 +0100
- In-reply-to: <20080617130246.GL10734@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> <20080617124607.GH5016@xxxxxxxxxxxxxxx> <20080617130246.GL10734@xxxxxxxxxxxxxxxxxxx>
On Tue, Jun 17, 2008 at 03:02:46PM +0200, Vincent Lefevre wrote:
[...]
> It is unspecified. So, the shell is right to choose how it sees it.
>
> > 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)).
>
> No, POSIX does not say that. It happens to be the same thing here
> just because of the properties of *, but you can't deduce anything
> for extensions.
It says $((x)) is meant to be the same as $(($x)) which I
understand as any occurrance of a variable name (other than $-,
$?, $0... obviously) in $((...)) should be the same as if the $
was not ommited (when $x contains an integer constant).
That's a recent addition to the text. Only recent versions of
ash (BSD shs) support that for instance, which is why it has
been recommanded for a while to write it $(($x + 1)) instead of
$((x + 1)) in POSIX scripts. And I still use x=$(($x + 1)).
For newer ashes, $((x + 1)) is now accepted and note that you do
get an error in ash if $x is not an integer constant.
> And in practice, shells don't treat $((a * 3)) and
> $(($a * 3)) in the same way:
>
> vin:~> a="1 + 1"
> vin:~> echo $((a * 3))
> 6
> vin:~> echo $(($a * 3))
> 4
But here, $a doesn't contain an integer constant, that's out of
the scope of POSIX.
>
> > If we extend that to the non-POSIX **, that would be:
> >
> > a=-3; $((a ** 2)) should be the same as $((-3 ** 2)).
>
> No, if we extend POSIX in an intuitive way (see above), $((a ** 2))
> should be the same as $(((-3) ** 2)), hence 9. So, no problem with
> $((-3**2)) being -9.
[...]
No, POSIX does say that $((a ** 2)) is the same as $(($a ** 2))
because $a contains an integer constant, and that's $((-3**2)).
--
Stéphane
Messages sorted by:
Reverse Date,
Date,
Thread,
Author