zsh-workers
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: Stephane Chazelas <stephane.chazelas@gmail.com>,
	"Zsh Hackers' List" <zsh-workers@zsh.org>
Subject: Re: [bug] $((0.5?1+1:3))
Date: Sun, 28 Apr 2013 21:15:42 -0700	[thread overview]
Message-ID: <130428211542.ZM14735@torch.brasslantern.com> (raw)
In-Reply-To: <20130429020002.GA7626@chaz.gmail.com>

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 ? <some-expression> : 42 )) where
} <some-expression> 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)


      reply	other threads:[~2013-04-29  4:15 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-04-29  2:00 Stephane Chazelas
2013-04-29  4:15 ` Bart Schaefer [this message]

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=130428211542.ZM14735@torch.brasslantern.com \
    --to=schaefer@brasslantern.com \
    --cc=stephane.chazelas@gmail.com \
    --cc=zsh-workers@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).