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


Groups > gnu.bash.bug > #15054

Re: Arithmetic evaluation of negative numbers with base prefix

From Ilkka Virta <itvirta@iki.fi>
Newsgroups gnu.bash.bug
Subject Re: Arithmetic evaluation of negative numbers with base prefix
Date 2019-06-18 08:52 +0300
Message-ID <mailman.314.1560837200.10840.bug-bash@gnu.org> (permalink)
References <20190614141947.GB27242@tower> <dc253e63-de07-6c60-7b20-1688286a43d2@case.edu> <20190617133026.GC27242@tower> <20190617154708.GG2072@eeg.ccf.org> <bc6739d9-03f2-44b2-5bb5-22599e41cb5b@iki.fi>

Show all headers | View raw


[Multipart message — attachments visible in raw view] - view raw

On 17.6. 18:47, Greg Wooledge wrote:
> On Mon, Jun 17, 2019 at 02:30:27PM +0100, Jeremy Townshend wrote:
>> 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.
> 
> Fair point.  I've updated <https://mywiki.wooledge.org/ArithmeticExpression>
> and <https://mywiki.wooledge.org/BashPitfalls>.

Good!

I still wish this could be fixed to do the useful thing without any 
workarounds, given it's what ksh and zsh do, and since this is the 
second time it comes up on the list, it appears to be surprising to 
users, too.

The <base># prefix is already an extension of the C numeric constant 
syntax, so extending it further to include an optional sign wouldn't 
seem in inappropriate.


I took a look last night and made some sort of a patch. It seems to 
work, though I'm not sure if I've missed any corner cases. Apart from 
the digitless '10#', the behaviour matches ksh and zsh, I made it an 
error, they apparently allow it.

   $ cat test.sh
   echo $(( 10 * 10#-123 ))  # -1230
   echo $(( 10 * 10#-008 ))  #   -80
   echo $(( 10 * 10#1+23 ))  #    10*1 + 23 = 33
   echo $(( 10# ))           #  error

   $ ./bash test.sh
   -1230
   -80
   33
   test.sh: line 5: 10#: no digits in number (error token is "10#")

   $ ksh test.sh
   -1230
   -80
   33
   0


-- 
Ilkka Virta / itvirta@iki.fi

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


Thread

Re: Arithmetic evaluation of negative numbers with base prefix Ilkka Virta <itvirta@iki.fi> - 2019-06-18 08:52 +0300

csiph-web