From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.4 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,UNPARSEABLE_RELAY autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 2872 invoked from network); 16 Apr 2021 19:50:47 -0000 Received: from zero.zsh.org (2a02:898:31:0:48:4558:7a:7368) by inbox.vuxu.org with ESMTPUTF8; 16 Apr 2021 19:50:47 -0000 ARC-Seal: i=1; cv=none; a=rsa-sha256; d=zsh.org; s=rsa-20200801; t=1618602647; b=w2eo6RUEoC2nCNC8b2BKR9H8xju/pdjlQlpTSJ6tNtJhhsX3PqvzsyRLTgBHOJj3QSQGC15bfh 1uXLd/pOxsPNYCeB8qaA/2sFhscoLI3skX9d/pIgWjj4EMcMT1njtfdonyQ0j72KQukVrsWNr4 PaQnUTvk52gcu5FODUYt9nOuO+RaPRgcD2tpASMmrrMMpujSCrdvyuqYQXRAerfzFp9ZhOhuPV QkTzkqr0JQuvnpybAHuXjI5cNntM8duVx1uQbnryMwFgNV1rxXqPAfKYxdsTmCc0+i3eXMbZiG 0f6gEgu8pNho9NGy1M9AELUmShXkx/y2IwPA9Y8nf8VLsQ==; ARC-Authentication-Results: i=1; zsh.org; iprev=pass (wout3-smtp.messagingengine.com) smtp.remote-ip=64.147.123.19; dkim=pass header.d=daniel.shahaf.name header.s=fm3 header.a=rsa-sha256; dkim=pass header.d=messagingengine.com header.s=fm2 header.a=rsa-sha256; dmarc=none header.from=daniel.shahaf.name; arc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed; d=zsh.org; s=rsa-20200801; t=1618602647; bh=QDYzYXMedtHwN03AiQFffOPfizLBmr7IveJlwTemMU8=; h=List-Archive:List-Owner:List-Post:List-Unsubscribe:List-Subscribe:List-Help: List-Id:Sender:In-Reply-To:Content-Transfer-Encoding:Content-Type: MIME-Version:References:Message-ID:Subject:To:From:Date:DKIM-Signature: DKIM-Signature:DKIM-Signature; b=Cx5tMe6hT6JYY1xXIf7NTvwVxNh/FQ6uVuPdr3evDxBLgqsjMkr0cAFg63VyAaxnnRNsGtecAi enOTd7iXW2vSTPk9fQoFEIWgv7UTxGZivqMMiWlvKomih3fTmcXtwi2CSeYhUz/GvaafR+P1Ko L3BDdH6/hbSNmwhhUepZ4W/At6xfNL1qy9CxAPwz39w7P0VXF6m9oGY5BpkNrMI5R4vv9BIgKu 8IhNFSuARnYueLY6xIf91I2Db0YOi/6nn2kJ3OuCjQgqIMC42gahQYYkywk+D1Tn0rXYpTh1E3 toRuSKLakOrTTVKj+zO3xuHBUimI5qthU17YbGCVdhYAQg==; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=zsh.org; s=rsa-20200801; h=List-Archive:List-Owner:List-Post:List-Unsubscribe: List-Subscribe:List-Help:List-Id:Sender:In-Reply-To:Content-Transfer-Encoding :Content-Type:MIME-Version:References:Message-ID:Subject:To:From:Date: Reply-To:Cc:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID; bh=bgPwJZ2ezm2cwQsjEAZOUCVnPm+07VllSHW2Us14qIc=; b=zFdA9vUxXE0CnvrKS3HwsMb+8u yHLn3m6sB2kV3CtwGE01fJCgasVNeuY5W0cl3ntltLcGxeARa8O3O0SYDd5AiL0MYrSGew/vGB9W/ LqwUjzJ+VFw39AM4FEjfy/vttNU4qNscKYX0BQNyoDnFoo4VKtc2k6sr+QKdB9z/8oMlkqNvqgVZq SYchPk+tAkAT6FJsKA5+xj9NVwXbkR5FeLbAtZLH1m06leIe49HlW1pI/89ZWRfS90Njrbjs7ZcDQ RUVK+pOz3UqT4zFBOfJUyk0O5VpJhFU5Sdm6dg8r2bp9nz9ymU0yc/jpp08ZO2MQhO+ntdVbc6o7+ VZ8udPgg==; Received: from authenticated user by zero.zsh.org with local id 1lXUUI-000IYE-CZ; Fri, 16 Apr 2021 19:50:46 +0000 Authentication-Results: zsh.org; iprev=pass (wout3-smtp.messagingengine.com) smtp.remote-ip=64.147.123.19; dkim=pass header.d=daniel.shahaf.name header.s=fm3 header.a=rsa-sha256; dkim=pass header.d=messagingengine.com header.s=fm2 header.a=rsa-sha256; dmarc=none header.from=daniel.shahaf.name; arc=none Received: from wout3-smtp.messagingengine.com ([64.147.123.19]:56241) by zero.zsh.org with esmtps (TLS1.3:TLS_AES_256_GCM_SHA384:256) id 1lXUU5-000IIk-C5; Fri, 16 Apr 2021 19:50:34 +0000 Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.west.internal (Postfix) with ESMTP id CB75613CD; Fri, 16 Apr 2021 15:50:31 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute3.internal (MEProxy); Fri, 16 Apr 2021 15:50:31 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= daniel.shahaf.name; h=date:from:to:subject:message-id:references :mime-version:content-type:content-transfer-encoding :in-reply-to; s=fm3; bh=bgPwJZ2ezm2cwQsjEAZOUCVnPm+07VllSHW2Us14 qIc=; b=RtmTXrSNvdU5/pSBt8NcP0VqcTZa9fdH//lPRZmL1sd+e2TUXZh8CURG 7+4otfJXf/MBIKRrFXh23gnol1FCZEDKH00DSjfVojxjXNik8FknxoNbZ7sKVNH/ u9TyNInAIcx7FMRzyzsJHPdCtIrOjPKqkUvz0wPTczIT+rOktXxOKZgveOKnIobB nGm4uDYaB0jLZjhaQhysx2CIJ4EmLeSXjZL27nu0MCKnPcYGP3eCuUhocas4qrQ6 NmfUSgTr3TW1A0M97egaZKcEiEaPvP64mwIv8I0+ASmuUjo7HWMBWB5c9B1Izhgt BxPAKDIgLILLdbTb6tIapoRfJwosxw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm2; bh=bgPwJZ2ezm2cwQsjEAZOUCVnPm+07VllSHW2Us14q Ic=; b=PLZHpop57jb/I66PaEO6JT2YxQll4SbX9SThywOeY5icT/ILsAHTZ934M MlH/htHZFVlL6UkFRzwT0fw08bsRCHuLBp1BQRvZFSg9Kr5qAwRDfp6lCeRqO4pD 0+JlolqbzsQw1+QipjPR4jhS7YC79c9pSVv8dZYQaz6/vUwZS+4a1WsMU0uv7IOj ws8np3YpLnQSjA3bZT+l+SKJtwRnJ4q5sYKNmuOi1ZXakTCH6VVfOHeST6cr0cbs qcza0JW3lbbImo+X1mxykte6NKr0fCfu5ygJkctvJJfJx8D9gDk99jxDvEJ/eupn GR7onn/EZ3AiI6mrnFYoj8BS8KH0w== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduledrudelhedgudeghecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecunecujfgurhepfffhvffukfhfgggtugfgjggfse htkedttddtreejnecuhfhrohhmpeffrghnihgvlhcuufhhrghhrghfuceougdrshesuggr nhhivghlrdhshhgrhhgrfhdrnhgrmhgvqeenucggtffrrghtthgvrhhnpefgkefgfeejgf dvvdfguddtleelkedvfeetiedtudfhveevveduhfdvveeffedvueenucfkphepuddtledr ieeirdeifedrvdeftdenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrih hlfhhrohhmpegurdhssegurghnihgvlhdrshhhrghhrghfrdhnrghmvg X-ME-Proxy: Received: from tarpaulin.shahaf.local2 (bzq-109-66-63-230.red.bezeqint.net [109.66.63.230]) by mail.messagingengine.com (Postfix) with ESMTPA id C03BA1080063 for ; Fri, 16 Apr 2021 15:50:30 -0400 (EDT) Received: by tarpaulin.shahaf.local2 (Postfix, from userid 1000) id 4FMRdc6ltbz2T2; Fri, 16 Apr 2021 19:50:28 +0000 (UTC) Date: Fri, 16 Apr 2021 19:50:28 +0000 From: Daniel Shahaf To: zsh-workers@zsh.org Subject: Re: zmathfunc: min, max, sum throw error if result equals 0 Message-ID: <20210416195028.GE15670@tarpaulin.shahaf.local2> References: <20210307171712.GA9936@tarpaulin.shahaf.local2> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: Mutt/1.10.1 (2018-07-13) X-Seq: 48606 Archived-At: X-Loop: zsh-workers@zsh.org Errors-To: zsh-workers-owner@zsh.org Precedence: list Precedence: bulk Sender: zsh-workers-request@zsh.org X-no-archive: yes List-Id: List-Help: List-Subscribe: List-Unsubscribe: List-Post: List-Owner: List-Archive: Again sorry for the delay on getting back to this. Bart Schaefer wrote on Sun, Mar 07, 2021 at 18:25:00 -0800: > On Sun, Mar 7, 2021 at 1:57 PM Daniel Shahaf wrote: > > > > Bart Schaefer wrote on Sun, 07 Mar 2021 21:39 +00:00: > > > > > Is it worth testing invalid cases? Such as uses outside math context > > > where the arguments are not syntax checked? > > > > If you have ideas, feel free to write them and post them; I'll > > transplant them into Z02 once I have committed it. > > One possibility below; I'll follow up if I think of more. > > With your patch adding "true" at the end, we get this: > > % zsh_math_func_min "foo bar" x y z > zsh_math_func_min:7: bad math expression: operator expected at `bar' > zsh_math_func_min:7: bad math expression: operator expected at `bar' > zsh_math_func_min:7: bad math expression: operator expected at `bar' > zsh_math_func_min:9: bad math expression: operator expected at `bar' > toltec-ubuntu% echo $? > 0 > > Doesn't seem as though anything that prints that many error messages > should return zero. > Patch below. > > > Because of the way math context works, if any of $@ is a string that > > > can be interpreted as a math expression, the above will evaluate it at > > > least twice (and up to $# times in the case of $1). This could have > > > side-effects. > > > > Could you post a regression test for this? > > In thinking more about it, I believe this only matters when the > function is called outside of math context. In math context, the > arguments are all going to be evaluated down to numbers before they > are passed to the function. > > % (( x = 0 )); zsh_math_func_min "x += 2" 4 5 6 > % print $x > 8 > % (( (x = 0), min("x += 2", 4, 5, 6) )) > % print $x > 2 I see. Still, going to fix this, if only because those min() max() sum() implementations are virtually the only example of `functions -M` in the tree, and it'd be good to have a complete example. > > Related to code as it is in master, are «(( $arg < result ))» and > > «(( arg < result ))» equivalent? > > No, but again math context matters. > > % arg="x += 2" x=0 result=0 > % (( $arg > result )) && result=$arg > % print $result $x $(( result )) $x > x += 2 1 3 3 > % x=0 result=0 > % (( arg > result )) && (( result=arg )) > % print $result $x $(( result )) $x > 4 4 4 4 Nice. So 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 ☺ Many thanks. Daniel diff --git a/Functions/Math/zmathfunc b/Functions/Math/zmathfunc index 8e4b78549..28f21e562 100644 --- a/Functions/Math/zmathfunc +++ b/Functions/Math/zmathfunc @@ -6,7 +6,12 @@ zsh_math_func_min() { shift local arg for arg ; do - (( $arg < result )) && result=$arg + (( $arg < result )) + case $? in + (0) result=$arg;; + (1) ;; + (*) return $?;; + esac done (( result )) true # Careful here: `return 0` evaluates an arithmetic expression @@ -19,7 +24,12 @@ zsh_math_func_max() { shift local arg for arg ; do - (( $arg > result )) && result=$arg + (( $arg > result )) + case $? in + (0) result=$arg;; + (1) ;; + (*) return $?;; + esac done (( result )) true # Careful here: `return 0` evaluates an arithmetic expression @@ -31,7 +41,7 @@ zsh_math_func_sum() { local sum local arg for arg ; do - (( sum += $arg )) + (( sum += arg )) done (( sum )) true # Careful here: `return 0` evaluates an arithmetic expression