|
From: | Ilkka Virta |
Subject: | Re: Arithmetic evaluation of negative numbers with base prefix |
Date: | Tue, 18 Jun 2019 08:52:59 +0300 |
User-agent: | Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:60.0) Gecko/20100101 Thunderbird/60.7.1 |
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
expr-allow-sign.c
Description: Text document
[Prev in Thread] | Current Thread | [Next in Thread] |