From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 10307 invoked by alias); 13 Jan 2015 23:15:50 -0000 Mailing-List: contact zsh-workers-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Workers List List-Post: List-Help: X-Seq: 34272 Received: (qmail 24980 invoked from network); 13 Jan 2015 23:15:46 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-2.7 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,RCVD_IN_DNSWL_LOW,T_FSL_HELO_BARE_IP_2 autolearn=ham version=3.3.2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex.ru; s=mail; t=1421190215; bh=q7R3mCQHA8QghWL209DzY1KhN8XhSc3OyqfWwHbut+I=; h=From:To:In-Reply-To:References:Subject:Date; b=Ztao8xxT3KLeRJdlRTYbz4mchkj4JtAuTfRXD/PGtIIAzdAyvYH7rXKbN4wiLY7/e 4emlHay9v+Bs/99JS3TR1PzRQwAIQJ9+CctH2LAbEDyFAevWjxewb1+tsZgys7xsG9 NnhszmkGEJyJAssmbNEb3HcswAu1KPP/dNcs5ToQ= From: ZyX To: Vincent Lefevre , "zsh-workers@zsh.org" In-Reply-To: <20150113160031.GA12079@ypig.lip.ens-lyon.fr> References: <54B0D451.1000502@eastlink.ca> <150110085320.ZM28012@torch.brasslantern.com> <54B173ED.20301@eastlink.ca> <150110111445.ZM21328@torch.brasslantern.com> <54B1A5EC.2080202@eastlink.ca> <150110175103.ZM21764@torch.brasslantern.com> <54B205B8.4080208@eastlink.ca> <20150112091737.GB27773@xvii.vinc17.org> <1518701421079502@web12g.yandex.ru> <20150113160031.GA12079@ypig.lip.ens-lyon.fr> Subject: Re: Math expression evaluation error? MIME-Version: 1.0 Message-Id: <2295541421190213@web9j.yandex.ru> X-Mailer: Yamail [ http://yandex.ru ] 5.0 Date: Wed, 14 Jan 2015 02:03:33 +0300 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset=utf-8 13.01.2015, 19:01, "Vincent Lefevre" : > On 2015-01-12 19:18:22 +0300, ZyX wrote: >>  12.01.2015, 12:18, "Vincent Lefevre" : >>>  It's only a notation problem. Unfortunately / was chosen (historically, >>>  not by zsh) as the symbol for integer division. The shell expression >>>  1/2 + 1/2 just means: >>> >>>    integer_div(1,2) + integer_div(1,2) >>> >>>  where integer_div is the integer division function, which gives 0. >>>  This is perfectly correct math. >>  It is not integer division. > > In POSIX, it is always an integer division. What?! 1. How POSIX is related? Zsh is not a POSIX shell and it is not emulation mode that is being discussed here. 2. If this standard is correct: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap01.html#tagtcjh_15 then it references ISO C standard. The ISO C standard (C99) says the following for multiplicative operators: > 6.5.5 Multiplicative operators … > 3 The usual arithmetic conversions are performed on the operands. > 2 Each of the operands shall have arithmetic type. The operands of the % operator shall > have integer type. … > 5 The result of the / operator is the quotient from the division of the first operand by the > second; the result of the % operator is the remainder. In both operations, if the value of > the second operand is zero, the behavior is undefined. > 6 When integers are divided … Where do you see a requirement for `/` to be integer division? 3. POSIX (http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_04) says that “Only signed long integer arithmetic is required.” And the section referenced there talks about signed long or double. Signed long integer arithmetic is *required* to be in there, but signed long integer arithmetics is *not required* to be the *only* arithmetics present in POSIX shell. So no, `/` has nothing to do with integer division, except that the referenced C standard expects well-defined integer division in case two integers are divided. If you have another POSIX standard or another interpretation please show it here. >>  1.0/2 and 1/2.0 will give the same result: 0.5. > > because 1.0 and 2.0 don't have an integer type. Therefore the result is > unspecified by POSIX, and zsh chooses to do a floating-point division. >>  If you want to see language with operator for integer division check >>  out Python-3: it has / for always-floating-point-unless-overridden >>  and // for always-integral-unless-overridden > > For Python, but not for zsh, unfortunately. But this choice comes > from POSIX historical behavior (and perhaps earlier). >>  I would rather call it “type-specific division”. > > In zsh. In POSIX, it is *always* an integer division. There is no > floating-point there. > > An alternative choice zsh could have done is to return an error > if the / operator is used in a floating-point context, and use a > different operator for the floating-point division. However the > use of / for integer division could still be surprising. > > -- > Vincent Lefèvre - Web: > 100% accessible validated (X)HTML - Blog: > Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)