From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 8675 invoked by alias); 13 Jan 2015 16:00:52 -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: 34267 Received: (qmail 20378 invoked from network); 13 Jan 2015 16:00:38 -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=-1.9 required=5.0 tests=BAYES_00 autolearn=ham version=3.3.2 Date: Tue, 13 Jan 2015 17:00:31 +0100 From: Vincent Lefevre To: "zsh-workers@zsh.org" Subject: Re: Math expression evaluation error? Message-ID: <20150113160031.GA12079@ypig.lip.ens-lyon.fr> Mail-Followup-To: "zsh-workers@zsh.org" 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> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <1518701421079502@web12g.yandex.ru> X-Mailer-Info: http://www.vinc17.net/mutt/ User-Agent: Mutt/1.5.23-6392-vl-r75100 (2015-01-06) 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. > 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)