zsh-workers
 help / color / mirror / Atom feed
From: Stephane Chazelas <stephane@chazelas.org>
To: Bart Schaefer <schaefer@brasslantern.com>
Cc: Zsh hackers list <zsh-workers@zsh.org>
Subject: Re: sh emulation POSIX non-conformances ("inf"/"Inf" in arithmetic expressions)
Date: Tue, 13 Apr 2021 08:17:42 +0100
Message-ID: <20210413071742.tisslgg5fdqbageg@chazelas.org> (raw)
In-Reply-To: <CAH+w=7aAeEMDJX9q4M9jvb0Z8nhYuOdHRk_N4KS91yKP5B5eLQ@mail.gmail.com>

2021-04-12 13:41:58 -0700, Bart Schaefer:
> On Sun, Apr 11, 2021 at 12:32 PM Stephane Chazelas
> <stephane@chazelas.org> 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


  reply	other threads:[~2021-04-13  7:18 UTC|newest]

Thread overview: 60+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-10 23:31 [PATCH] Document imperfections in POSIX/sh compatibility dana
2021-04-10 23:50 ` Bart Schaefer
2021-04-11  0:19   ` dana
2021-04-11 16:54     ` Bart Schaefer
2021-04-11 17:57       ` sh emulation POSIX non-conformances (Was: [PATCH] Document imperfections in POSIX/sh compatibility) Stephane Chazelas
2021-04-11 18:13         ` Bart Schaefer
2021-04-11 19:18         ` sh emulation POSIX non-conformances (no word splitting upon arithmetic expansion) Stephane Chazelas
2021-04-22 15:03           ` Vincent Lefevre
2021-04-22 18:27             ` Bart Schaefer
2021-04-11 19:31         ` sh emulation POSIX non-conformances ("inf"/"Inf" in arithmetic expressions) Stephane Chazelas
2021-04-12 20:41           ` Bart Schaefer
2021-04-13  7:17             ` Stephane Chazelas [this message]
2021-04-22 15:31               ` Vincent Lefevre
2021-04-22 18:55                 ` Bart Schaefer
2021-04-22 20:45                   ` Daniel Shahaf
2021-04-22 21:25                     ` Bart Schaefer
2021-04-23 16:45                   ` Vincent Lefevre
2021-04-23 20:31                     ` Bart Schaefer
2021-04-23 22:46                       ` Oliver Kiddle
2021-04-23 23:34                         ` Bart Schaefer
2021-04-24  2:10                           ` Daniel Shahaf
2021-04-24  3:42                             ` Bart Schaefer
2021-04-24  7:33                               ` Stephane Chazelas
2021-04-24 16:04                                 ` Bart Schaefer
2021-04-24 23:02                         ` Vincent Lefevre
2021-04-25  2:18                           ` Bart Schaefer
2021-04-25 20:17                             ` Vincent Lefevre
2021-04-25 21:58                               ` Bart Schaefer
2021-04-26 10:28                                 ` Vincent Lefevre
2021-04-25 22:00                               ` Bart Schaefer
2021-04-26 10:34                                 ` Vincent Lefevre
2021-04-26 23:25                                   ` Vincent Lefevre
2021-04-11 19:33         ` sh emulation POSIX non-conformances (some of zsh's special variables) Stephane Chazelas
2021-04-11 19:42         ` sh emulation POSIX non-conformances (printf %10s and bytes vs character) Stephane Chazelas
2021-04-13 15:57           ` Daniel Shahaf
2021-04-13 18:03             ` Stephane Chazelas
2021-04-13 21:09               ` Bart Schaefer
2021-04-22 13:59           ` Vincent Lefevre
2021-04-22 14:28             ` Vincent Lefevre
2021-04-22 19:22             ` Bart Schaefer
2021-04-23 16:53               ` Vincent Lefevre
2021-04-23 23:01                 ` Oliver Kiddle
2021-04-24 21:41                   ` Vincent Lefevre
2021-04-24 21:46                   ` Vincent Lefevre
2021-04-24  7:09                 ` Stephane Chazelas
2021-04-24 21:52                   ` Vincent Lefevre
2021-04-24 22:28                     ` Bart Schaefer
2021-04-24 23:18                       ` Vincent Lefevre
2021-04-25  2:20                         ` Bart Schaefer
2021-04-25 11:07                           ` Vincent Lefevre
2021-04-11 23:04       ` [PATCH] Document imperfections in POSIX/sh compatibility dana
2021-04-13 16:01 ` Daniel Shahaf
2021-04-13 16:12   ` Peter Stephenson
2021-04-13 20:28   ` Oliver Kiddle
2021-04-13 21:40     ` dana
2021-04-13 22:02       ` Bart Schaefer
2021-04-14 12:38       ` Daniel Shahaf
2021-04-18  4:50         ` dana
2021-04-20 21:26           ` Daniel Shahaf
2021-05-03 23:42             ` dana

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210413071742.tisslgg5fdqbageg@chazelas.org \
    --to=stephane@chazelas.org \
    --cc=schaefer@brasslantern.com \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

zsh-workers

This inbox may be cloned and mirrored by anyone:

	git clone --mirror http://inbox.vuxu.org/zsh-workers

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V1 zsh-workers zsh-workers/ http://inbox.vuxu.org/zsh-workers \
		zsh-workers@zsh.org
	public-inbox-index zsh-workers

Example config snippet for mirrors.
Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.zsh.workers


code repositories for the project(s) associated with this inbox:

	https://git.vuxu.org/mirror/zsh/

AGPL code for this site: git clone https://public-inbox.org/public-inbox.git