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

Re: arithmetic operator precedence



On 2008-06-17 12:57:42 +0100, Stephane Chazelas wrote:
> Which makes sense to me. I was serious when I said that -3 ** 2
> was more intuitive to me. Because -3 for me looks like a single
> number constant.

But it behaves in the same way as - 3, which doesn't look like a
single number constant. And in math, when writing -3^2, the -3 isn't
a single number constant; said otherwise, you need to take the whole
expression into account.

BTW, note that in C at least, -3 isn't a single number constant.
And this fact is visible in practice, e.g. on a machine with 32-bit
int's:

#include <stdio.h>
int main (void)
{
  printf ("%d %d\n",
          (int) sizeof(-2147483648),
          (int) sizeof(-2147483647));
  return 0;
}

outputs "8 4", though the value -2147483648 fits in an int (if
-2147483648 were regarded as a single number constant, one would
have got "4 4").

> BTW, I think I've got a rationale for -3 ** 2 == 9:
> 
> I think POSIX allows $((a * 3)) only as far as $a contains a
> constant. If it contains "1 + 1", you won't be guaranteed to
> have either 6 or 4.

But this rationale doesn't apply if the shell chooses to return 6
(which is IMHO the right extension, and what both bash and zsh do),
i.e. you can see $((a * 3)) as equivalent to $(((a) * 3)). Indeed,
with this choice, -3**2 == -9 is consistent with:

  $ a=-3
  $ echo $((a ** 2))
  9

> So, if you agree that -3 is an integer constant, and agree that
> as POSIX says variable expansion should be performed before the
> arithmetic expression is evaluated, so that x=3; echo $(($x *
> 2)) is the same as echo $((-3 ** 2)), that'd mean tha

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.

> > I think that Perl authors would say something like conventional math
> > writing (that's what some of authors of calculators say and what users
> > often demand).
> [...]
> 
> But I don't write 2**3 in conventional math writing,

But there is a direct equivalence (the only ambiguity is where the
part in exponent ends, and there's an rule in languages for that).

Let's take another example: "-3!" which is conventional math writing.
In math, this means -(3!), i.e. -6. As you can see, -3 isn't regarded
as a constant. And if a shell (or some other language) wants to
support the factorial, it should regard -3! as -(3!).

-- 
Vincent Lefèvre <vincent@xxxxxxxxxx> - Web: <http://www.vinc17.org/>
100% accessible validated (X)HTML - Blog: <http://www.vinc17.org/blog/>
Work: CR INRIA - computer arithmetic / Arenaire project (LIP, ENS-Lyon)



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