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


Groups > gnu.bash.bug > #15054 > unrolled thread

Re: Arithmetic evaluation of negative numbers with base prefix

Started byIlkka Virta <itvirta@iki.fi>
First post2019-06-18 08:52 +0300
Last post2019-06-18 08:52 +0300
Articles 1 — 1 participant

Back to article view | Back to gnu.bash.bug

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

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

#15054 — Re: Arithmetic evaluation of negative numbers with base prefix

FromIlkka Virta <itvirta@iki.fi>
Date2019-06-18 08:52 +0300
SubjectRe: Arithmetic evaluation of negative numbers with base prefix
Message-ID<mailman.314.1560837200.10840.bug-bash@gnu.org>

[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

[toc] | [standalone]


Back to top | Article view | gnu.bash.bug


csiph-web