From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 23384 invoked by alias); 5 Mar 2013 19:58:06 -0000 Mailing-List: contact zsh-users-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Users List List-Post: List-Help: X-Seq: 17665 Received: (qmail 1290 invoked from network); 5 Mar 2013 19:57:54 -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.6 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_LOW autolearn=ham version=3.3.2 Received-SPF: neutral (ns1.primenet.com.au: 74.125.82.41 is neither permitted nor denied by SPF record at ntlworld.com) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=x-received:x-proxyuser-ip:date:from:to:subject:message-id :in-reply-to:references:x-mailer:mime-version:content-type :content-transfer-encoding:x-gm-message-state; bh=V9ukwaDfI/Vkpc/BafHgXzi6Eh37yzAnDHqpbO7D3JU=; b=G1lTgkarAnrigN7q7ymCrY6Twd1cWs0g35aOOCnIROUwKPS0nDOeLR3LHKo91DbVla ujh0ujyioicNztTCAtIoEeU94ubGPJWqnyXYDCgow/TmFVhW9Zkd+xEKCjAq+s9Aoi07 9G4K7wMwC6EdfTaOJixh71d7M5R7kBk+j0KmErPdNsFmlD+4db0gPdweq9G7ZpYLR6e8 HR+6P4w14JCKvCh1reSJ+dngfBlZY921NNLMKvme3eEOTf0LCRnLGRFFS9M94Nk4QWPW JB8AfQH8Yo0ZMy6AcE2VF9g6UdNPwQVisScvIGEVfQMdsxr3F25dCoLXSqb5x79kK3aB 3WnQ== X-Received: by 10.194.121.6 with SMTP id lg6mr28904750wjb.22.1362513113793; Tue, 05 Mar 2013 11:51:53 -0800 (PST) X-ProxyUser-IP: 86.26.6.143 Date: Tue, 5 Mar 2013 19:51:47 +0000 From: Peter Stephenson To: zsh-users@zsh.org Subject: Re: force floating point arithmetics Message-ID: <20130305195147.690e801b@pws-pc.ntlworld.com> In-Reply-To: <20130304072902.GA9639@chaz.gmail.com> References: <20130304072902.GA9639@chaz.gmail.com> X-Mailer: Claws Mail 3.8.0 (GTK+ 2.24.7; x86_64-redhat-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Gm-Message-State: ALoCoQl8CsFbpKAYaxstAqYmLBBSxSlwdPe6jEe1EvptEeGAdOfLjsVDuId7oPs/rqzGPAwbh/1O On Mon, 4 Mar 2013 07:29:02 +0000 Stephane Chazelas wrote: > So, is there a way to force floating point arithmetics there > even when entering integer constants? I think you'd need something like this. Works quite well with zcalc, I should probably make it an option. Index: Doc/Zsh/options.yo =================================================================== RCS file: /cvsroot/zsh/zsh/Doc/Zsh/options.yo,v retrieving revision 1.108 diff -p -u -r1.108 options.yo --- Doc/Zsh/options.yo 15 Nov 2012 21:08:16 -0000 1.108 +++ Doc/Zsh/options.yo 5 Mar 2013 19:50:09 -0000 @@ -485,6 +485,17 @@ Treat the `tt(#)', `tt(~)' and `tt(^)' c for filename generation, etc. (An initial unquoted `tt(~)' always produces named directory expansion.) ) +pindex(FORCE_FLOAT) +pindex(NO_FORCE_FLOAT) +pindex(FORCEFLOAT) +pindex(NOFORCEFLOAT) +cindex(floating point, forcing use of) +cindex(forcing use of floating point) +item(tt(FORCE_FLOAT))( +Constants in arithmetic evaluation will be treated as floating point +even without the use of a decimal point. Integers in any base +will be converted. +) pindex(GLOB) pindex(NO_GLOB) pindex(NOGLOB) Index: Src/math.c =================================================================== RCS file: /cvsroot/zsh/zsh/Src/math.c,v retrieving revision 1.44 diff -p -u -r1.44 math.c --- Src/math.c 8 Dec 2012 19:50:34 -0000 1.44 +++ Src/math.c 5 Mar 2013 19:50:09 -0000 @@ -456,6 +456,11 @@ lexconstant(void) yyval.u.l = zstrtol_underscore(ptr, &ptr, 0, 1); /* Should we set lastbase here? */ lastbase = 16; + if (isset(FORCEFLOAT)) + { + yyval.type = MN_FLOAT; + yyval.u.d = (double)yyval.u.l; + } return NUM; } else if (isset(OCTALZEROES)) @@ -475,6 +480,11 @@ lexconstant(void) { yyval.u.l = zstrtol_underscore(ptr, &ptr, 0, 1); lastbase = 8; + if (isset(FORCEFLOAT)) + { + yyval.type = MN_FLOAT; + yyval.u.d = (double)yyval.u.l; + } return NUM; } nptr = ptr2; @@ -537,6 +547,11 @@ lexconstant(void) lastbase = yyval.u.l; yyval.u.l = zstrtol_underscore(ptr, &ptr, lastbase, 1); } + if (isset(FORCEFLOAT)) + { + yyval.type = MN_FLOAT; + yyval.u.d = (double)yyval.u.l; + } } return NUM; } Index: Src/options.c =================================================================== RCS file: /cvsroot/zsh/zsh/Src/options.c,v retrieving revision 1.66 diff -p -u -r1.66 options.c --- Src/options.c 15 Nov 2012 21:08:16 -0000 1.66 +++ Src/options.c 5 Mar 2013 19:50:10 -0000 @@ -131,6 +131,7 @@ static struct optname optns[] = { {{NULL, "extendedhistory", OPT_CSH}, EXTENDEDHISTORY}, {{NULL, "evallineno", OPT_EMULATE|OPT_ZSH}, EVALLINENO}, {{NULL, "flowcontrol", OPT_ALL}, FLOWCONTROL}, +{{NULL, "forcefloat", 0}, FORCEFLOAT}, {{NULL, "functionargzero", OPT_EMULATE|OPT_NONBOURNE},FUNCTIONARGZERO}, {{NULL, "glob", OPT_EMULATE|OPT_ALL}, GLOBOPT}, {{NULL, "globalexport", OPT_EMULATE|OPT_ZSH}, GLOBALEXPORT}, Index: Src/zsh.h =================================================================== RCS file: /cvsroot/zsh/zsh/Src/zsh.h,v retrieving revision 1.187 diff -p -u -r1.187 zsh.h --- Src/zsh.h 15 Nov 2012 21:08:16 -0000 1.187 +++ Src/zsh.h 5 Mar 2013 19:50:10 -0000 @@ -1988,6 +1988,7 @@ enum { EXTENDEDHISTORY, EVALLINENO, FLOWCONTROL, + FORCEFLOAT, FUNCTIONARGZERO, GLOBOPT, GLOBALEXPORT, Index: Test/C01arith.ztst =================================================================== RCS file: /cvsroot/zsh/zsh/Test/C01arith.ztst,v retrieving revision 1.20 diff -p -u -r1.20 C01arith.ztst --- Test/C01arith.ztst 8 Dec 2012 19:50:37 -0000 1.20 +++ Test/C01arith.ztst 5 Mar 2013 19:50:10 -0000 @@ -243,3 +243,18 @@ >6000000 >5000 >255 + + # Force floating point. + for expr in "3/4" "0x100/0x200" "0x30/0x10"; do + print $(( $expr )) + setopt force_float + print $(( $expr )) + unsetopt force_float + done +0:Forcing floating point constant evaluation, or not. +>0 +>0.75 +>0 +>0.5 +>3 +>3. -- Peter Stephenson Web page now at http://homepage.ntlworld.com/p.w.stephenson/