zsh-users
 help / color / mirror / code / Atom feed
* force floating point arithmetics
@ 2013-03-04  7:29 Stephane Chazelas
  2013-03-05 19:51 ` Peter Stephenson
  0 siblings, 1 reply; 3+ messages in thread
From: Stephane Chazelas @ 2013-03-04  7:29 UTC (permalink / raw)
  To: zsh-users; +Cc: embe8573

Hiya,

this is a follow up on
http://unix.stackexchange.com/a/66755/22565

I was trying to find a way for $((...)) to always do floating
point arithmetics as in $((1/3)) not returning 0, and the best I
could come up with was appending . to any sequence of decimal
digits that is not otherwise part of a hex number (or number in
another base) or of a variable name or 12e-20 type numbers like:

setopt extendedglob
calc() printf '%.6g\n' $((${*//(#bm)(([0-9.]##[eE][-+][0-9]##|[[:alnum:]_#]#[.#_[:alpha:]][[:alnum:]_#]#)|([0-9]##))/$MATCH${match[3]:+.}}))
alias 'calc=noglob calc'

calc 1/3

...

Like the OP there, I'm surprised there's no easier way to do it,
but maybe I've not looked hard enough.

So, is there a way to force floating point arithmetics there
even when entering integer constants?

Thanks,
Stephane


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: force floating point arithmetics
  2013-03-04  7:29 force floating point arithmetics Stephane Chazelas
@ 2013-03-05 19:51 ` Peter Stephenson
  2013-03-05 20:02   ` Peter Stephenson
  0 siblings, 1 reply; 3+ messages in thread
From: Peter Stephenson @ 2013-03-05 19:51 UTC (permalink / raw)
  To: zsh-users

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/


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: force floating point arithmetics
  2013-03-05 19:51 ` Peter Stephenson
@ 2013-03-05 20:02   ` Peter Stephenson
  0 siblings, 0 replies; 3+ messages in thread
From: Peter Stephenson @ 2013-03-05 20:02 UTC (permalink / raw)
  To: zsh-users

On Tue, 5 Mar 2013 19:51:47 +0000
Peter Stephenson <p.w.stephenson@ntlworld.com> wrote:
> Works quite well with zcalc, I should probably make it an option.

Index: Doc/Zsh/contrib.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/contrib.yo,v
retrieving revision 1.147
diff -p -u -r1.147 contrib.yo
--- Doc/Zsh/contrib.yo	26 Jan 2013 22:54:01 -0000	1.147
+++ Doc/Zsh/contrib.yo	5 Mar 2013 20:00:42 -0000
@@ -3158,6 +3158,10 @@ The output base can be initialised by pa
 for example `tt(zcalc -#16)' (the `tt(#)' may have to be quoted, depending
 on the globbing options set).
 
+If the option `tt(-f)' is set, all numbers are treated as floating
+point, hence for example the expression `tt(3/4)' evaluates to 0.75
+rather than 0.  Options must appear in separate words.
+
 The prompt is configurable via the parameter tt(ZCALCPROMPT), which
 undergoes standard prompt expansion.  The index of the current entry is
 stored locally in the first element of the array tt(psvar), which can be
Index: Functions/Misc/zcalc
===================================================================
RCS file: /cvsroot/zsh/zsh/Functions/Misc/zcalc,v
retrieving revision 1.20
diff -p -u -r1.20 zcalc
--- Functions/Misc/zcalc	28 Jul 2010 14:01:12 -0000	1.20
+++ Functions/Misc/zcalc	5 Mar 2013 20:00:42 -0000
@@ -114,7 +114,7 @@ float PI E
 (( PI = 4 * atan(1), E = exp(1) ))
 
 # Process command line
-while [[ -n $1 && $1 = -(|[#-]*) ]]; do
+while [[ -n $1 && $1 = -(|[#-]*|f) ]]; do
   optlist=${1[2,-1]}
   shift
   [[ $optlist = (|-) ]] && break
@@ -139,6 +139,9 @@ while [[ -n $1 && $1 = -(|[#-]*) ]]; do
 	    fi
             defbase="[#${arg}]"
 	    ;;
+	(f) # Force floating point operation
+	    setopt forcefloat
+	    ;;
     esac
   done
 done

-- 
Peter Stephenson <p.w.stephenson@ntlworld.com>
Web page now at http://homepage.ntlworld.com/p.w.stephenson/


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2013-03-05 20:02 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-03-04  7:29 force floating point arithmetics Stephane Chazelas
2013-03-05 19:51 ` Peter Stephenson
2013-03-05 20:02   ` Peter Stephenson

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).