expr

Computes an expression with the operators of C and the math functions.

expr ARG ?ARG ...?

Description

Joins the ARG words with spaces, reads the result as an expression, and gives its value. An expression has numbers, strings in quotes or braces, variables, command substitutions in brackets, operators, parentheses, and calls of math functions such as sqrt(2).

Put the expression in braces: expr {$a * 2}. Then expr does the substitution of variables and commands itself, a value from a variable stays one operand, and the expression is compiled once. With no braces, the parser of Tcl substitutes first, so set a {1 + 1}; expr $a * 2 computes 1 + 1 * 2.

An integer has no fixed size: 2 ** 100 is exact. An integer divided by an integer is an integer rounded down, and % has the sign of the divisor. An operation with a double gives a double. A string compares as a number with < and == when both operands are numbers, and as text when one is not. eq and ne always compare the text.

&&, ||, and ?: read their operands only as far as they need: expr {0 && [error x]} gives 0 and runs no command.

The number forms are integers in decimal, 0x hex, 0o octal, and 0b binary, and doubles such as 1.5, .5, 1e3, Inf, and -Inf. The values true, false, yes, no, on, and off are booleans for !, &&, ||, and ?:.

Arguments

ArgumentTypeDefaultDescription
ARGexpressionrequiredA part of the expression, joined to the other parts with spaces.
ValueMeaning
- + ~ !Unary minus, plus, bitwise not, and logical not. The highest precedence.
**Power. It groups from the right: 2 ** 3 ** 2 is 2 ** 9.
* / %Multiply, divide, and remainder.
+ -Add and subtract.
<< >>Shift left and right.
< > <= >=Compare as numbers, or as text when an operand is not a number.
lt gt le geCompare as text. Tcl 9.1 only
== !=Equal and not equal, as numbers when both are numbers.
eq neEqual and not equal as text.
in niThe left operand is, or is not, an element of the list on the right.
&Bitwise and.
^Bitwise exclusive or.
|Bitwise or.
&&Logical and: the right operand runs only when the left one is true.
||Logical or: the right operand runs only when the left one is false.
?:If then else: a ? b : c. The lowest precedence.

Returns

The value of the expression: an integer, a double, a boolean as 0 or 1, or a string.

Examples

% expr {2 ** 100}
1267650600228229401496703205376

% expr {7 / 2}
3

% expr {-7 / 2}
-4

% expr {7 % -2}
-1

% expr {7 / 2.0}
3.5

% expr {"a" eq "a" ? "same" : "other"}
same

% expr {5 in {1 5 9}}
1

% expr {"1" == "1.0"}
1

% expr {"1" eq "1.0"}
0

% expr {2 ** 3 ** 2}
512

% expr {0x10 + 0b101}
21

% expr {1.0 / 0}
Inf

% expr {0 && [error never]}
0

% expr {abs(-3) + max(1, 5)}
8

% expr {1 + 2} {* 3}
7

# With no braces, the value of a is a part of the expression.
% set a {1 + 1}
> expr $a * 2
3

# Tcl 8.6 only
% expr {010}
8

# Tcl 9.1 only
% expr {010}
10

# Tcl 9.1 only
% expr {1_000 + 1}
1001

# Tcl 9.1 only
% expr {"a" lt "b"}
1

Errors

% expr {1 / 0}
divide by zero
-errorcode ARITH DIVZERO {divide by zero}

% expr {"ten" + 1}
can't use non-numeric string as operand of "+"
-errorcode ARITH DOMAIN {non-numeric string}

% expr {7.0 % 2}
can't use floating-point value as operand of "%"
-errorcode ARITH DOMAIN {floating-point value}

% expr {0 ** -1}
exponentiation of zero by negative power
-errorcode ARITH DOMAIN {exponentiation of zero by negative power}

% expr {(1 + 2}
unbalanced open paren
in expression "(1 + 2"
-errorcode TCL PARSE EXPR UNBALANCED

% expr {nosuchfunc(1)}
invalid command name "tcl::mathfunc::nosuchfunc"
-errorcode TCL LOOKUP COMMAND tcl::mathfunc::nosuchfunc

% expr
wrong # args: should be "expr arg ?arg ...?"
-errorcode TCL WRONGARGS

Tcl 8.6 and Tcl 9.1

Differences from Tcl

Facts

Capability
None.
Tcl release
Tcl 9.1 and 8.6.
Since
ztcl 1.0.0-beta.1

See also