[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Arithmetic pow results incorrect in arithmetic expansion.
From: |
Chet Ramey |
Subject: |
Re: Arithmetic pow results incorrect in arithmetic expansion. |
Date: |
Sun, 10 Jan 2021 14:09:02 -0500 |
User-agent: |
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Thunderbird/78.6.0 |
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, CWRU chet@case.edu http://tiswww.cwru.edu/~chet/