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

Re: arithmetic operator precedence



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