From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 4208 invoked from network); 16 Jun 2008 13:59:56 -0000 X-Spam-Checker-Version: SpamAssassin 3.2.4 (2008-01-01) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=AWL,BAYES_00 autolearn=ham version=3.2.4 Received: from news.dotsrc.org (HELO a.mx.sunsite.dk) (130.225.247.88) by ns1.primenet.com.au with SMTP; 16 Jun 2008 13:59:56 -0000 Received-SPF: none (ns1.primenet.com.au: domain at sunsite.dk does not designate permitted sender hosts) Received: (qmail 96060 invoked from network); 16 Jun 2008 13:59:53 -0000 Received: from sunsite.dk (130.225.247.90) by a.mx.sunsite.dk with SMTP; 16 Jun 2008 13:59:53 -0000 Received: (qmail 13180 invoked by alias); 16 Jun 2008 13:59:50 -0000 Mailing-List: contact zsh-workers-help@sunsite.dk; run by ezmlm Precedence: bulk X-No-Archive: yes X-Seq: 25161 Received: (qmail 13170 invoked from network); 16 Jun 2008 13:59:50 -0000 Received: from bifrost.dotsrc.org (130.225.254.106) by sunsite.dk with SMTP; 16 Jun 2008 13:59:50 -0000 Received: from mail.o2.co.uk (jabba.london.02.net [82.132.130.169]) by bifrost.dotsrc.org (Postfix) with ESMTP id 519E08028AC3 for ; Mon, 16 Jun 2008 15:59:44 +0200 (CEST) Received: from sc.homeunix.net (78.105.216.138) by mail.o2.co.uk (8.0.013.3) (authenticated as stephane.chazelas) id 4851DD95009B2AA5; Mon, 16 Jun 2008 14:59:34 +0100 Received: from chazelas by sc.homeunix.net with local (Exim 4.69) (envelope-from ) id 1K8FF8-0002UG-3u; Mon, 16 Jun 2008 14:59:34 +0100 Date: Mon, 16 Jun 2008 14:59:34 +0100 From: Stephane Chazelas To: Peter Stephenson Cc: Zsh hackers list Subject: Re: arithmetic operator precedence Message-ID: <20080616135934.GD5091@sc.homeunix.net> Mail-Followup-To: Peter Stephenson , Zsh hackers list References: <20080612095723.GF5113@sc.homeunix.net> <20080616080726.GP10734@prunille.vinc17.org> <20080616144211.276fb0e3@pws-pc> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-15 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20080616144211.276fb0e3@pws-pc> User-Agent: Mutt/1.5.16 (2007-09-19) X-Virus-Scanned: ClamAV 0.92.1/7488/Mon Jun 16 08:06:58 2008 on bifrost X-Virus-Status: Clean On Mon, Jun 16, 2008 at 02:42:11PM +0100, Peter Stephenson wrote: > On Mon, 16 Jun 2008 10:07:26 +0200 > Vincent Lefevre wrote: > > Speaking of precedence, the following one is nasty: > > > > vin% zsh -c 'echo $((-3**2))' > > 9 > > vin% bash -c 'echo $((-3**2))' > > 9 > > vin% ksh93 -c 'echo $((-3**2))' > > 9 > > > > IMHO these shells should be fixed to give -9, i.e. ** should have > > the precedence over the unary -, like conventional math writing. > > That's an interesting point for C_PRECEDENCES since I was trying to get > it behave as much as possible like Perl. What does anyone else think? [...] It's at least consistent with other shells and bc at the moment: ~$ bash -c 'echo $((-2**2))' 4 ~$ zsh -c 'echo $((-2**2))' 4 ~$ ksh93 -c 'echo $((-2**2))' 4 ~$ echo '-2 ^ 2' | bc 4 bc is one place where POSIX specifies the precedence of unary-minus vs power: http://www.opengroup.org/onlinepubs/009695399/utilities/bc.html minus has higher precedence than ^ which I have to say is more intuitive to me. Having said that: ~$ perl -le 'print -2 ** 2' -4 ~$ python -c 'print -2 ** 2' -4 ~$ ruby -e 'print -2 ** 2' -4% ~$ gawk 'BEGIN {print -2 ** 2}' -4 TCL has pow(x, y) instead of an operator. -- Stéphane