beetlebug▾
/

tcl::mathfunc abs

Gives the absolute value of a number.

tcl::mathfunc::abs NUMBER
expr {abs(NUMBER)}

Description

Gives NUMBER with no sign. An integer gives an integer of any size, so abs of the smallest 64-bit integer is exact. A float gives a float, and abs(-0.0) is 0.0.

Arguments

ArgumentTypeDefaultDescription
NUMBERnumberrequiredAn integer or a float.

Returns

An integer or a float, 0 or more.

Examples

% expr {abs(-3)}
3

% expr {abs(-3.5)}
3.5

% expr {abs(-9223372036854775808)}
9223372036854775808

% expr {abs(-0.0)}
0.0

Errors

# Tcl 9.1 only
% expr {abs("x")}
cannot use non-numeric string "x" as operand of "abs"
-errorcode ARITH DOMAIN {non-numeric string}

# Tcl 8.6 only
% expr {abs("x")}
can't use non-numeric string as operand of "abs"
-errorcode ARITH DOMAIN {non-numeric string}

% expr {abs(1, 2)}
too many arguments for math function "abs"
-errorcode TCL WRONGARGS

% tcl::mathfunc::abs
not enough arguments for math function "abs"
-errorcode TCL WRONGARGS

Facts

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

See also