Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

# Re: [bug] \$((0.5?1+1:3))

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

```

Messages sorted by: Reverse Date, Date, Thread, Author