expr "raise to power" operator, similar to pow function
Returns an integer value if both arguments are integers, and double-precision floating-point otherwise.
The left operand may be any integer from Tcl's unlimited integer range.
The right operand is limited to a max integer value of 268435455 (0xfffffff) (28 bits). (Unless the left operand is -1, 0, or 1, so that the answer is trivial).
Expect to wait a long while for [expr 2**0xfffffff] to return.
During early 2009, a thread broke out in the comp.lang.tcl usenet group discussing the observation that Tcl's calculation for
puts [expr {-2**2}]
surprised someone who was expecting that to mean
puts [expr {-(2**2)}
while Tcl's operator precedence rules cause it to mean
puts [expr {(-2)**2}
What this does mean is that it is consistently possible to interpret -2 as minus two; no other operators around can change the interpretation.
It also means the answer you get will be different than most, if not all, other computer programming languages.
AMG: Is this set in concrete? I was recently surprised by this behavior, not for the first time, and likely not for the last time. I'm hard pressed to think of any case where it's important that - have higher precedence than **, especially since Tcl expr doesn't do complex numbers.
RLE: (2012-03-07) The problem is that the raw (no parens) equation is requesting expr to calculate this value:
Does that mean the negative of the square of two, or does it mean the square of negative two? If I am remembering my math correctly from way too long ago, I would interpret that equation to mean the square of minus two, which would result in four, just as Tcl produces.
From comp.lang.tcl:
In math:
a**0 == 1, a != 0, and 0**a == 0, a != 0. 0**0 is an indeterminant
In Tcl (8.5.7):
expr {0**0} => 1
Here is an interesting example which tests the precedence of ** and - and demonstrates the right to left application of **:
expr {2**-2**2} => 16
Force delayed application of -:
expr {2.0**-(2**2)} => .0625
Force left to right exponentiation:
expr {(2.0**-2)**2} => .0625
Unambiguous:
expr {(2.0)**((-2)**2)} => 16