* [bug] $((0.5?1+1:3))
@ 2013-04-29 2:00 Stephane Chazelas
2013-04-29 4:15 ` Bart Schaefer
0 siblings, 1 reply; 2+ messages in thread
From: Stephane Chazelas @ 2013-04-29 2:00 UTC (permalink / raw)
To: Zsh Hackers' List
Hello,
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".
OK$ echo $((0.5?2:3))
2
NOK$ echo $((0.5?1+1:3))
0
OK$ echo $((1.1?2:3))
2
$ echo $ZSH_VERSION
5.0.2
(debian 7 amd64)
Work around is to write it $(( 0.5 != 0 ? 1+1 : 3 )) (since 0.5
!= 0 then has a value of 1 which falls outside that range I
suppose).
regards,
Stephane
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [bug] $((0.5?1+1:3))
2013-04-29 2:00 [bug] $((0.5?1+1:3)) Stephane Chazelas
@ 2013-04-29 4:15 ` Bart Schaefer
0 siblings, 0 replies; 2+ messages in thread
From: Bart Schaefer @ 2013-04-29 4:15 UTC (permalink / raw)
To: Stephane Chazelas, Zsh Hackers' List
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)
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2013-04-29 4:15 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-04-29 2:00 [bug] $((0.5?1+1:3)) Stephane Chazelas
2013-04-29 4:15 ` Bart Schaefer
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).