zsh-users
 help / color / mirror / code / Atom feed
From: Peter Stephenson <p.w.stephenson@ntlworld.com>
To: zsh-users@zsh.org
Subject: Re: force floating point arithmetics
Date: Tue, 5 Mar 2013 19:51:47 +0000	[thread overview]
Message-ID: <20130305195147.690e801b@pws-pc.ntlworld.com> (raw)
In-Reply-To: <20130304072902.GA9639@chaz.gmail.com>

On Mon, 4 Mar 2013 07:29:02 +0000
Stephane Chazelas <stephane.chazelas@gmail.com> 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 <p.w.stephenson@ntlworld.com>
Web page now at http://homepage.ntlworld.com/p.w.stephenson/


  reply	other threads:[~2013-03-05 19:58 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-03-04  7:29 Stephane Chazelas
2013-03-05 19:51 ` Peter Stephenson [this message]
2013-03-05 20:02   ` Peter Stephenson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20130305195147.690e801b@pws-pc.ntlworld.com \
    --to=p.w.stephenson@ntlworld.com \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).