From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 14070 invoked by alias); 29 Sep 2015 05:06:45 -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: 36693 Received: (qmail 1264 invoked from network); 29 Sep 2015 05:06:44 -0000 X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM, T_DKIM_INVALID,UNPARSEABLE_RELAY autolearn=ham autolearn_force=no version=3.4.0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=date:from:to:subject:message-id:mime-version:content-type :content-disposition:user-agent; bh=9URKOGI19i0Un4f4U69mT4D73eVc0f+jc2BS09ZbEUE=; b=OgpLxggUnX5P6yU+I8pXmNGRcA8uuLrdyhn+xPVpHffL2e1HJMnkV7MASHXIMRlsiQ KD6/xQcB1/K5vdoBTInk7fjTTcJHdnIVAi7AtoX1CXCMCqKk8NhMdzPUQ0BWR5PWJz0m jm42EysKF53fdPN9QnMgXqsoWixwmbGaEDw98paTjuMJuMlsoptihq7E4iC0A5c0qbrt CcDaYcD4iwY0kq8C9hJ3dTlgdFjIBIc+UfByq2Ippgw1w23QA5GV/3Fr7eXOkYkU1VZp UhVz6qcP9G4OEEzaVk781Bl4f01UVmQEleqvu/55Q0M8hAX5kmHzx12IE11ploo6l+Dw OTMg== X-Received: by 10.68.195.3 with SMTP id ia3mr30149012pbc.106.1443503201211; Mon, 28 Sep 2015 22:06:41 -0700 (PDT) Date: Tue, 29 Sep 2015 00:06:39 -0500 From: Matthew Martin To: zsh-workers@zsh.org Subject: [patch] Round to zero not down Message-ID: <20150929050639.GA29354@CptOrmolo.darkstar> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.5.24 (2015-08-30) The docs claim that various operations round down; however, % a=-5;((b=a/2.0));echo $b -2 % echo $((~ -5/2.0)) 2 # Actually rounds down % echo $(((-5/2.0) & -1)) -2 % echo $(((-5/2.0) | 0)) -2 % echo $(((-5/2.0) ^ 0)) -2 % echo $(((-5/2.0) % 0)) -2.5 # Doesn't round at all % echo $(((-5/2.0) >> 1)) -1 # The -2.5 is rounded to -2 % echo $(((-6/2.0) >> 1)) -2 # But -1,5 (the result of the shift) is rounded down % echo $(((-5/2.0) << 1)) -4 % echo $((-6/8)) 0 The diff below changes the docs to reflect this behavior. diff --git a/Doc/Zsh/arith.yo b/Doc/Zsh/arith.yo index 1dcd18c..17f466f 100644 --- a/Doc/Zsh/arith.yo +++ b/Doc/Zsh/arith.yo @@ -215,7 +215,7 @@ findex(integer, use of) Arithmetic evaluation is performed on the value of each assignment to a named parameter declared integer in this manner. Assigning a floating point number to an integer results in -rounding down to the next integer. +rounding towards zero. cindex(parameters, floating point) cindex(floating point parameters) @@ -230,16 +230,16 @@ format. Promotion of integer to floating point values is performed where necessary. In addition, if any operator which requires an integer -(`tt(~)', `tt(&)', `tt(|)', `tt(^)', `tt(%)', `tt(<<)', `tt(>>)' and their -equivalents with assignment) is given a floating point argument, it will be -silently rounded down to the next integer. +(`tt(&)', `tt(|)', `tt(^)', `tt(<<)', `tt(>>)' and their equivalents with +assignment) is given a floating point argument, it will be silently rounded +towards zero except for `tt(~)' which rounds down. Users should beware that, in common with many other programming languages but not software designed for calculation, the evaluation of an expression in zsh is taken a term at a time and promotion of integers to floating point does not occur in terms only containing integers. A typical result of this is that a division such as tt(6/8) is truncated, -in this being rounded down to 0. The tt(FORCE_FLOAT) shell option can +in this being rounded towards 0. The tt(FORCE_FLOAT) shell option can be used in scripts or functions where floating point evaluation is required throughout.