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.3 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,UNPARSEABLE_RELAY autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 16937 invoked from network); 13 Apr 2021 07:18:04 -0000 Received: from zero.zsh.org (2a02:898:31:0:48:4558:7a:7368) by inbox.vuxu.org with ESMTPUTF8; 13 Apr 2021 07:18:04 -0000 ARC-Seal: i=1; cv=none; a=rsa-sha256; d=zsh.org; s=rsa-20200801; t=1618298284; b=O/95sVVpQVlfwqkvaJ2Wn9pskVYYZcG/mCqu8j/jzovYSqeeVSH+Kl4BAQPgH7xWFl55C94Edc 17Riyt5LXlCQQhZkjnumcg13MORBb7N1TzBq3po1Tw3FO1iRbD5fny7XphwGnuqz2eVTdtL5m/ jkMmhi9qvv1UvxLndI5ee69AegLnsEyw5UkYxKsgV6XZJ/4t3wWqSEKByywK59yn7XQediZ+pB NM06+alt9OxW3xORCF+bX9yykZac6SEGLj/PIVIDTfz2FtdxmGvt8sxVdfs9WYPU7+8huP82zo zuHvUJWXtmAZSsz6KJ9YrrtJC9TxFyxMZmS6xfDmmFnapg==; ARC-Authentication-Results: i=1; zsh.org; iprev=pass (relay8-d.mail.gandi.net) smtp.remote-ip=217.70.183.201; dmarc=none header.from=chazelas.org; arc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed; d=zsh.org; s=rsa-20200801; t=1618298284; bh=lFapB5lCNuZoR349L/9uD+S4P16nV6Jc9eZutBhFfcU=; h=List-Archive:List-Owner:List-Post:List-Unsubscribe:List-Subscribe:List-Help: List-Id:Sender:In-Reply-To:Content-Type:MIME-Version:References:Message-ID: Subject:Cc:To:From:Date:DKIM-Signature; b=UphjrFV8xuwY46QtiVzM0WhP5FNwlfnJSik60IzI1oDqxjZlSiZs3uAv2vchn3b8qrDWp4iZJZ tQqhmfYSx/JYdf6mRcvAJ2/Zhl3gL5gY50/zNjJ/CXihRSJdubm/j1jVNtQ38BuwijzndfGglf q0YSA9mL+8pIpBkYYHQFhXADBQDhcqhxzmA88hInUSVtlDPpIQg5ktakfCK7++sywdXUxzoJvR FeAf4T5n1sv0Hrk6tF+31VZB1Dv0oyGhxSS5uEc62wD7RyTZaKZZ/9E/lIzTFhEX/pQS/zs/Ry ztZIQUPRdwjHRaNJxNqStPBc+FbImsOVhvcCmTax6r0kqg==; 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-Type:MIME-Version :References:Message-ID:Subject:Cc:To:From:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID; bh=JX3gZJ0QXvI7WkRX0EkYWmzX2hBcpyljs2WKodzMkFg=; b=bU+j/ualXsWGo8IRSrSr3b+4KD jZqWIwS8B+Hc1m9PbQd3ezSy8K3VWxsstvO7PSPCYno1r1McWPZh3TkdrqTWqXr0miSSWR58EGTLB JGZc+tPReMtnQIGtMaBQwelfAiERWFswbtG4cRl6VDYdf7UAA1yDaqS9ecpllCScMQMKV5ZAZrKKq bFwF0I2xefqPH9QEF4/MllVuHUaQnYTudlqDe95g8heeJYiVCUAZuSZaTo+YWIaGlgqFNHZUN239h vhDF/ZVMVyuT0wrSGTnSGfkQYtSi9DnkveGFhvno7Sd5zKovSvNijsAkJJIVXPaTVQ/ToH1AMTr8t 4VZdNH+A==; Received: from authenticated user by zero.zsh.org with local id 1lWDJC-000BdA-Tk; Tue, 13 Apr 2021 07:18:03 +0000 Authentication-Results: zsh.org; iprev=pass (relay8-d.mail.gandi.net) smtp.remote-ip=217.70.183.201; dmarc=none header.from=chazelas.org; arc=none Received: from relay8-d.mail.gandi.net ([217.70.183.201]:55425) by zero.zsh.org with esmtps (TLS1.2:ECDHE-RSA-AES256-GCM-SHA384:256) id 1lWDIu-000BNH-BY; Tue, 13 Apr 2021 07:17:44 +0000 X-Originating-IP: 90.215.204.106 Received: from chazelas.org (unknown [90.215.204.106]) (Authenticated sender: stephane@chazelas.org) by relay8-d.mail.gandi.net (Postfix) with ESMTPSA id 0B7611BF204; Tue, 13 Apr 2021 07:17:42 +0000 (UTC) Date: Tue, 13 Apr 2021 08:17:42 +0100 From: Stephane Chazelas To: Bart Schaefer Cc: Zsh hackers list Subject: Re: sh emulation POSIX non-conformances ("inf"/"Inf" in arithmetic expressions) Message-ID: <20210413071742.tisslgg5fdqbageg@chazelas.org> Mail-Followup-To: Bart Schaefer , Zsh hackers list References: <7FD930F4-37CD-402B-9A06-893818856199@dana.is> <20210411175726.hxnm33mxoska2tsm@chazelas.org> <20210411193154.zkekzvb4o6xmriwo@chazelas.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Seq: 48518 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: 2021-04-12 13:41:58 -0700, Bart Schaefer: > On Sun, Apr 11, 2021 at 12:32 PM Stephane Chazelas > wrote: > > > > $ zsh --emulate sh -c 'inf=42; echo $((inf))' > > Inf > > > > (POSIX requires 42 there). > > Is that because "Inf" is case-sensitive, or because POSIX requires > evaluating the variable? E.g. what does That was because "inf" in an arithmetic expression, where inf is the name of a variable whose contents is an integer constant (decimal, octal or hex) is meant to represent the corresponding integer number (and an empty or unset variable is meant to yield 0).. https://pubs.opengroup.org/onlinepubs/9699919799.2018edition/basedefs/V1_chap08.html#tag_08 warns about some of the special variables used by some shells (such as RANDOM and SECONDS), but in those cases, $((RANDOM)) still expands to the value of the variable. Here, a user must make sure they don't use "inf" (or INF/InF/Inf, nan/NAN...) as the name of a variable if they want to use it in an arithmetic expression. Same family of issues as those linked to zsh special parameters or keywords/builtins. Since the POSIX shell language doesn't support floating point arithmetics, zsh could disable it in POSIX mode, but it may not be worth the bother. Since floating point arithmetics is supported by a few shells (ksh93, yash, zsh), maybe a better approach would be for text to be added to the POSIX standard to warn against using those as variable names. I think it would be worth documenting that nan and inf are recognised in arithmetic expressions (and warn against using variables with the same name). Maybe something like: diff --git a/Doc/Zsh/arith.yo b/Doc/Zsh/arith.yo index bc3e35ad5..44c35edab 100644 --- a/Doc/Zsh/arith.yo +++ b/Doc/Zsh/arith.yo @@ -120,6 +120,11 @@ taken for a parameter name. All numeric parts (before and after the decimal point and in the exponent) may contain underscores after the leading digit for visual guidance; these are ignored in computation. +tt(Inf)) and tt(NaN) and all their variation of case (tt(inf), tt(NAN), etc.) +are also recognised as meaning "infinity" and "not-a-number" floating point +constants respectively. One should avoid using variables with such names when +they are to be used in arithmetic expressions. + cindex(arithmetic operators) cindex(operators, arithmetic) An arithmetic expression uses nearly the same syntax and > > sh -c 'Inf=42; echo $((Inf))' > > yield in POSIX? What about 42 > sh -c 'unset Inf; echo $((Inf))' 0 > sh -c 'unset inf; echo $((inf))' 0 > ?? I don't have a POSIX shell to test with, it seems. Ksh "Version A > 2020.0.0" responds the same as zsh, and bash "5.0.17(1)-release" > doesn't seem to have Inf at all (and gives a syntax error on > floating-point arithmetic?). There's not really such a thing as a POSIX shell. There's a standard specification of the POSIX sh *language*, and a number of shell implementations that try to provide a compliant interpreter for that language. That language specification was initially based on a subset of ksh88's, but with some deviations. The only Unix shells that I know that have been /certified/ as compliant are some ksh88 derivatives (like on Solaris, AIX, HPUX) and some versions of bash (in posix mode and with xpg_echo enabled, like on macos, Inspur K/UX). They both have non-conformances, especially ksh88-based ones (which have much more serious ones than the one I've listed in this thread). I'd say zsh's sh emulation is probably more compliant than those ksh88-based ones. The Opengroup does have a certification test suite, but I don't think it's publicly available. Note that ksh2020 development has been abandoned. It was based on a beta version of ksh93 released when AT&T Research was shut down, but eventually deemed too buggy to fix. There is still some community effort to maintain a ksh93u+ based shell. Having said that, ksh93/ksh2020 is one of the few Bourne-like shells that support floating point arithmetic expressions (and the first one that did). $((inf)) expands to "inf" for me with those (which would also make it non-compliant). yash (the other shell with floating point arithmetic expressions and which was written to the POSIX specification) doesn't support nan/inf in arithmetic expressions unless you do inf=inf nan=nan (and Inf=inf, NaN=nan... if you want to use those) and disables floating point arithmetics when in posix mode (yash -o posix). All three handle the locale decimal radix character differently, which makes me think there's little hope floating point arithmetics ever makes it to the POSIX spec. -- Stephane