zsh-workers
 help / color / mirror / code / Atom feed
* negative bases in arithmetic expressions
@ 2018-08-08 15:37 ` Stephane Chazelas
  2018-08-08 16:05   ` Peter Stephenson
  0 siblings, 1 reply; 2+ messages in thread
From: Stephane Chazelas @ 2018-08-08 15:37 UTC (permalink / raw)
  To: Zsh hackers list

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

-- 
Stephane


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

* Re: negative bases in arithmetic expressions
  2018-08-08 15:37 ` negative bases in arithmetic expressions Stephane Chazelas
@ 2018-08-08 16:05   ` Peter Stephenson
  0 siblings, 0 replies; 2+ messages in thread
From: Peter Stephenson @ 2018-08-08 16:05 UTC (permalink / raw)
  To: Zsh hackers list

On Wed, 8 Aug 2018 16:37:06 +0100
Stephane Chazelas <stephane.chazelas@gmail.com> 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


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

end of thread, other threads:[~2018-08-08 16:05 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <CGME20180808153741epcas3p346a9ba58cddb7bd5d5e91aaf3a63d51c@epcas3p3.samsung.com>
2018-08-08 15:37 ` negative bases in arithmetic expressions Stephane Chazelas
2018-08-08 16:05   ` 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).