Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: $[ 09.5 ] -- bad math expression
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
Messages sorted by:
Reverse Date,
Date,
Thread,
Author