zsh-users
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: zsh-users@zsh.org
Subject: Re: $[ 09.5 ]  -- bad math expression
Date: Sun, 02 Dec 2012 13:59:40 -0800	[thread overview]
Message-ID: <121202135940.ZM19771@torch.brasslantern.com> (raw)
In-Reply-To: <alpine.DEB.2.00.1212022323450.27859@oreo.lan>

On Dec 2, 11:30pm, Atom Smasher wrote:
} 
}  	echo "$[ 09.5 ]"
}  	zsh: bad math expression: operator expected at `.5 '
} 
} the problem seems to come up when using a non-integer with a leading
} zero.

Hmm.  This appears to have changed back in about 2007, workers/23165.
Any number with a leading zero is interpreted as an integer, unless a
decimal point immediately follows the zero, in which case it's treated
as floating point.

} is this a known bug?

I'd certainly never heard of it before.  Looks like a logic error in
adding the "else" clause at the very end of the math.c hunk of 23165.

} are there any workarounds?

Counterintuitively, you can fix this by "setopt octalzeroes".  With
that option set, the entire constant is checked for whether it forms
a valid octal number, so then the presence of the decimal point forces
it to be interpreted as floating point.  However, that might have some
unintended consequences on the rest of your script ...

In the patch below I pulled the memchr from the original code that was
changed by 23165.  There may be a better way to do that test.

Index: Src/math.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/math.c,v
retrieving revision 1.43
diff -u -r1.43 math.c
--- Src/math.c  11 Sep 2012 16:02:42 -0000      1.43
+++ Src/math.c  2 Dec 2012 21:46:50 -0000
@@ -447,7 +447,8 @@
     if (*nptr == '-')
 	nptr++;
 
-    if (*nptr == '0')
+    if (*nptr == '0' &&
+	(memchr(nptr, '.', strlen(nptr)) == NULL))
     {
 	nptr++;
 	if (*nptr == 'x' || *nptr == 'X') {
Index: Test/C01arith.ztst
===================================================================
RCS file: /cvsroot/zsh/zsh/Test/C01arith.ztst,v
retrieving revision 1.19
diff -u -r1.19 C01arith.ztst
--- Test/C01arith.ztst  11 Sep 2012 16:02:42 -0000      1.19
+++ Test/C01arith.ztst  2 Dec 2012 21:57:29 -0000
@@ -152,6 +152,16 @@
 0:commas and parentheses, part 1
 >4
 
+  print $(( 07.5 ))
+  (setopt octalzeroes; print $(( 09.5 )))
+0:leading zero doesn't affect floating point
+>7.5
+>9.5
+
+  (setopt octalzeroes; print $(( 09 )))
+1:octalzeroes rejects invalid constants
+?(eval):1: bad math expression: operator expected at `9 '
+
   (setopt octalzeroes; print $(( 08#77 )))
 0:octalzeroes doesn't affect bases
 >63


  parent reply	other threads:[~2012-12-02 22:00 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-12-02 10:30 Atom Smasher
2012-12-02 21:05 ` Peter Stephenson
2012-12-02 21:59 ` Bart Schaefer [this message]
2012-12-02 22:15   ` Atom Smasher

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=121202135940.ZM19771@torch.brasslantern.com \
    --to=schaefer@brasslantern.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).