Operators and Math Functions

The expr command recognizes the following operators, in decreasing order of precedence:

+ − ˜ !

Unary plus and minus, bitwise NOT, logical NOT

* / %

Multiply, divide, remainder

+ −

Add, subtract

<< >>

Bitwise shift left, bitwise shift right

< > <= >=

Boolean comparison for less than, greater than, less than or equal, greater than or equal

== !=

Boolean test for equality, inequality

&

Bitwise AND

^

Bitwise exclusive OR

|

Bitwise inclusive OR

&&

Logical AND

||

Logical OR

x?y:z

If x !=0, then y, else z

All operators support integers. All except ˜, %, <<, >>, &, ^, and | support floating-point values. Boolean operators can also be used for string operands, in which case string comparison will be used. This will occur if any of the operands are not valid numbers. The &&, ||, and ?: operators have lazy evaluation, as in C, in which evaluation stops if the outcome can be determined.

The expr command also recognizes the following math functions:

abs(arg)

Absolute value of arg

acos(arg)

Arc cosine of arg

asin(arg)

Arc sine of arg

atan(arg)

Arc tangent of arg

atan2(x, y)

Arc tangent of x/y

ceil(arg)

Rounds arg up to the nearest integer

cos(arg)

Cosine of arg

cosh(arg)

Hyperbolic cosine of arg

double(arg)

Floating-point value of arg

exp(arg)

e to the power of arg

floor(arg)

Round arg down to the nearest integer

fmod(x, y)

Remainder of x/y

hypot(x, y)

sqrt (x*x + y*y)

int(arg)

arg as integer by truncating

log(arg)

Natural logarithm of arg

log10(arg)

Base 10 logarithm of arg

pow(x, y)

x raised to the exponent y

rand()

Random ...

Get Tcl/Tk in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.