Re: Arithmetic pow results incorrect in arithmetic expansion.

2021-01-10 Thread Chet Ramey

On 1/9/21 12:22 AM, Oğuz wrote:


Here's another trivial side effect of implementing
unary minus using binary minus:

 $ echo $((16#-10))
 -10


Since `-' is an operator, this is not unary minus. It's the same as
$(( 16# - 10 )), which bash before version 5.1 helpfully (or not) treats
identically to $(( 16#0 - 10 )). Bash-5.1 requires at least one digit
after the base specification, so this is a syntax error.



 $ echo $((-16#10))
 -16


This is unary minus applied to an integer constant.


--
``The lyf so short, the craft so long to lerne.'' - Chaucer
 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRUc...@case.eduhttp://tiswww.cwru.edu/~chet/



Re: Arithmetic pow results incorrect in arithmetic expansion.

2021-01-10 Thread Chet Ramey

On 1/8/21 11:19 PM, Hyunho Cho wrote:


Bash Version: 5.0
Patch Level: 17
Release Status: release



i have tested below in gnome calculator, Qalculate, gawk, perl
and all results in -4 but bash is 4

$ awk 'BEGIN { print -2 ^ 2 }'
-4

$ perl -E 'say -2 ** 2'
-4

$ echo $(( -2 ** 2 )) # only bash results in 4
4


This is how bash does operator precedence.

Like C, unary plus and minus are operators that have higher precedence
than any of the mathematic operators (multiplication/division/addition/
subtraction/etc). So unary operators have a higher precedence than
exponentiation.

This behavior varies widely among programs that implement exponentiation
operators.

--
``The lyf so short, the craft so long to lerne.'' - Chaucer
 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRUc...@case.eduhttp://tiswww.cwru.edu/~chet/



Re: Arithmetic pow results incorrect in arithmetic expansion.

2021-01-09 Thread Oğuz
9 Ocak 2021 Cumartesi tarihinde Ilkka Virta  yazdı:
>
> Note that binary minus doesn't really compare here. It has a lower
> precedence, so gives a different result:
>

Yes, you are right, and not only that, but the example I gave doesn't even
work on 5.1.

  $ (( 16#-10 ))
  bash: ((: 16#: invalid integer constant (error token is "16#")


-- 
Oğuz


Re: Arithmetic pow results incorrect in arithmetic expansion.

2021-01-09 Thread Ilkka Virta
On Sat, Jan 9, 2021 at 9:14 AM Robert Elz  wrote:

> Not "all" other shells, most don't implement exponentiation at all,
> since it isn't a standard C operator.
>

Which also means that the statement about the operators being "same as in
the C language" doesn't really help figure out how this particular one
works.
Which is probably why Zsh has explicitly mentioned it.


Re: Arithmetic pow results incorrect in arithmetic expansion.

2021-01-09 Thread Ilkka Virta
On Sat, Jan 9, 2021 at 7:22 AM Oğuz  wrote:

> 9 Ocak 2021 Cumartesi tarihinde Hyunho Cho  yazdı:
> > $ echo $(( -2 ** 2 )) # only bash results in 4
> > 4
> `bc' does that too. Here's another trivial side effect of implementing
> unary minus using binary minus:
>

Note that binary minus doesn't really compare here. It has a lower
precedence, so gives a different result:

$ echo $(( -3 ** 2 ))
9
$ echo $(( 0 - 3 ** 2 ))
-9


Re: Arithmetic pow results incorrect in arithmetic expansion.

2021-01-08 Thread Robert Elz
Date:Sat, 9 Jan 2021 14:45:49 +0900
From:Hyunho Cho 
Message-ID:  


  | I didn't know that all other shells work the same.

Not "all" other shells, most don't implement exponentiation at all,
since it isn't a standard C operator.

Just the ones that happen to implement it.

kre




Re: Arithmetic pow results incorrect in arithmetic expansion.

2021-01-08 Thread Hyunho Cho
I didn't know that all other shells work the same.
Thanks for the clarification.




2021년 1월 9일 (토) 오후 2:29, Lawrence Velázquez 님이 작성:

> > On Jan 8, 2021, at 11:19 PM, Hyunho Cho  wrote:
> >
> > Machine: x86_64
> > OS: linux-gnu
> > Compiler: gcc
> > Compilation CFLAGS: -g -O2
> > -fdebug-prefix-map=/build/bash-a6qmCk/bash-5.0=.
> > -fstack-protector-strong -Wformat -Werror=format-security -Wall
> > -Wno-parentheses -Wno-format-security
> > uname output: Linux EliteBook 5.4.0-42-generic #46-Ubuntu SMP Fri Jul
> > 10 00:24:02 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
> > Machine Type: x86_64-pc-linux-gnu
> >
> > Bash Version: 5.0
> > Patch Level: 17
> > Release Status: release
> >
> > 
> >
> > i have tested below in gnome calculator, Qalculate, gawk, perl
> > and all results in -4 but bash is 4
> >
> > $ awk 'BEGIN { print -2 ^ 2 }'
> > -4
> >
> > $ perl -E 'say -2 ** 2'
> > -4
> >
> > $ echo $(( -2 ** 2 )) # only bash results in 4
> > 4
>
> The bash results disagree with awk and perl (and, admittedly, common
> mathematical convention), but the man page clearly states that unary
> minus has higher precedence than exponentiation, so this behavior
> is intentional.
>
> The operators and their precedence, associativity, and values are
> the same as in the C language. The following list of operators is
> grouped into levels of equal-precedence operators. The levels are
> listed in order of decreasing precedence.
>
> id++ id--   variable post-increment and post-decrement
> - + unary minus and plus
> ++id --id   variable pre-increment and pre-decrement
> ! ~ logical and bitwise negation
> **  exponentiation
> [remaining operators follow]
>
> Additionally, this precedence is consistent with other shells.
>
> % bash --version | head -n 1
> GNU bash, version 5.0.17(1)-release (x86_64-apple-darwin18.7.0)
> % bash -c 'printf %s\\n "$((-2 ** 2))"'
> 4
>
> % ksh --version
>   version sh (AT Research) 93u+ 2012-08-01
> % ksh -c 'printf %s\\n "$((-2 ** 2))"'
> 4
>
> % zsh --version
> zsh 5.8 (x86_64-apple-darwin18.7.0)
> % zsh -fc 'printf %s\\n "$((-2 ** 2))"'
> 4
> % zsh -f -o C_PRECEDENCES -c 'printf %s\\n "$((-2 ** 2))"'
> 4
>
> The zshmisc(1) man page even goes out of its way to address this.
>
> Note the precedence of exponentiation in both cases is below that of
> unary operators, hence `-3**2' evaluates as `9', not `-9'. Use
> parentheses where necessary: `-(3**2)'. This is for compatibility
> with other shells.
>
> I don't know why things shook out this way, but it sure seems like
> the ship has sailed.
>
> vq
>


Re: Arithmetic pow results incorrect in arithmetic expansion.

2021-01-08 Thread Lawrence Velázquez
> On Jan 8, 2021, at 11:19 PM, Hyunho Cho  wrote:
> 
> Machine: x86_64
> OS: linux-gnu
> Compiler: gcc
> Compilation CFLAGS: -g -O2
> -fdebug-prefix-map=/build/bash-a6qmCk/bash-5.0=.
> -fstack-protector-strong -Wformat -Werror=format-security -Wall
> -Wno-parentheses -Wno-format-security
> uname output: Linux EliteBook 5.4.0-42-generic #46-Ubuntu SMP Fri Jul
> 10 00:24:02 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
> Machine Type: x86_64-pc-linux-gnu
> 
> Bash Version: 5.0
> Patch Level: 17
> Release Status: release
> 
> 
> 
> i have tested below in gnome calculator, Qalculate, gawk, perl
> and all results in -4 but bash is 4
> 
> $ awk 'BEGIN { print -2 ^ 2 }'
> -4
> 
> $ perl -E 'say -2 ** 2'
> -4
> 
> $ echo $(( -2 ** 2 )) # only bash results in 4
> 4

The bash results disagree with awk and perl (and, admittedly, common
mathematical convention), but the man page clearly states that unary
minus has higher precedence than exponentiation, so this behavior
is intentional.

The operators and their precedence, associativity, and values are
the same as in the C language. The following list of operators is
grouped into levels of equal-precedence operators. The levels are
listed in order of decreasing precedence.

id++ id--   variable post-increment and post-decrement
- + unary minus and plus
++id --id   variable pre-increment and pre-decrement
! ~ logical and bitwise negation
**  exponentiation
[remaining operators follow]

Additionally, this precedence is consistent with other shells.

% bash --version | head -n 1
GNU bash, version 5.0.17(1)-release (x86_64-apple-darwin18.7.0)
% bash -c 'printf %s\\n "$((-2 ** 2))"' 
4

% ksh --version
  version sh (AT Research) 93u+ 2012-08-01
% ksh -c 'printf %s\\n "$((-2 ** 2))"'
4

% zsh --version
zsh 5.8 (x86_64-apple-darwin18.7.0)
% zsh -fc 'printf %s\\n "$((-2 ** 2))"'
4
% zsh -f -o C_PRECEDENCES -c 'printf %s\\n "$((-2 ** 2))"'
4

The zshmisc(1) man page even goes out of its way to address this.

Note the precedence of exponentiation in both cases is below that of
unary operators, hence `-3**2' evaluates as `9', not `-9'. Use
parentheses where necessary: `-(3**2)'. This is for compatibility
with other shells.

I don't know why things shook out this way, but it sure seems like
the ship has sailed.

vq



Re: Arithmetic pow results incorrect in arithmetic expansion.

2021-01-08 Thread Oğuz
9 Ocak 2021 Cumartesi tarihinde Hyunho Cho  yazdı:
>
> i have tested below in gnome calculator, Qalculate, gawk, perl
> and all results in -4 but bash is 4
>
> $ awk 'BEGIN { print -2 ^ 2 }'
> -4
>
> $ perl -E 'say -2 ** 2'
> -4
>
> $ echo $(( -2 ** 2 )) # only bash results in 4
> 4
>

`bc' does that too. Here's another trivial side effect of implementing
unary minus using binary minus:

$ echo $((16#-10))
-10
$ echo $((-16#10))
-16


-- 
Oğuz


Arithmetic pow results incorrect in arithmetic expansion.

2021-01-08 Thread Hyunho Cho
Machine: x86_64
OS: linux-gnu
Compiler: gcc
Compilation CFLAGS: -g -O2
-fdebug-prefix-map=/build/bash-a6qmCk/bash-5.0=.
-fstack-protector-strong -Wformat -Werror=format-security -Wall
-Wno-parentheses -Wno-format-security
uname output: Linux EliteBook 5.4.0-42-generic #46-Ubuntu SMP Fri Jul
10 00:24:02 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
Machine Type: x86_64-pc-linux-gnu

Bash Version: 5.0
Patch Level: 17
Release Status: release



i have tested below in gnome calculator, Qalculate, gawk, perl
and all results in -4 but bash is 4

$ awk 'BEGIN { print -2 ^ 2 }'
-4

$ perl -E 'say -2 ** 2'
-4

$ echo $(( -2 ** 2 )) # only bash results in 4
4