From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 4565 invoked by alias); 29 Apr 2013 04:15:50 -0000 Mailing-List: contact zsh-workers-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Workers List List-Post: List-Help: X-Seq: 31353 Received: (qmail 6131 invoked from network); 29 Apr 2013 04:15:48 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.2 Received-SPF: none (ns1.primenet.com.au: domain at closedmail.com does not designate permitted sender hosts) From: Bart Schaefer Message-id: <130428211542.ZM14735@torch.brasslantern.com> Date: Sun, 28 Apr 2013 21:15:42 -0700 In-reply-to: <20130429020002.GA7626@chaz.gmail.com> Comments: In reply to Stephane Chazelas "[bug] $((0.5?1+1:3))" (Apr 29, 3:00am) References: <20130429020002.GA7626@chaz.gmail.com> X-Mailer: OpenZMail Classic (0.9.2 24April2005) To: Stephane Chazelas , "Zsh Hackers' List" Subject: Re: [bug] $((0.5?1+1:3)) MIME-version: 1.0 Content-type: text/plain; charset=us-ascii On Apr 29, 3:00am, Stephane Chazelas wrote: } } That's quite an odd one. For any value of x between -1 and 1 } (-1, 1 and 0 excluded), $(( x ? : 42 )) where } is anything but a numeric constant, expands to } "0". Hrm. In math.c around line 1398 (varies a lot by version of zsh) we have the "case QUEST:" that handles ternary expressions. There we find this: q = (stack[sp].val.type == MN_FLOAT) ? (zlong)stack[sp].val.u.d : stack[sp].val.u.l; Note this is rouding q down to an integer, so fractions less than one are false. This is followed by: if (!q) noeval++; mathparse(prec[COLON] - 1); if (!q) noeval--; So the expression is consumed without parsing it. This "works" for contatants by accident. Later when parsing is complete and the correct non-zero-ness of the fraction is determined, the value of the unparsed expression is substituted, which is almost always zero. This should do it: diff --git a/Src/math.c b/Src/math.c index a2462a3..5bd4b90 100644 --- a/Src/math.c +++ b/Src/math.c @@ -1459,7 +1459,8 @@ mathparse(int pc) case QUEST: if (stack[sp].val.type == MN_UNSET) stack[sp].val = getmathparam(stack + sp); - q = (stack[sp].val.type == MN_FLOAT) ? (zlong)stack[sp].val.u.d : + q = (stack[sp].val.type == MN_FLOAT) ? + (stack[sp].val.u.d == 0 ? 0 : 1) : stack[sp].val.u.l; if (!q)