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
| Argument | Type | Default | Description | ||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| ARG | expression | required | A part of the expression, joined to the other parts with spaces.
|
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
- In Tcl 8.6 an integer with a leading zero, such as
010, is octal (8). In Tcl 9.1 it is decimal (10), and only0o10is octal. - Tcl 9.1 has the string comparison operators
lt,le,gt, andge, the prefix0dof decimal integers, and_between the digits of a number, such as1_000.
Differences from Tcl
- An operation with no numeric result, such as
0.0 / 0, givesNaN, where Tcl 8.6.16 and 9.0.4 raise a domain error (issue 679). - In Tcl 9.0.4 the message of an operand of the wrong type names the operand and its side, such as
cannot use non-numeric string "ten" as left operand of "+". ztcl gives the message of Tcl 8.6 in both releases (issue 680). - A variable with no value has
-errorcode NONE, where Tcl 8.6.16 and 9.0.4 giveTCL LOOKUP VARNAME(issue 658).
Facts
- Capability
- None.
- Tcl release
- Tcl 9.1 and 8.6.
- Since
- ztcl 1.0.0-beta.1