Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > gnu.bash.bug > #15051

Re: Arithmetic evaluation of negative numbers with base prefix

Path csiph.com!xmission!news.snarked.org!news.linkpendium.com!news.linkpendium.com!panix!usenet.stanford.edu!not-for-mail
From Jeremy Townshend <jeremy.townshend@gmail.com>
Newsgroups gnu.bash.bug
Subject Re: Arithmetic evaluation of negative numbers with base prefix
Date Mon, 17 Jun 2019 14:30:27 +0100
Lines 48
Approved bug-bash@gnu.org
Message-ID <mailman.275.1560778236.10840.bug-bash@gnu.org> (permalink)
References <20190614141947.GB27242@tower> <dc253e63-de07-6c60-7b20-1688286a43d2@case.edu> <20190617133026.GC27242@tower>
NNTP-Posting-Host lists.gnu.org
Mime-Version 1.0
Content-Type text/plain; charset=us-ascii
X-Trace usenet.stanford.edu 1560778237 734 209.51.188.17 (17 Jun 2019 13:30:37 GMT)
X-Complaints-To action@cs.stanford.edu
Cc bug-bash@gnu.org, bash@packages.debian.org
To Chet Ramey <chet.ramey@case.edu>
Envelope-to bug-bash@gnu.org
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=gkU4dG+mjUS1gXs9RHrCcG1UHtDGWsrSjGPoV+GpJEA=; b=FnhTBLcFmOWH4leLy4Iwfiwoz3oCeqp7lsLoxJeeeqf5A7DhJoOfvDH2GX+uSzXIxU /mrJjdeMXFX2PbaxNKEsgvY558jUeU8KDtN3vVBH4q+bFAe3BGbornacAMuGuahAJBJ0 itWJjFIPinS9fIy3JCfG/HlD5fN4fKwlcDks8AxhHgOzgV+OxEpTNcnvKwFl0Daok2vc WwujDVqeiOY+APv2RgL8Ut/5Mc5e+gr/8A+V9jkH+Lt4uVQTfmgI+LHgJWSVFoSdKsbw hJrOxuZtLZ/6G1pugd+IudZQQY4+SPLriAnFZkfTaePAyA4DRzYND9Dy7TtweiJB8kVY 72ng==
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=gkU4dG+mjUS1gXs9RHrCcG1UHtDGWsrSjGPoV+GpJEA=; b=tRver3p6N/gHeEshzKfrFb+mSD5iQ9MRBLmmHK/zAGRbMVefwd/IPHITdgLF0YzoEF UE5YdN+ZkqhcibiOchFPOK/Ee2eqzLwMlC+OfOdb7cdwZYkWZ8tQ2bFJSj/9WTKQYB2n oFSSYaKVgQsKjBE2AWlW5zVBtXw7DACwUdcEetHCu6RhzRzRIT1GnRSApjQcTtTlwV8Q xNgrn7Tceo4dz0LzRlcAiF5Urdb5F7RWzmG61WqEtDE6yoGBlL67KtUFCA5Mz8LkaDt8 n79Bfgof+++6jIbo9uYN9RBGJlJXjvSaJP7oOESTknfj6Dg1uThkpm7phTDGSh0++DG9 Z/NQ==
X-Gm-Message-State APjAAAWVe3qt4V0koayrvgFhE2MhyeGdsAnST33rve2dqOVJ4nWNHB8s Bf1BZhY2OJ8Qr2mUwcr040U=
X-Google-Smtp-Source APXvYqz3uYOfKjB0y480ee1LLPqRSp+04ho773iAeuQLUSvRFw3EXTSQSGtQTfezf9pb6molsPUF9Q==
X-Received by 2002:adf:f3c7:: with SMTP id g7mr75768538wrp.133.1560778229986; Mon, 17 Jun 2019 06:30:29 -0700 (PDT)
Content-Disposition inline
In-Reply-To <dc253e63-de07-6c60-7b20-1688286a43d2@case.edu>
User-Agent Mutt/1.9.4 (2018-02-28)
X-detected-operating-system by eggs.gnu.org: Genre and OS details not recognized.
X-Received-From 2a00:1450:4864:20::433
X-BeenThere bug-bash@gnu.org
X-Mailman-Version 2.1.23
Precedence list
List-Id Bug reports for the GNU Bourne Again SHell <bug-bash.gnu.org>
List-Unsubscribe <https://lists.gnu.org/mailman/options/bug-bash>, <mailto:bug-bash-request@gnu.org?subject=unsubscribe>
List-Archive <https://lists.gnu.org/archive/html/bug-bash>
List-Post <mailto:bug-bash@gnu.org>
List-Help <mailto:bug-bash-request@gnu.org?subject=help>
List-Subscribe <https://lists.gnu.org/mailman/listinfo/bug-bash>, <mailto:bug-bash-request@gnu.org?subject=subscribe>
X-Mailman-Original-Message-ID <20190617133026.GC27242@tower>
X-Mailman-Original-References <20190614141947.GB27242@tower> <dc253e63-de07-6c60-7b20-1688286a43d2@case.edu>
Xref csiph.com gnu.bash.bug:15051

Show key headers only | View raw


Dear Chet

Many thanks for your impressively swift response.  It is enlightening to see
how these expressions are parsed.

For the record, whilst I can now see how they are parsed, it feels
particularly unsatisfactory that the following two expressions yield the same
result when the variable i happens to have unwittingly been decremented below
zero (by bash arithmetic evaluation by the way - not by the output of some
external command):

  echo $((3-10#${i})) 
  echo $((3+10#${i})) 

As you indicate, this is caused by 10# being parsed as zero.  That silent
assumption of zero effectively then also silently nullifies/swallows the
preceding operator.

Ilkka Virta's email helpfully pointed me to a somewhat related debate that
occurred about 11 months ago.  I agree with your comment in this debate:

  "There would be a good case for rejecting the '10#' because it's missing
  the value."

It is this silently proceeding with a plausible (but undesirable) output in
such cases which is especially concerning.

In the meantime it would seem cautionary to advise against the pitfall of
using base# prefixed to variables (contrary to
mywiki.wooledge.org/ArithmeticExpression) unless you can be confident that
they will never be decremented below zero.

At the very least it would be helpful if the manual reflected that 10#
followed by anything other than a digit ([0-9a-zA-Z@_]) is parsed as zero, and
rlarified more completely the constraints of "number" for "n" in the "base#"
paragraph.  

I cannot find anywhere else in the manual where the word "number", "numeric
value" or "integer" excludes values less than zero without explicitly stating
so.  On the other hand phrases like "[if] ...  number/numeric values less than
zero", "if ...  [not] a number greater than [or equal to] zero" are used
repeatedly.  In those cases "number" clearly doesn't exclude those less than
zero.


Jeremy Townshend

Back to gnu.bash.bug | Previous | Next | Find similar | Unroll thread


Thread

Re: Arithmetic evaluation of negative numbers with base prefix Jeremy Townshend <jeremy.townshend@gmail.com> - 2019-06-17 14:30 +0100

csiph-web