Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: zmathfunc: min, max, sum throw error if result equals 0
- X-seq: zsh-workers 48607
- From: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>
- To: Daniel Shahaf <d.s@xxxxxxxxxxxxxxxxxx>
- Cc: Zsh hackers list <zsh-workers@xxxxxxx>
- Subject: Re: zmathfunc: min, max, sum throw error if result equals 0
- Date: Fri, 16 Apr 2021 13:05:13 -0700
- Archived-at: <https://zsh.org/workers/48607>
- In-reply-to: <20210416195028.GE15670@tarpaulin.shahaf.local2>
- List-id: <zsh-workers.zsh.org>
- References: <m2pn0aj4y2.fsf@zaclys.net> <20210307171712.GA9936@tarpaulin.shahaf.local2> <CAH+w=7bPz5u9-mQPbmPmbo=8Cz8em7JmbSn-sp8eZzuvFW3mnQ@mail.gmail.com> <ee7c107d-8bfb-4063-85e8-a7638ae0f951@www.fastmail.com> <CAH+w=7ZFbOJc16r6JX4Lr6uPdVRji_=RmY3WZVBK9GtXzxjPmA@mail.gmail.com> <20210416195028.GE15670@tarpaulin.shahaf.local2>
On Fri, Apr 16, 2021 at 12:51 PM Daniel Shahaf <d.s@xxxxxxxxxxxxxxxxxx> wrote:
>
> [...] the first one interpolates $arg as in double-quoted strings
> and then parses the string " x += 2 > result ", where the > operator has
> higher precedence, and «2 > result» evaluates to 1, and the «print» then
> evaluates everything left to right; whereas the latter sees «arg» and
> tries to evaluate that _as a number_, which works out to 2 (with a side
> effect), so the condition evaluates to true, and the assignment on the
> RHS then works out to 4 (with a side effect). Clear as a cloudless day
Given that, I'm puzzled why you did
> + (( $arg < result ))
> + case $? in
> + (0) result=$arg;;
> + (1) ;;
> + (*) return $?;;
> + esac
> done
> (( result ))
and why not
(( arg < result ))
case $? in
(0) (( result = arg ));;
(1) ;;
(*) return $?;;
esac
done
(( result ))
?? The (( $arg < result )) formulation still potentially has
different operator precedence in and out of math context, and
result=$arg followed by (( result )) still does repeat interpretation
of $arg. Why ever interpolate $arg as a string?
- References:
- zmathfunc: min, max, sum throw error if result equals 0
- Re: zmathfunc: min, max, sum throw error if result equals 0
- Re: zmathfunc: min, max, sum throw error if result equals 0
- Re: zmathfunc: min, max, sum throw error if result equals 0
- Re: zmathfunc: min, max, sum throw error if result equals 0
- Re: zmathfunc: min, max, sum throw error if result equals 0
Messages sorted by:
Reverse Date,
Date,
Thread,
Author