Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: force floating point arithmetics



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



Messages sorted by: Reverse Date, Date, Thread, Author