beetlebug▾
/

tcl::mathop arithmetic

The commands of the operators of arithmetic: add, subtract, multiply, divide, remainder, and power.

tcl::mathop::+ ?NUMBER ...?
tcl::mathop::- NUMBER ?NUMBER ...?
tcl::mathop::* ?NUMBER ...?
tcl::mathop::/ NUMBER ?NUMBER ...?
tcl::mathop::% INTEGER INTEGER
tcl::mathop::** ?NUMBER ...?

Description

Each command works on numbers: integers of any size and floats. Two integers give an integer, and a float on either side gives a float.

Arguments

ArgumentTypeDefaultDescription
NUMBERnumberrequiredAn integer of any size or a float.
INTEGERintegerrequiredAn integer of any size.

Returns

A number: an integer when each argument is an integer, else a float, except / with one argument, which gives a float.

Examples

% tcl::mathop::+ 1 2 3
6

% tcl::mathop::+
0

# An integer has any size.
% tcl::mathop::+ 9223372036854775807 1
9223372036854775808

% tcl::mathop::- 5
-5

% tcl::mathop::- 10 1 2
7

% tcl::mathop::* 2 2.5
5.0

% tcl::mathop::/ 7 2
3

% tcl::mathop::/ -7 2
-4

% tcl::mathop::/ 7.0 2
3.5

% tcl::mathop::/ 4
0.25

% tcl::mathop::/ 1.0 0
Inf

% tcl::mathop::% -7 3
2

% tcl::mathop::% 7 -3
-2

% tcl::mathop::** 2 3 2
512

% tcl::mathop::** 2 100
1267650600228229401496703205376

% tcl::mathop::** 2 -1
0

Errors

% tcl::mathop::/ 1 0
divide by zero
-errorcode ARITH DIVZERO {divide by zero}

% tcl::mathop::% 7 0
divide by zero
-errorcode ARITH DIVZERO {divide by zero}

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

% tcl::mathop::-
wrong # args: should be "tcl::mathop::- value ?value ...?"
-errorcode TCL WRONGARGS

% tcl::mathop::% 7
wrong # args: should be "tcl::mathop::% integer integer"
-errorcode TCL WRONGARGS

# Tcl 9.1 only
% tcl::mathop::% 7.5 2
cannot use floating-point value "7.5" as left operand of "%"
-errorcode ARITH DOMAIN {floating-point value}

# Tcl 8.6 only
% tcl::mathop::% 7.5 2
can't use floating-point value as operand of "%"
-errorcode ARITH DOMAIN {floating-point value}

# Tcl 9.1 only
% tcl::mathop::- 1 x
cannot use non-numeric string "x" as right operand of "-"
-errorcode ARITH DOMAIN {non-numeric string}

# Tcl 8.6 only
% tcl::mathop::- 1 x
can't use non-numeric string as operand of "-"
-errorcode ARITH DOMAIN {non-numeric string}

Facts

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

See also