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.
+gives the sum of its arguments, and 0 with none.-with one argument gives its negative. With more, it subtracts each argument after the first from the first, from left to right.*gives the product of its arguments, and 1 with none./with one argument gives 1.0 divided by it. With more, it divides the first by each other argument, from left to right. Two integers give the integer quotient rounded down, so/ -7 2is -4. An integer zero divisor is the errordivide by zero, and a float zero divisor givesInf.%needs two integers and gives the remainder of the division rounded down, with the sign of the divisor:% -7 3is 2. A float is an error.**gives a power, and it groups from the right:** 2 3 2is 2 to the power of 9. With no argument it gives 1, and with one it gives that argument. An integer base with a negative integer exponent gives an integer, so** 2 -1is 0.
Arguments
| Argument | Type | Default | Description |
|---|---|---|---|
| NUMBER | number | required | An integer of any size or a float. |
| INTEGER | integer | required | An 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