Arithmetic Fuctions#
The arithmetic functions are all scalar functions; they penetrate into nested arrays all the way down to the scalars. The standard functions +
, -
, ×
, ÷
all act as you might expect:
8+9
4 3 2-3 6 4
length←20.5
height←13.1
area←length×height
area
(4 2 3)÷(1 1 5)(1 5 1)(5 1 1)
All of those functions can also be applied monadically. For example -x
is negative x
(or 0-x
). ÷x
is the reciprocal of x
(or 1÷x
)
-3 ¯1 4 ¯1 5
÷3
Monadic +
and ×
are designed for complex numbers. +
gives the complex conjugate. ×
gives a complex number with magnitude 1 (unless it is applied to 0
) in the direction of its argument. For real numbers, +
has no effect, and ×
gives the sign of its argument.
+1j2 0j¯1 4 3j3
×10 3j3 4j4 8j¯22 ¯3 0
*
is the power function. X*Y
is X
to the power of Y
1.3×1.3×1.3
1.3*3
9*0.5 ⍝ square root
20*¯1 ⍝ reciprocal
Monadically, *x
is e to the power of x
.
e←2.71828182845
(e*3.2) (*3.2)
A close relative of *
is ⍟
which is logarithm. a⍟b
is the base-a
logarithm of b
.
2⍟64
As a monad, ⍟x
is the natural logarithm of x
(or e⍟x
).
⍟1
⍟(*23.14)
a⌈b
, gives the maximum of a
and b
. While ⌈x
is the ceiling of x
, that is, the smallest integer larger than x
.
4 4 2 ¯4 0 ¯10 ⌈ 0
⌈1.5 3 ¯0.3 5.0001 ¯2.7
After seeing what ⌈
does, it is not hard to guess what ⌊
does. It is of course minimum and floor
3 2 10⌊¯2 5 5
⌊1.4 ¯6.02 5
Numbers can be rounded to the nearest integer with ⌊0.5+
. You can see it highlighted below because it is an idiom, recognised and optimised by the interpreter.
⌊0.5+3.3 3.7 6.5 1
Monadically, |x
gives the magnitude of x
. For real numbers, this is the absolute value.
| 2 ¯3 4 0 ¯10.5
| 3j4
Dyadically, |
is residue. x|y
is the remainder when y
is divided by x
.
3|0 1 2 3 4 5 6 7
1|2 3.4 0.31 5.2 ⍝ The fractional parts of each number