Version 17 of **

Updated 2009-05-27 16:54:50 by ZB

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.


Precedence

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}

ZB That "someone" must be a lawyer ;) Current TCL's way of calculation seems logical to me. If one's writing "second power of minus two", it means - logically - that the intention is "to raise minus two to power of two", and not: "raise two to two - then change sign". Interpreting the minus - if it's a very first sign of expression (when no parentheses) - as sign of the first operand (and not as changing the sign of final result) is just logical.