zsh-users
 help / color / mirror / code / Atom feed
* $[ 09.5 ]  -- bad math expression
@ 2012-12-02 10:30 Atom Smasher
  2012-12-02 21:05 ` Peter Stephenson
  2012-12-02 21:59 ` Bart Schaefer
  0 siblings, 2 replies; 4+ messages in thread
From: Atom Smasher @ 2012-12-02 10:30 UTC (permalink / raw)
  To: zsh-users

with zsh 4.2.1 i get what i'd expect here:

 	echo "$[ 09.5 ]"
 	9.5

and all math operations work as expected. with zsh 4.3.10, this is broken:

 	echo "$[ 09.5 ]"
 	zsh: bad math expression: operator expected at `.5 '

and all math operations using "09.5" result in errors. the problem seems 
to come up when using a non-integer with a leading zero.

is this a known bug? are there any workarounds?

thanks...


-- 
         ...atom

  ________________________
  http://atom.smasher.org/
  762A 3B98 A3C3 96C9 C6B7 582A B88D 52E4 D9F5 7808
  -------------------------------------------------

 	"Those who profess to favor freedom, and yet deprecate
 	 agitation, are men who want rain without thunder and
 	 lightning. They want the ocean without the roar of
 	 its many waters."
 		-- Frederick Douglass


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

* Re: $[ 09.5 ]  -- bad math expression
  2012-12-02 10:30 $[ 09.5 ] -- bad math expression Atom Smasher
@ 2012-12-02 21:05 ` Peter Stephenson
  2012-12-02 21:59 ` Bart Schaefer
  1 sibling, 0 replies; 4+ messages in thread
From: Peter Stephenson @ 2012-12-02 21:05 UTC (permalink / raw)
  To: zsh-users

On Sun, 2 Dec 2012 23:30:34 +1300 (NZDT)
Atom Smasher <atom@smasher.org> wrote:
> with zsh 4.2.1 i get what i'd expect here:
> 
>  	echo "$[ 09.5 ]"
>  	9.5
> 
> and all math operations work as expected. with zsh 4.3.10, this is broken:
> 
>  	echo "$[ 09.5 ]"
>  	zsh: bad math expression: operator expected at `.5 '

Oops.

OCTAL_ZEROES looks a bit broken, too.  Good job no one uses it.

Index: Src/math.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/math.c,v
retrieving revision 1.43
diff -p -u -r1.43 math.c
--- Src/math.c	11 Sep 2012 16:02:42 -0000	1.43
+++ Src/math.c	2 Dec 2012 21:02:25 -0000
@@ -476,14 +476,11 @@ lexconstant(void)
 		lastbase = 8;
 		return NUM;
 	    }
-	    nptr = ptr2;
 	}
     }
-    else
-    {
-	while (idigit(*nptr) || *nptr == '_')
-	    nptr++;
-    }
+
+    while (idigit(*nptr) || *nptr == '_')
+	nptr++;
 
     if (*nptr == '.' || *nptr == 'e' || *nptr == 'E') {
 	char *ptr2;
Index: Test/C01arith.ztst
===================================================================
RCS file: /cvsroot/zsh/zsh/Test/C01arith.ztst,v
retrieving revision 1.19
diff -p -u -r1.19 C01arith.ztst
--- Test/C01arith.ztst	11 Sep 2012 16:02:42 -0000	1.19
+++ Test/C01arith.ztst	2 Dec 2012 21:02:25 -0000
@@ -233,3 +233,8 @@
 >6000000
 >5000
 >255
+
+  # 3/4 is a multiple of power of two so assume it's represented exactly.
+  print ${$(( 09.75 * 4))%%.*}
+0:leading underscores
+>39


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

* Re: $[ 09.5 ]  -- bad math expression
  2012-12-02 10:30 $[ 09.5 ] -- bad math expression Atom Smasher
  2012-12-02 21:05 ` Peter Stephenson
@ 2012-12-02 21:59 ` Bart Schaefer
  2012-12-02 22:15   ` Atom Smasher
  1 sibling, 1 reply; 4+ messages in thread
From: Bart Schaefer @ 2012-12-02 21:59 UTC (permalink / raw)
  To: zsh-users

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


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

* Re: $[ 09.5 ]  -- bad math expression
  2012-12-02 21:59 ` Bart Schaefer
@ 2012-12-02 22:15   ` Atom Smasher
  0 siblings, 0 replies; 4+ messages in thread
From: Atom Smasher @ 2012-12-02 22:15 UTC (permalink / raw)
  To: zsh-users

On Sun, 2 Dec 2012, Bart Schaefer wrote:

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

thanks! i'll give a try with the workaround, and look forward to the patch 
being incorporated into new builds!

i guess i can also strip the leading zero(s) from any numbers where that 
could be an issue...


-- 
         ...atom

  ________________________
  http://atom.smasher.org/
  762A 3B98 A3C3 96C9 C6B7 582A B88D 52E4 D9F5 7808
  -------------------------------------------------

 	"We in the West must bear in mind that the poor countries
 	 are poor primarily because we have exploited them through
 	 political or economic colonialism."
 		-- Martin Luther King, Jr


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

end of thread, other threads:[~2012-12-02 23:37 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-12-02 10:30 $[ 09.5 ] -- bad math expression Atom Smasher
2012-12-02 21:05 ` Peter Stephenson
2012-12-02 21:59 ` Bart Schaefer
2012-12-02 22:15   ` Atom Smasher

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