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

Re: zmathfunc: min, max, sum throw error if result equals 0



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?




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