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

Re: negative bases in arithmetic expressions



On Wed, 8 Aug 2018 16:37:06 +0100
Stephane Chazelas <stephane.chazelas@xxxxxxxxx> wrote:
> $ zsh -c 'echo $((-8#10))'
> zsh:1: invalid base (must be 2 to 36 inclusive): -8
> $ zsh -c 'echo $((- 8#10))'
> -8
> 
> I'd rather zsh interpreted $((-8#10)) the same as $((- 8#10))
> like other shells do as it's a bit pointless to consider the
> sign as being part of the base and then reject anything negative
> afterwise.

No real argument there, I don't think.

pws


diff --git a/Src/math.c b/Src/math.c
index 4b7ecf0..b08e05c 100644
--- a/Src/math.c
+++ b/Src/math.c
@@ -640,8 +640,19 @@ zzlex(void)
 	    }
 	    if (unary) {
 		if (idigit(*ptr) || *ptr == '.') {
-		    ptr--;
-		    return lexconstant();
+		    int ctype = lexconstant();
+		    if (ctype == NUM)
+		    {
+			if (yyval.type == MN_FLOAT)
+			{
+			    yyval.u.d = -yyval.u.d;
+			}
+			else
+			{
+			    yyval.u.l = -yyval.u.l;
+			}
+		    }
+		    return ctype;
 		} else
 		    return UMINUS;
 	    } else
diff --git a/Test/C01arith.ztst b/Test/C01arith.ztst
index 77a46eb..f1364ab 100644
--- a/Test/C01arith.ztst
+++ b/Test/C01arith.ztst
@@ -467,3 +467,7 @@
 >6
 >4
 ?(eval):6: bad math expression: lvalue required
+
+  print $(( -2#101-16#f ))
+0: Unary minus doesn't apply to base but to number as a whole.
+>-20



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