The expr command handles mathematical functions of the form sin($x) or atan2($y,$x) by converting them to
calls of the form [tcl::mathfunc::sin[expr{$x}]] or [tcl::mathfunc::atan2[expr{$y}][expr{$x}]]. A
number of math functions are available by default within the namespace ::tcl::mathfunc; these functions
are also available for code apart from expr, by invoking the given commands directly.
Tcl supports the following mathematical functions in expressions, all of which work solely with floating-
point numbers unless otherwise noted:
absacosasinatanatan2boolceilcoscoshdoubleentierexpfloorfmodhypotintisfiniteisinfisnanisnormalisqrtissubnormalisunorderedloglog10maxminpowrandroundsinsinhsqrtsrandtantanhwide
In addition to these predefined functions, applications may define additional functions by using proc (or
any other method, such as interpalias or Tcl_CreateObjCommand) to define new commands in the
tcl::mathfunc namespace.
DETAILEDDEFINITIONSabsarg
Returns the absolute value of arg. Arg may be either integer or floating-point, and the result is
returned in the same form.
acosarg
Returns the arc cosine of arg, in the range [0,pi] radians. Arg should be in the range [-1,1].
asinarg
Returns the arc sine of arg, in the range [-pi/2,pi/2] radians. Arg should be in the range
[-1,1].
atanarg
Returns the arc tangent of arg, in the range [-pi/2,pi/2] radians.
atan2yx
Returns the arc tangent of y/x, in the range [-pi,pi] radians. x and y cannot both be 0. If x is
greater than 0, this is equivalent to “atan [expr {y/x}]”.
boolarg
Accepts any numeric value, or any string acceptable to stringisboolean, and returns the
corresponding boolean value 0 or 1. Non-zero numbers are true. Other numbers are false. Non-
numeric strings produce boolean value in agreement with stringistrue and stringisfalse.
ceilarg
Returns the smallest integral floating-point value (i.e. with a zero fractional part) not less
than arg. The argument may be any numeric value.
cosarg
Returns the cosine of arg, measured in radians.
cosharg
Returns the hyperbolic cosine of arg. If the result would cause an overflow, an error is
returned.
doublearg
The argument may be any numeric value, If arg is a floating-point value, returns arg, otherwise
converts arg to floating-point and returns the converted value. May return Inf or -Inf when the
argument is a numeric value that exceeds the floating-point range.
entierarg
The argument may be any numeric value. The integer part of arg is determined and returned. The
integer range returned by this function is unlimited, unlike int and wide which truncate their
range to fit in particular storage widths.
exparg
Returns the exponential of arg, defined as e**arg. If the result would cause an overflow, an
error is returned.
floorarg
Returns the largest integral floating-point value (i.e. with a zero fractional part) not greater
than arg. The argument may be any numeric value.
fmodxy
Returns the floating-point remainder of the division of x by y. If y is 0, an error is returned.
hypotxy
Computes the length of the hypotenuse of a right-angled triangle, approximately “sqrt [expr
{x*x+y*y}]” except for being more numerically stable when the two arguments have substantially
different magnitudes.
intarg
The argument may be any numeric value. The integer part of arg is determined, and then the low
order bits of that integer value up to the machine word size are returned as an integer value.
For reference, the number of bytes in the machine word are stored in the wordSize element of the
tcl_platform array.
isfinitearg
Returns 1 if the floating-point number arg is finite. That is, if it is zero, subnormal, or 2
normal. Returns 0 if the number is infinite or NaN. Throws an error if arg cannot be promoted to a 2
floating-point value.
isinfarg
Returns 1 if the floating-point number arg is infinite. Returns 0 if the number is finite or NaN. 2
Throws an error if arg cannot be promoted to a floating-point value.
isnanarg
Returns 1 if the floating-point number arg is Not-a-Number. Returns 0 if the number is finite or 2
infinite. Throws an error if arg cannot be promoted to a floating-point value.
isnormalarg
Returns 1 if the floating-point number arg is normal. Returns 0 if the number is zero, subnormal, 2
infinite or NaN. Throws an error if arg cannot be promoted to a floating-point value.
isqrtarg
Computes the integer part of the square root of arg. Arg must be a positive value, either an
integer or a floating point number. Unlike sqrt, which is limited to the precision of a floating
point number, isqrt will return a result of arbitrary precision.
issubnormalarg
Returns 1 if the floating-point number arg is subnormal, i.e., the result of gradual underflow. 2
Returns 0 if the number is zero, normal, infinite or NaN. Throws an error if arg cannot be 2
promoted to a floating-point value.
isunorderedxy
Returns 1 if x and y cannot be compared for ordering, that is, if either one is NaN. Returns 0 if 2
both values can be ordered, that is, if they are both chosen from among the set of zero, 2
subnormal, normal and infinite values. Throws an error if either x or y cannot be promoted to a 2
floating-point value.
logarg
Returns the natural logarithm of arg. Arg must be a positive value.
log10arg
Returns the base 10 logarithm of arg. Arg must be a positive value.
maxarg...
Accepts one or more numeric arguments. Returns the one argument with the greatest value.
minarg...
Accepts one or more numeric arguments. Returns the one argument with the least value.
powxy
Computes the value of x raised to the power y. If x is negative, y must be an integer value.
rand Returns a pseudo-random floating-point value in the range (0,1). The generator algorithm is a
simple linear congruential generator that is not cryptographically secure. Each result from rand
completely determines all future results from subsequent calls to rand, so rand should not be used
to generate a sequence of secrets, such as one-time passwords. The seed of the generator is
initialized from the internal clock of the machine or may be set with the srand function.
roundarg
If arg is an integer value, returns arg, otherwise converts arg to integer by rounding and returns
the converted value.
sinarg
Returns the sine of arg, measured in radians.
sinharg
Returns the hyperbolic sine of arg. If the result would cause an overflow, an error is returned.
sqrtarg
The argument may be any non-negative numeric value. Returns a floating-point value that is the
square root of arg. May return Inf when the argument is a numeric value that exceeds the square
of the maximum value of the floating-point range.
srandarg
The arg, which must be an integer, is used to reset the seed for the random number generator of
rand. Returns the first random number (see rand) from that seed. Each interpreter has its own
seed.
tanarg
Returns the tangent of arg, measured in radians.
tanharg
Returns the hyperbolic tangent of arg.
widearg
The argument may be any numeric value. The integer part of arg is determined, and then the low
order 64 bits of that integer value are returned as an integer value.